PropertiesSettingsProvider implements SettingsProvider { @Override public <T> Optional<T> getAs(String settingName, Class<T> cls) { Optional<String> settingCandidate = get(settingName); if (settingCandidate.isPresent()) { try { Constructor<T> constructor = cls.getConstructor(String.class); return Optional.of(constructor.newInstance(settingCandidate.get())); } catch (Exception e) { return Optional.empty(); } } return Optional.empty(); } PropertiesSettingsProvider(Properties properties); @Override Optional<String> get(String settingName); @Override List<String> getList(String settingName, List<String> defaultValue); @Override Optional<T> getAs(String settingName, Class<T> cls);  }
@Test public void testGetAs() throws Exception { assertThat(settingsProvider.getAs(MISSING_KEY, StringBuffer.class).isPresent(), is(false)); assertThat(settingsProvider.getAs(EXISTING_KEY, Math.class).isPresent(), is(false)); assertThat(settingsProvider.getAs(EXISTING_KEY, StringBuffer.class).isPresent(), is(true)); assertThat(settingsProvider.getAs(EXISTING_KEY, StringBuffer.class).get().toString(), equalTo(VALUE)); }