BitSets { public static SortedMap<Integer, BitSet> closure( SortedMap<Integer, BitSet> equivalence) { if (equivalence.isEmpty()) { return ImmutableSortedMap.of(); } int length = equivalence.lastKey(); for (BitSet bitSet : equivalence.values()) { length = Math.max(length, bitSet.length()); } if (equivalence.size() < length || equivalence.firstKey() != 0) { SortedMap<Integer, BitSet> old = equivalence; equivalence = new TreeMap<>(); for (int i = 0; i < length; i++) { final BitSet bitSet = old.get(i); equivalence.put(i, bitSet == null ? new BitSet() : bitSet); } } final Closure closure = new Closure(equivalence); return closure.closure; } private  BitSets(); static boolean contains(BitSet set0, BitSet set1); static boolean contains(BitSet set0, ImmutableBitSet set1); static Iterable<Integer> toIter(final BitSet bitSet); static Iterable<Integer> toIter(final ImmutableBitSet bitSet); static IntList toList(final BitSet bitSet); static int[] toArray(final BitSet bitSet); static BitSet of(int... bits); static BitSet of(Integer[] bits); static BitSet of(Iterable<? extends Number> bits); static BitSet of(ImmutableIntList bits); static BitSet range(int fromIndex, int toIndex); static BitSet range(int toIndex); static void setAll(BitSet bitSet, Iterable<? extends Number> list); static BitSet union(BitSet set0, BitSet... sets); static int previousClearBit(BitSet bitSet, int fromIndex); static SortedMap<Integer, BitSet> closure(
      SortedMap<Integer, BitSet> equivalence); static void populate(BitSet bitSet, Iterable<? extends Number> list); static void populate(BitSet bitSet, ImmutableIntList list);  }
@Test public void testClosure() { final SortedMap<Integer, BitSet> empty = Maps.newTreeMap(); assertThat(BitSets.closure(empty), equalTo(empty)); final SortedMap<Integer, BitSet> map = Maps.newTreeMap(); map.put(0, BitSets.of(3)); map.put(1, BitSets.of()); map.put(2, BitSets.of(7)); map.put(3, BitSets.of(4, 12)); map.put(4, BitSets.of()); map.put(5, BitSets.of()); map.put(6, BitSets.of()); map.put(7, BitSets.of()); map.put(8, BitSets.of()); map.put(9, BitSets.of()); map.put(10, BitSets.of()); map.put(11, BitSets.of()); map.put(12, BitSets.of()); final String original = map.toString(); final String expected = "{0={3, 4, 12}, 1={}, 2={7}, 3={3, 4, 12}, 4={4, 12}, 5={}, 6={}, 7={7}, 8={}, 9={}, 10={}, 11={}, 12={4, 12}}"; assertThat(BitSets.closure(map).toString(), equalTo(expected)); assertThat("argument modified", map.toString(), equalTo(original)); final SortedMap<Integer, BitSet> map2 = Maps.newTreeMap(); map2.put(0, BitSets.of(3)); map2.put(2, BitSets.of(7)); map2.put(3, BitSets.of(4, 12)); map2.put(9, BitSets.of()); final String original2 = map2.toString(); assertThat(BitSets.closure(map2).toString(), equalTo(expected)); assertThat("argument modified", map2.toString(), equalTo(original2)); }