@Override @SuppressWarnings("unchecked") public <T> T prompt(final String message, final Class<T> clazz, final T defaultIfEmpty) { Object result; String input; do { String query = " [" + defaultIfEmpty + "] "; if (defaultIfEmpty instanceof Boolean) { if (Boolean.TRUE.equals(defaultIfEmpty)) query = " [Y/n] "; else { query = " [y/N] "; } } input = prompt(message + query); if ((input == null) || "".equals(input.trim())) { result = defaultIfEmpty; } else { input = input.trim(); try { result = DataConversion.convert(input, clazz); } catch (Exception e) { result = InvalidInput.INSTANCE; } } } while ((result instanceof InvalidInput)); return (T) result; }
public static Statement convert(final Context context, Object input, final MetaClass targetType) { try { if (input instanceof Statement) { if (input instanceof LiteralValue<?>) { input = ((LiteralValue<?>) input).getValue(); } else { if ("null".equals(((Statement) input).generate(context))) { return (Statement) input; } assertAssignableTypes(context, ((Statement) input).getType(), targetType); return (Statement) input; } } if (input != null && MetaClassFactory.get(input.getClass()) .getOuterComponentType() .getFullyQualifiedName() .equals(MetaClass.class.getName())) { return generate(context, input); } if (Object.class.getName().equals(targetType.getFullyQualifiedName())) { return generate(context, input); } Class<?> inputClass = input == null ? Object.class : input.getClass(); if (MetaClass.class.isAssignableFrom(inputClass)) { inputClass = Class.class; } final Class<?> targetClass = targetType.asBoxed().asClass(); if (NullType.class.getName().equals(targetClass.getName())) { return generate(context, input); } if (!targetClass.isAssignableFrom(inputClass) && DataConversion.canConvert(targetClass, inputClass)) { return generate(context, DataConversion.convert(input, targetClass)); } else { return generate(context, input); } } catch (NumberFormatException nfe) { throw new InvalidTypeException(nfe); } }
@Override @SuppressWarnings("unchecked") public <T> T prompt(final String message, final Class<T> clazz) { Object result; Object input; do { input = prompt(message); try { result = DataConversion.convert(input, clazz); } catch (Exception e) { result = InvalidInput.INSTANCE; } } while ((result instanceof InvalidInput)); return (T) result; }