CorrelationKeySet implements Serializable { public List<CorrelationKeySet> findSubSets() { List<CorrelationKeySet> subSets = new ArrayList<CorrelationKeySet>(); CorrelationKey opaqueKey = null; boolean containsNonOpaque = false; CorrelationKeySet explicitKeySet = new CorrelationKeySet(); for( CorrelationKey ckey : correlationKeys ) { if( ckey.getCorrelationSetName().equals("-1") ) { opaqueKey = ckey; } else { containsNonOpaque = true; } explicitKeySet.add(ckey); } if( opaqueKey != null && containsNonOpaque ) { explicitKeySet.correlationKeys.remove(opaqueKey); } for( int setIndex = 0; setIndex < Math.pow(2, explicitKeySet.correlationKeys.size()); setIndex++ ) { CorrelationKeySet subKeySet = new CorrelationKeySet(); int bitPattern = setIndex; Iterator<CorrelationKey> ckeys = explicitKeySet.iterator(); while( ckeys.hasNext() && bitPattern > 0 ) { CorrelationKey ckey = ckeys.next(); if( (bitPattern & 0x01) > 0 ) { subKeySet.add(ckey); } bitPattern = bitPattern >> 1; } if(!subKeySet.isEmpty()) { subSets.add(subKeySet); } } if( subSets.isEmpty() ) { subSets.add(new CorrelationKeySet()); } return subSets; } CorrelationKeySet();  CorrelationKeySet(String canonicalForm); CorrelationKeySet add(CorrelationKey ck); boolean isOpaque(); boolean isRoutableTo(CorrelationKeySet candidateKeySet, boolean isAllRoute); boolean containsAll(CorrelationKeySet c); boolean isEmpty(); boolean contains(CorrelationKey correlationKey); Iterator<CorrelationKey> iterator(); void clear(); @Override boolean equals(Object o); List<CorrelationKeySet> findSubSets(); String toCanonicalString(); @Override String toString(); void restore(String canonicalForm); final static String VERSION_1; final static String VERSION_2; }
@Test public void testFindSubSets() throws Exception { StringBuffer buf = new StringBuffer(); for( CorrelationKeySet subSet : new CorrelationKeySet().findSubSets() ) { if( buf.length() > 0 ) { buf.append(","); } buf.append("'").append(subSet.toCanonicalString()).append("'"); } assertEquals("'@2'", buf.toString()); CorrelationKeySet keySet = new CorrelationKeySet(); keySet.add(keyX); keySet.add(keyY); keySet.add(keyZ); assertTrue(keySet.findSubSets().size() == 7); keySet = new CorrelationKeySet(); keySet.add(optX); keySet.add(optY); keySet.add(optZ); buf = new StringBuffer(); for( CorrelationKeySet subSet : keySet.findSubSets() ) { if( buf.length() > 0 ) { buf.append(","); } buf.append("'").append(subSet.toCanonicalString()).append("'"); } assertEquals("'@2[1~a~b]','@2[2~b~c]','@2[1~a~b],[2~b~c]','@2[3~c~d]','@2[1~a~b],[3~c~d]','@2[2~b~c],[3~c~d]','@2[1~a~b],[2~b~c],[3~c~d]'", buf.toString()); keySet = new CorrelationKeySet(); keySet.add(keyX); keySet.add(keyY); keySet.add(optZ); buf = new StringBuffer(); for( CorrelationKeySet subSet : keySet.findSubSets() ) { if( buf.length() > 0 ) { buf.append(","); } buf.append("'").append(subSet.toCanonicalString()).append("'"); } assertEquals("'@2[1~a~b]','@2[2~b~c]','@2[1~a~b],[2~b~c]','@2[3~c~d]','@2[1~a~b],[3~c~d]','@2[2~b~c],[3~c~d]','@2[1~a~b],[2~b~c],[3~c~d]'", buf.toString()); keySet = new CorrelationKeySet(); keySet.add(keyX); keySet.add(optY); keySet.add(optZ); buf = new StringBuffer(); for( CorrelationKeySet subSet : keySet.findSubSets() ) { if( buf.length() > 0 ) { buf.append(","); } buf.append("'").append(subSet.toCanonicalString()).append("'"); } assertEquals("'@2[1~a~b]','@2[2~b~c]','@2[1~a~b],[2~b~c]','@2[3~c~d]','@2[1~a~b],[3~c~d]','@2[2~b~c],[3~c~d]','@2[1~a~b],[2~b~c],[3~c~d]'", buf.toString()); }