Utility method to construct converter from 2 functions
@param <F> type to convert from
@param to function to convert to String
@param from function to convert from String
@return converter
185522
public static <F> AbstractJointConverter<F> of(final SerializableFunction<? super F, String> to,
			                                    final SerializableFunction<String, ? extends F> from) {
		return new AbstractJointConverter<F>() {
			@Override
			public String convertToString(F value, Locale locale) {
				return to.apply(value);
			}
			
			@Override
			public F convertToObject(String value, Locale locale) throws ConversionException {
				return from.apply(value);
			}
		};
	}
656665
def create(converter_func: ConversionMethod, caught: Exception):
        
        msg = 'Caught TypeError while calling conversion function \'' + str(converter_func.__name__) + '\'. ' \
              'Note that the conversion function signature should be \'' + conversion_method_example_signature_str \
              + '\' (unpacked options mode - default) or ' + multioptions_conversion_method_example_signature_str \
              + ' (unpack_options = False).' \
              + 'Caught error message is : ' + caught.__class__.__name__ + ' : ' + str(caught)
        return CaughtTypeError(msg).with_traceback(caught.__traceback__)
484607
@SuppressWarnings("unchecked")
    public <T> T convert(Object source, String syntax, Class<T> toClass) {
        Object result = null;

        // What are we converting form
        Class<?> fromClass = source.getClass();

        // Deal with primitives
        Class<?> targetClass = toClass;
        if (toClass.isPrimitive()) {
            targetClass = primitiveTypeMap.get(toClass);
        }

        // Try to convert with any syntax we have been given
        Converter syntaxConverter = converters.get(makeConverterKey(fromClass, syntax, targetClass));
        if (syntaxConverter != null) {
            try {
                result = syntaxConverter.convert(source, targetClass);
            } catch (Exception e) {
                // Ignore as we may still be able to convert successfully
            }
        }

        // Do we actually need to do any conversion?
        if (result == null && targetClass.isAssignableFrom(fromClass)) {
            result = source;
        }

        // If we were given a syntax and we failed to convert drop back to any mapping
        // that will work from class -> to class
        if (result == null && syntax != null) {
            Converter nullSyntaxConverter = converters.get(makeConverterKey(fromClass, null, targetClass));
            if (nullSyntaxConverter != null) {
                try {
                    result = nullSyntaxConverter.convert(source, targetClass);
                } catch (Exception e) {
                    // Handled at the end of the method
                }
            }
        }

        if (result == null) {
            throw new ConverterException(String.format(
                    "Cannot convert %1$s of class %2$s via syntax %3$s to class %4$s", source, source.getClass(),
                    syntax, toClass));
        }

        // We cannot do the safe thing of doing a .cast as we need to rely on auto-unboxing to deal with primitives!
        return (T)result;
    }
1138723
function toFunction(obj, couldNotCastError) {
	if (obj == null) {
		throw couldNotCastError;
	}

	var result;
	if (typeof obj === 'object') {
		if (obj.hasOwnProperty('constructor')) {
			if (obj.constructor.prototype !== obj) {
				throw couldNotCastError;
			}
			result = obj.constructor;
		} else {
			var EmptyInitialiser = function() {};
			EmptyInitialiser.prototype = obj;
			Object.defineProperty(obj, 'constructor', {
				enumerable: false, value: EmptyInitialiser
			});
			result = EmptyInitialiser;
		}
	} else if (typeof obj === 'function') {
		result = obj;
	} else {
		throw couldNotCastError;
	}
	return result;
}
51952
public static Converter converterFor( Class type )
    {
        if (!converters.containsKey( type ))
            throw new IllegalArgumentException( "Don't know type: " + type );

        return converters.get( type );
    }
706492
private static void copy(Class<?> toType, Object to, Object... from) {
        int f = 0, fi = 0,
            length = Array.getLength(from[0]),
            total = from.length > 1 ? Array.getLength(to) : length;
        if ( Types.arrayDimensions(to.getClass()) == 1 ) {
            for ( int i = 0; i < total; i++ ) {
                Object value = Array.get(from[f], fi++);
                try {
                    value = Primitive.unwrap(
                            Types.castObject(value, toType, Types.CAST));
                } catch (UtilEvalError e) { /* ignore cast errors */ }
                if ( Byte.TYPE == toType )
                    Array.setByte(to, i, (byte) value);
                else if ( Short.TYPE == toType )
                    Array.setShort(to, i, (short) value);
                else if ( Integer.TYPE == toType )
                    Array.setInt(to, i, (int) value);
                else if ( Long.TYPE == toType )
                    Array.setLong(to, i, (long) value);
                else if ( Float.TYPE == toType )
                    Array.setFloat(to, i, (float) value);
                else if ( Double.TYPE == toType )
                    Array.setDouble(to, i, (double) value);
                else if ( Character.TYPE == toType )
                    Array.setChar(to, i, (char) value);
                else if ( Boolean.TYPE == toType )
                    Array.setBoolean(to, i, (boolean) value);
                else
                    Array.set(to, i, value);

                // concatenate multiple from arrays
                if ( length < total && fi == length && f+1 < from.length ) {
                    length = Array.getLength(from[++f]);
                    fi = 0;
                }
            }
        } else for ( int i = 0; i < total; i++ ) {
            // concatenate multiple from arrays
            if ( length < total && fi == length && f+1 < from.length ) {
                length = Array.getLength(from[++f]);
                fi = 0;
            }

            Object frm = Array.get(from[f], fi++);

            // null dimension example: new Integer[2][]
            if ( null == frm ) {
                Array.set(to, i, null);
                continue;
            }

            Object tto = Array.get(to, i);

            // mixed array lengths in multiple dimensions ex: {{1,2}, {3}}
            if ( Array.getLength(frm) != Array.getLength(tto) )
                Array.set(to, i,
                    tto = Array.newInstance(toType, dimensions(frm)));

            // recurse copy for next array dimension
            copy(toType, tto, frm);
        }
    }
262983
public void convert(String fromVariable, TypeMirror to) throws IOException, IllegalConversionException
    {
        TypeMirror from = getLocalType(fromVariable);
        if (Typ.isAssignable(from, to))
        {
            tload(fromVariable);
            checkcast(to);
            return;
        }
        if (to.getKind() == TypeKind.DECLARED)
        {
            DeclaredType dt = (DeclaredType) to;
            TypeElement te = (TypeElement) dt.asElement();
            ExecutableElement c = El.getConstructor(te, from);
            if (c != null)
            {
                anew(te);
                dup();
                tload(fromVariable);
                invokespecial(c);
                return;
            }
            ExecutableElement m = El.getMethod(te, "valueOf", from);
            if (m != null && m.getModifiers().contains(Modifier.STATIC) && Typ.isAssignable(m.getReturnType(), to))
            {
                tload(fromVariable);
                invokestatic(m);
                return;
            }
        }
        if (from.getKind() == TypeKind.DECLARED && Typ.isPrimitive(to))
        {
            DeclaredType dt = (DeclaredType) from;
            TypeElement te = (TypeElement) dt.asElement();
            String str = to.getKind().name().toLowerCase() + "Value";
            ExecutableElement m = El.getMethod(te, str);
            // xxxValue()
            // xxxValue()
            if (m != null && Typ.isAssignable(m.getReturnType(), to))
            {
                tload(fromVariable);
                invokevirtual(m);
                return;
            }
        }
        if (Typ.isPrimitive(from))
        {
            switch (from.getKind())
            {
                case BYTE:
                case CHAR:
                case SHORT:
                case INT:
                    switch (to.getKind())
                    {
                        case BYTE:
                        case CHAR:
                        case SHORT:
                        case INT:
                        case LONG:
                        case FLOAT:
                        case DOUBLE:
                            tload(fromVariable);
                            i2t();
                            break;
                        default:
                            throw new IllegalConversionException("Cannot convert from " + from + " to " + to);
                    }
                    break;
                case LONG:
                    switch (to.getKind())
                    {
                        case BYTE:
                        case CHAR:
                        case SHORT:
                        case INT:
                            tload(fromVariable);
                            l2t();
                            break;
                        case FLOAT:
                        case DOUBLE:
                            tload(fromVariable);
                            l2t();
                            break;
                        default:
                            throw new IllegalConversionException("Cannot convert from " + from + " to " + to);
                    }
                    break;
                case FLOAT:
                    switch (to.getKind())
                    {
                        case BYTE:
                        case CHAR:
                        case SHORT:
                        case INT:
                            tload(fromVariable);
                            f2t();
                            break;
                        case LONG:
                        case DOUBLE:
                            tload(fromVariable);
                            f2t();
                            break;
                        default:
                            throw new IllegalConversionException("Cannot convert from " + from + " to " + to);
                    }
                    break;
                case DOUBLE:
                    switch (to.getKind())
                    {
                        case BYTE:
                        case CHAR:
                        case SHORT:
                        case INT:
                            tload(fromVariable);
                            d2t();
                            break;
                        case LONG:
                        case FLOAT:
                            tload(fromVariable);
                            d2t();
                            break;
                        default:
                            throw new IllegalConversionException("Cannot convert from " + from + " to " + to);
                    }
                    break;
                default:
                    throw new IllegalConversionException("Cannot convert from " + from + " to " + to);
            }
        }
        throw new IllegalConversionException("!Cannot convert from " + from+" "+from.getKind() + " to " + to+" "+to.getKind());
    }
947136
public static <F, T> List<T> transform(Collection<F> fromList,
            Function<? super F, ? extends T> function) {
        if (CollectionUtil.isEmpty(fromList)) {
            return Collections.emptyList();
        }

        List<T> ret = new ArrayList<T>(fromList.size());
        for (F f : fromList) {
            T t = function.apply(f);
            if (t == null) {
                continue;
            }
            ret.add(t);
        }

        return ret;
    }
220651
public static <T> FnListOf<T> of(final Type<T> type) {
        return new FnListOf<T>(type);
    }
1367616
public <T> Converter<T> getConverter(Type type, boolean isCustomFirst) {
		Converter<T> converter = null;
		if (isCustomFirst) {
			converter = this.getCustomConverter(type);
			if (null == converter) {
				converter = this.getDefaultConverter(type);
			}
		} else {
			converter = this.getDefaultConverter(type);
			if (null == converter) {
				converter = this.getCustomConverter(type);
			}
		}
		return converter;
	}
618817
public static Expression numericConversion(final Expression expr, final Type to) {
    if (to.equals(expr.resultType())) {
      return expr;
    }
    if (!isNumericPrimitive(to) || !isNumericPrimitive(expr.resultType())) {
      throw new IllegalArgumentException("Cannot convert from " + expr.resultType() + " to " + to);
    }
    return new Expression(to, expr.features()) {
      @Override
      protected void doGen(CodeBuilder adapter) {
        expr.gen(adapter);
        adapter.cast(expr.resultType(), to);
      }
    };
  }
662580
public static <T> FnSetOf<T> of(final Type<T> type) {
        return new FnSetOf<T>(type);
    }
148564
public static <T> Converter<T> createConverter(
      Class<T> klass, InstantiatorModule... modules) {
    return createConverterForType(klass, modules);
  }
915927
public <T> T convert(Object value, Class<T> type) {
		return converter.convert(value, type);
	}
1185874
public static final <T> Function<T,List<T>> intoSingletonListOf(final Type<T> type) {
        return new IntoSingletonList<T>();
    }
449693
private String makeConverterKey(Class<?> fromClass, String syntax, Class<?> toClass) {
        StringBuilder key = new StringBuilder();
        if (syntax==null) {
            syntax="";
        }
        key.append(fromClass.getName()).append(KEY_SEP).append(syntax).append(KEY_SEP).append(toClass.getName());
        return key.toString();
    }
479190
public Converter<NetworkResponse, ?> getResponseConverter(Type type, Annotation[] annotations) {
        checkNotNull(type, "type == null");
        checkNotNull(annotations, "annotations == null");

        for (int i = 0, count = converterFactories.size(); i < count; i++) {
            Converter<NetworkResponse, ?> converter =
                    converterFactories.get(i).fromResponse(type, annotations);
            if (converter != null) {
                return converter;
            }
        }

        StringBuilder builder = new StringBuilder("Could not locate Response converter for ")
                .append(type)
                .append(". Tried:");
        for (Factory converterFactory : converterFactories) {
            builder.append("\n * ").append(converterFactory.getClass().getName());
        }
        throw new IllegalArgumentException(builder.toString());
    }