private <T extends Enum<T>> T convertEnum(Class<T> type, Object notation) { return NotationParserBuilder .toType(type) .noImplicitConverters() .fromCharSequence(new EnumFromCharSequenceNotationParser<T>(type)) .toComposite() .parseNotation(notation); }
public DefaultTypeConverter(final PathToFileResolver fileResolver) { registerConverter(new CharSequenceNotationConverter<Object, File>(new CharSequenceConverter<File>(File.class) { public void convert(String notation, NotationConvertResult<? super File> result) throws TypeConversionException { result.converted(fileResolver.resolve(notation)); } }), File.class); registerConverters(); }
@Override protected void convertNumberToNumber(BigDecimal n, NotationConvertResult<? super BigDecimal> result) { result.converted(n); } }
private void registerConverters() { registerConverter(new DoubleNumberConverter(Double.class), Double.class); registerConverter(new DoubleNumberConverter(double.class), double.class); registerConverter(new FloatNumberConverter(Float.class), Float.class); registerConverter(new FloatNumberConverter(float.class), float.class); registerConverter(new IntegerNumberConverter(Integer.class), Integer.class); registerConverter(new IntegerNumberConverter(int.class), int.class); registerConverter(new LongNumberConverter(Long.class), Long.class); registerConverter(new LongNumberConverter(long.class), long.class); registerConverter(new ShortNumberConverter(Short.class), Short.class); registerConverter(new ShortNumberConverter(short.class), short.class); registerConverter(new ByteNumberConverter(Byte.class), Byte.class); registerConverter(new ByteNumberConverter(byte.class), byte.class); registerConverter(new BigDecimalNumberConverter(), BigDecimal.class); registerConverter(new BigIntegerNumberConverter(), BigInteger.class); CharSequenceConverter<Boolean> booleanConverter = new BooleanConverter(); registerStringConverter(booleanConverter, Boolean.class); registerStringConverter(booleanConverter, boolean.class); registerStringConverter(new CharacterConverter(Character.class, Character.class), Character.class); registerStringConverter(new CharacterConverter(Character.class, char.class), char.class); registerConverter(new StringConverter(), String.class); }
public void convert(String notation, NotationConvertResult<? super Character> result) throws TypeConversionException { if (notation.length() != 1) { throw new TypeConversionException(String.format("Cannot convert string value '%s' with length %d to type %s", notation, notation.length(), target.getSimpleName())); } result.converted(notation.charAt(0)); } }
private static <T> NotationParser<Object, T> build(NotationConverter<Object, T> converter, Class<T> type) { return NotationParserBuilder .toType(type) .noImplicitConverters() .converter(converter) .toComposite(); }
@Override protected ConvertMethod create(Class<?> key, Class<?>[] params) { Method convertMethod = findConvertMethod(key); Annotation[][] parameterAnnotations = convertMethod.getParameterAnnotations(); String[] keyNames = new String[parameterAnnotations.length]; boolean[] optional = new boolean[parameterAnnotations.length]; for (int i = 0; i < parameterAnnotations.length; i++) { Annotation[] annotations = parameterAnnotations[i]; keyNames[i] = keyName(annotations); optional[i] = optional(annotations); } return new ConvertMethod(convertMethod, keyNames, optional); }
private <T> void registerStringConverter(NotationConverter<String, T> converter, Class<T> type) { parsers.put(type, build(new CharSequenceNotationConverter<Object, T>(converter), type)); }
public static synchronized ConvertMethod of(Class clazz) { return CONVERT_METHODS.get(clazz, EMPTY); } }
public TypedNotationConverter(TypeInfo<N> typeToken) { assert typeToken != null : "typeToken cannot be null"; this.typeToken = typeToken.getTargetType(); }
public static NormalizedTimeUnit millis(int value) { return new NormalizedTimeUnit(value, TimeUnit.MILLISECONDS); }
protected TypeConverter createTypeConverter(PathToFileResolver fileResolver) { return new DefaultTypeConverter(fileResolver); }
private static String keyName(Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation instanceof MapKey) { return ((MapKey) annotation).value(); } } throw new UnsupportedOperationException("No @Key annotation on parameter of parseMap() method"); }
public Set<String> getAvailableValues() { ValueCollectingDiagnosticsVisitor visitor = new ValueCollectingDiagnosticsVisitor(); notationParser.describe(visitor); return visitor.getValues(); }
static public <T extends Enum<T>> T toEnumValue(Class<T> enumType, CharSequence charSequence) { return NotationParserBuilder .toType(enumType) .noImplicitConverters() .fromCharSequence(new EnumFromCharSequenceNotationParser<T>(enumType)) .toComposite() .parseNotation(charSequence); } }
@Override public void convert(CharSequence notation, NotationConvertResult<? super T> result) throws TypeConversionException { try { result.converted(GUtil.toEnum(type, notation)); } catch (IllegalArgumentException e) { throw new TypeConversionException(e.getMessage(), e); } }
@Override protected void convertNumberToNumber(BigDecimal n, NotationConvertResult<? super Long> result) { result.converted(n.longValueExact()); } }
@Override public void convert(CharSequence notation, NotationConvertResult<? super T> result) throws TypeConversionException { try { result.converted(GUtil.toEnum(type, notation)); } catch (IllegalArgumentException e) { throw new TypeConversionException(e.getMessage(), e); } }
@Override protected void convertNumberToNumber(BigDecimal n, NotationConvertResult<? super Short> result) { result.converted(n.shortValueExact()); } }