@Test public void testConvertValue() { DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); assertThat(100 == (Integer) c.convertValue("aHundred", Collections.singletonList(PropertyValue.createValue("aHundred", "100")), TypeLiteral.of(Integer.class))).isTrue(); }
@SuppressWarnings("unchecked") protected <T> T convertValue(String key, List<PropertyValue> values, TypeLiteral<T> type) { if (values != null && !values.isEmpty()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); ConversionContext context = new ConversionContext.Builder(this, key, type) .setValues(values) .build(); String value = values.get(0).getValue(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); if (t != null) { return t; } } catch (Exception e) { LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert createValue: " + value, e); } } if (type.equals(TypeLiteral.of(String.class))) { return (T) value; } throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + ", supported formats: " + context.getSupportedFormats()); } return null; }
DefaultConfiguration implements Configuration, Serializable { @SuppressWarnings("unchecked") protected <T> T convertValue(String key, List<PropertyValue> values, TypeLiteral<T> type) { if (values != null && !values.isEmpty()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); ConversionContext context = new ConversionContext.Builder(this, key, type) .setValues(values) .build(); String value = values.get(0).getValue(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); if (t != null) { return t; } } catch (Exception e) { LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert createValue: " + value, e); } } if (type.equals(TypeLiteral.of(String.class))) { return (T) value; } throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + ", supported formats: " + context.getSupportedFormats()); } return null; } }
DefaultConfiguration implements Configuration, Serializable { @SuppressWarnings("unchecked") protected <T> T convertValue(String key, List<PropertyValue> values, TypeLiteral<T> type) { if (values != null && !values.isEmpty()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); ConversionContext context = new ConversionContext.Builder(this, key, type) .setValues(values) .build(); String value = values.get(0).getValue(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); if (t != null) { return t; } } catch (Exception e) { LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert createValue: " + value, e); } } if (type.equals(TypeLiteral.of(String.class))) { return (T) value; } throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + ", supported formats: " + context.getSupportedFormats()); } return null; } DefaultConfiguration(ConfigurationContext configurationContext); }
DefaultConfiguration implements Configuration, Serializable { @SuppressWarnings("unchecked") protected <T> T convertValue(String key, List<PropertyValue> values, TypeLiteral<T> type) { if (values != null && !values.isEmpty()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); ConversionContext context = new ConversionContext.Builder(this, key, type) .setValues(values) .build(); String value = values.get(0).getValue(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); if (t != null) { return t; } } catch (Exception e) { LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert createValue: " + value, e); } } if (type.equals(TypeLiteral.of(String.class))) { return (T) value; } throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + ", supported formats: " + context.getSupportedFormats()); } return null; } DefaultConfiguration(ConfigurationContext configurationContext); @Override String get(String key); List<PropertyValue> getValues(String key); @Override String getOrDefault(String key, String defaultValue); @Override T getOrDefault(String key, Class<T> type, T defaultValue); @Override Map<String, String> getProperties(); @Override T get(String key, Class<T> type); @Override T get(String key, TypeLiteral<T> type); @Override T getOrDefault(String key, TypeLiteral<T> type, T defaultValue); @Override Configuration with(ConfigOperator operator); @Override T query(ConfigQuery<T> query); @Override ConfigurationContext getContext(); @Override ConfigurationSnapshot getSnapshot(Iterable<String> keys); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); }
DefaultConfiguration implements Configuration, Serializable { @SuppressWarnings("unchecked") protected <T> T convertValue(String key, List<PropertyValue> values, TypeLiteral<T> type) { if (values != null && !values.isEmpty()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); ConversionContext context = new ConversionContext.Builder(this, key, type) .setValues(values) .build(); String value = values.get(0).getValue(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); if (t != null) { return t; } } catch (Exception e) { LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert createValue: " + value, e); } } if (type.equals(TypeLiteral.of(String.class))) { return (T) value; } throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + ", supported formats: " + context.getSupportedFormats()); } return null; } DefaultConfiguration(ConfigurationContext configurationContext); @Override String get(String key); List<PropertyValue> getValues(String key); @Override String getOrDefault(String key, String defaultValue); @Override T getOrDefault(String key, Class<T> type, T defaultValue); @Override Map<String, String> getProperties(); @Override T get(String key, Class<T> type); @Override T get(String key, TypeLiteral<T> type); @Override T getOrDefault(String key, TypeLiteral<T> type, T defaultValue); @Override Configuration with(ConfigOperator operator); @Override T query(ConfigQuery<T> query); @Override ConfigurationContext getContext(); @Override ConfigurationSnapshot getSnapshot(Iterable<String> keys); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }