PropertiesHelper { public static List<Properties> segment( Properties properties ) { Set<String> keys = properties.stringPropertyNames(); HashMap<String, Properties> map = new HashMap<String, Properties>(); for ( String key : keys ) { String[] split = key.split( "\\.", 2 ); String segmentKey; String propertyKey; if ( split.length == 1 ) { segmentKey = ""; propertyKey = key; } else { segmentKey = split[ 0 ]; propertyKey = split[ 1 ]; } Properties segment = getProperties( map, segmentKey ); segment.setProperty( propertyKey, properties.getProperty( key ) ); map.put( segmentKey, segment ); } List<Properties> list = new ArrayList<Properties>(); list.addAll( map.values() ); return list; }  static List<Properties> segment( Properties properties );  }
@Test public void testSegmentsPropertiesIntoMultipleProperties() throws Exception { Properties properties = new Properties(); properties.setProperty( "foodmart.name", "Foodmart" ); properties.setProperty( "foodmart.connectString", "jdbc:mondrian:host=aplace" ); properties.setProperty( "sample.name", "Samples" ); properties.setProperty( "sample.connectString", "jdbc:mondrian:host=overthere" ); properties.setProperty( "name", "noName" ); properties.setProperty( "connectString", "jdbc:mondrian:blah" ); List<Properties> list = PropertiesHelper.segment( properties ); Collections.sort( list, new Comparator<Properties>() { @Override public int compare( Properties o1, Properties o2 ) { return o1.getProperty( "name" ).compareTo( o2.getProperty( "name" ) ); } } ); Properties foodmart = list.get( 0 ); Assert.assertEquals( "Foodmart", foodmart.getProperty( "name" ) ); Assert.assertEquals( "jdbc:mondrian:host=aplace", foodmart.getProperty( "connectString" ) ); Properties sample = list.get( 1 ); Assert.assertEquals( "Samples", sample.getProperty( "name" ) ); Assert.assertEquals( "jdbc:mondrian:host=overthere", sample.getProperty( "connectString" ) ); Properties noname = list.get( 2 ); Assert.assertEquals( "noName", noname.getProperty( "name" ) ); Assert.assertEquals( "jdbc:mondrian:blah", noname.getProperty( "connectString" ) ); }