This utility method picks the instance type to be used for executing programs.
<p>

@param types The available types.
@return The type to be used for scheduling.

@throws CompilerException
@throws IllegalArgumentException
353292
private InstanceTypeDescription getType(Map<InstanceType, InstanceTypeDescription> types)
	throws CompilerException
	{
		if (types == null || types.size() < 1) {
			throw new IllegalArgumentException("No instance type found.");
		}
		
		InstanceTypeDescription retValue = null;
		long totalMemory = 0;
		int numInstances = 0;
		
		final Iterator<InstanceTypeDescription> it = types.values().iterator();
		while(it.hasNext())
		{
			final InstanceTypeDescription descr = it.next();
			
			// skip instances for which no hardware description is available
			// this means typically that no 
			if (descr.getHardwareDescription() == null || descr.getInstanceType() == null) {
				continue;
			}
			
			final int curInstances = descr.getMaximumNumberOfAvailableInstances();
			final long curMemory = curInstances * descr.getHardwareDescription().getSizeOfFreeMemory();
			
			// get, if first, or if it has more instances and not less memory, or if it has significantly more memory
			// and the same number of cores still
			if ( (retValue == null) ||
				(curInstances > numInstances && (int) (curMemory * 1.2f) > totalMemory) ||
				(curInstances * retValue.getInstanceType().getNumberOfCores() >= numInstances && 
							(int) (curMemory * 1.5f) > totalMemory)
				)
			{
				retValue = descr;
				numInstances = curInstances;
				totalMemory = curMemory;
			}
		}
		
		if (retValue == null) {
			throw new CompilerException("No instance currently registered at the job-manager. Retry later.\n" +
				"If the system has recently started, it may take a few seconds until the instances register.");
		}
		
		return retValue;
	}
251249
@BindTypeVariable
    public Typed<SOURCE> getSourceType() {
        final Position.Readable<SOURCE> source = getSourcePosition();
        final Type result = narrow(source);
        if (!TypeUtils.equals(result, source.getType())) {
            return new Typed<SOURCE>() {

                @Override
                public Type getType() {
                    return result;
                }
            };
        }
        return source;
    }
570280
public <T> T createInstance(Class<T> type, String typeName) {
        try {
            return type.cast(getType(typeName).newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalArgumentException("Cannot create instance of class " + type.getName(), e);
        }
    }
118549
@SuppressWarnings("serial")
    public static Type[] forTypeParameters(final Class<?> type) {
        Type[] result = new Type[type.getTypeParameters().length];
        for (int i = 0; i < result.length; i++) {
            final int index = i;
            result[i] = forTypeProvider(new DefaultTypeProvider() {
                @Override
                public Type getType() {
                    return type.getTypeParameters()[index];
                }
            });
        }
        return result;
    }
1328852
public static Class<?> getTypeByDescriptor(TypeDescriptor type) {
		if (type == null)
			throw new NullPointerException("Type descriptor cannot be null");

		return getType(type.getName(), type.getLibrary());
	}
1079397
private static Class <?> type2class(Type type){
		if(type instanceof Class <?>){
			return (Class <?>) type;	//base case
		}else if(type instanceof ParameterizedType){
			return type2class( ((ParameterizedType) type).getRawType() );
		}else if(type instanceof TypeVariable<?>){
			return type2class( ((TypeVariable<?>) type).getBounds()[0] );
		}else if(type instanceof WildcardType){
			return type2class( ((WildcardType) type).getUpperBounds()[0] );
		}else{
			throw new IllegalArgumentException("Cannot convert type to class: " + type);
		}
	}
1067083
@SuppressWarnings("unchecked")
    public <T extends XPathBuilder> T setType(final String type) {
        this.type = type;
        return (T) this;
    }
1177456
@SuppressWarnings("WeakerAccess")
  public CreateInstanceRequest setType(@Nonnull Instance.Type type) {
    Preconditions.checkNotNull(type);
    Preconditions.checkArgument(type != Instance.Type.UNRECOGNIZED, "Type is unrecognized");
    builder.getInstanceBuilder().setType(type.toProto());
    return this;
  }
150196
public static <T> Class<FieldParser<T>> getParserForType(Class<T> type) {
		Class<? extends FieldParser<?>> parser = PARSERS.get(type);
		if (parser == null) {
			return null;
		} else {
			@SuppressWarnings("unchecked")
			Class<FieldParser<T>> typedParser = (Class<FieldParser<T>>) parser;
			return typedParser;
		}
	}
437117
@SuppressWarnings("unchecked")
	public <T> DatagraphEnvironment setInstance(Class<T> type, T value) {
		((Map<Class<T>, T>) instances).put(type, value);
		return this;
	}
1215461
public static <T> Class<?>[] getClasses(Type[] types) {
        if (types == null)
            return null;
        if (types.length == 0)
            return new Class<?>[0];

        Class<?>[] classes = new Class[types.length];
        // Resolve types by chasing down type variables.
        for (int i = 0; i < types.length; i++) {
            classes[i] = getClass(types[i]);
        }
        return classes;
    }
520423
public void instanceOfType(Local<?> target, Local<?> source, TypeId<?> type) {
        addInstruction(new ThrowingCstInsn(Rops.INSTANCE_OF, sourcePosition,
                RegisterSpecList.make(source.spec()), catches, type.constant));
        moveResult(target, true);
    }
350295
public JClassType getType( String type ) throws UnableToCompleteException {
        try {
            return typeOracle.getType( type );
        } catch ( NotFoundException e ) {
            logger.log( TreeLogger.ERROR, "TypeOracle could not find " + type );
            throw new UnableToCompleteException();
        }
    }
922029
@SuppressWarnings("unchecked")
    private ClassesTransformer<T> getTyped(ClassesTransformer<? extends T> classesTransformer) {
        return (ClassesTransformer<T>) classesTransformer;
    }
428128
public static Type boxedType(Type type) {
        if (type instanceof Class<?>) {
            return boxedClass((Class<?>) type);
        } else {
            return type;
        }
    }
818297
protected <T> TypeConfiguration<T> type( Class<T> type ) {
        if ( type.isPrimitive() ) {
            throw new IllegalArgumentException( "Type " + type + " is a primitive. Call primitiveType(Class) instead" );
        }
        return new TypeConfiguration<T>( type );
    }
1243317
public static int type(String type)
    {
        if ("request".equalsIgnoreCase(type))
            return __REQUEST;
        if ("forward".equalsIgnoreCase(type))
            return __FORWARD;
        if ("include".equalsIgnoreCase(type))
            return __INCLUDE;
        if ("error".equalsIgnoreCase(type))
            return __ERROR;
        throw new IllegalArgumentException(type);
    }