@Test public void testExtractPropertiesWithPrefix() { Properties properties = new Properties(); properties.setProperty("k1.kk1", "v1"); properties.setProperty("k1.kk2", "v2"); properties.setProperty("k2.kk", "v3"); Properties extractedPropertiesK1 = PropertiesUtils.extractPropertiesWithPrefix(properties, Optional.of("k1")); Assert.assertEquals(extractedPropertiesK1.getProperty("k1.kk1"), "v1"); Assert.assertEquals(extractedPropertiesK1.getProperty("k1.kk2"), "v2"); Assert.assertTrue(!extractedPropertiesK1.containsKey("k2.kk")); Properties extractedPropertiesK2 = PropertiesUtils.extractPropertiesWithPrefix(properties, Optional.of("k2")); Assert.assertTrue(!extractedPropertiesK2.containsKey("k1.kk1")); Assert.assertTrue(!extractedPropertiesK2.containsKey("k1.kk2")); Assert.assertEquals(extractedPropertiesK2.getProperty("k2.kk"), "v3"); Properties extractedPropertiesK3 = PropertiesUtils.extractPropertiesWithPrefix(properties, Optional.of("k3")); Assert.assertTrue(!extractedPropertiesK3.containsKey("k1.kk1")); Assert.assertTrue(!extractedPropertiesK3.containsKey("k1.kk1")); Assert.assertTrue(!extractedPropertiesK3.containsKey("k2.kk")); }
public static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix) { Preconditions.checkNotNull(properties); Preconditions.checkNotNull(prefix); Properties extractedProperties = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (StringUtils.startsWith(entry.getKey().toString(), prefix.or(StringUtils.EMPTY))) { extractedProperties.put(entry.getKey().toString(), entry.getValue()); } } return extractedProperties; }
PropertiesUtils { public static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix) { Preconditions.checkNotNull(properties); Preconditions.checkNotNull(prefix); Properties extractedProperties = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (StringUtils.startsWith(entry.getKey().toString(), prefix.or(StringUtils.EMPTY))) { extractedProperties.put(entry.getKey().toString(), entry.getValue()); } } return extractedProperties; } }
PropertiesUtils { public static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix) { Preconditions.checkNotNull(properties); Preconditions.checkNotNull(prefix); Properties extractedProperties = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (StringUtils.startsWith(entry.getKey().toString(), prefix.or(StringUtils.EMPTY))) { extractedProperties.put(entry.getKey().toString(), entry.getValue()); } } return extractedProperties; }  }
PropertiesUtils { public static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix) { Preconditions.checkNotNull(properties); Preconditions.checkNotNull(prefix); Properties extractedProperties = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (StringUtils.startsWith(entry.getKey().toString(), prefix.or(StringUtils.EMPTY))) { extractedProperties.put(entry.getKey().toString(), entry.getValue()); } } return extractedProperties; }  static Properties combineProperties(Properties... properties); static Map<String, ?> propsToStringKeyMap(Properties properties); static boolean getPropAsBoolean(Properties properties, String key, String defaultValue); static int getPropAsInt(Properties properties, String key, int defaultValue); static long getPropAsLong(Properties properties, String key, long defaultValue); static List<String> getPropAsList(Properties properties, String key); static List<String> getPropAsList(Properties properties, String key, String def); static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix); static String serialize(Properties properties); static Properties deserialize(String serialized); static String prettyPrintProperties(Properties properties); }
PropertiesUtils { public static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix) { Preconditions.checkNotNull(properties); Preconditions.checkNotNull(prefix); Properties extractedProperties = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (StringUtils.startsWith(entry.getKey().toString(), prefix.or(StringUtils.EMPTY))) { extractedProperties.put(entry.getKey().toString(), entry.getValue()); } } return extractedProperties; }  static Properties combineProperties(Properties... properties); static Map<String, ?> propsToStringKeyMap(Properties properties); static boolean getPropAsBoolean(Properties properties, String key, String defaultValue); static int getPropAsInt(Properties properties, String key, int defaultValue); static long getPropAsLong(Properties properties, String key, long defaultValue); static List<String> getPropAsList(Properties properties, String key); static List<String> getPropAsList(Properties properties, String key, String def); static Properties extractPropertiesWithPrefix(Properties properties, Optional<String> prefix); static String serialize(Properties properties); static Properties deserialize(String serialized); static String prettyPrintProperties(Properties properties);  }