ConfigurationParameters implements Map<String, Object> { @Nullable public <T> T getConfigValue(@NotNull String key, @Nullable T defaultValue, @Nullable Class<T> targetClass) { if (options.containsKey(key)) { return convert(options.get(key), defaultValue, targetClass); } else { return defaultValue; } } private  ConfigurationParameters(); private  ConfigurationParameters(@NotNull Map<String, ?> options); @NotNull static ConfigurationParameters of(@NotNull ConfigurationParameters... params); @NotNull static ConfigurationParameters of(@NotNull Properties properties); @NotNull static ConfigurationParameters of(@NotNull Dictionary<String, Object> properties); @NotNull static ConfigurationParameters of(@NotNull Map<?, ?> map); @NotNull static ConfigurationParameters of(@NotNull String key, @NotNull Object value); @NotNull static ConfigurationParameters of(@NotNull String key1, @NotNull Object value1,
                                             @NotNull String key2, @NotNull Object value2); boolean contains(@NotNull String key); @Nullable T getConfigValue(@NotNull String key, @Nullable T defaultValue,
                                @Nullable Class<T> targetClass); @NotNull T getConfigValue(@NotNull String key, @NotNull T defaultValue); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override Object get(Object key); @Override Object put(String key, Object value); @Override Object remove(Object key); @Override void putAll(@NotNull Map<? extends String, ?> m); @Override void clear(); @NotNull @Override Set<String> keySet(); @NotNull @Override Collection<Object> values(); @NotNull @Override Set<Entry<String,Object>> entrySet(); static final ConfigurationParameters EMPTY; }
@Test public void testGetConfigValue() { Map<String, String> map = new HashMap<>(); map.put("o1", "v"); ConfigurationParameters options = ConfigurationParameters.of(map); assertEquals("v", options.getConfigValue("o1", "v2")); assertEquals("v2", options.getConfigValue("missing", "v2")); }
@Test public void testDefaultValue() { TestObject obj = new TestObject("t"); Integer int1000 = 1000; ConfigurationParameters options = ConfigurationParameters.EMPTY; assertEquals(obj, options.getConfigValue("missing", obj)); assertEquals(int1000, options.getConfigValue("missing", int1000)); assertNull(options.getConfigValue("missing", null, null)); assertNull(options.getConfigValue("missing", null, String.class)); assertEquals(obj, options.getConfigValue("missing", obj, null)); assertEquals(obj, options.getConfigValue("missing", obj, TestObject.class)); assertEquals(int1000, options.getConfigValue("missing", int1000, Integer.class)); }