BitField implements Comparable<BitField> { @Override public int compareTo(BitField o) { int common = Math.min(bits.length, o.bits.length); int cmp = WritableComparator.compareBytes(bits, 0, common, o.bits, 0, common); if (cmp != 0) { return cmp; } boolean allZeros = true; byte[] longestArray = o.bits; if (bits.length > common) { longestArray = bits; } for (int i = common; i < longestArray.length; i++) { if (longestArray[i] != 0) { allZeros = false; break; } } if (allZeros) { return 0; } else { return bits.length - o.bits.length; } } BitField(); void set(int bit, boolean value); void set(int bit); void unset(int bit); boolean isSet(int bit); void ser(DataOutput out); int deser(DataInput in); int deser(byte[] bytes, int start); @Override int compareTo(BitField o); void clear(); String toString();  }
@Test public void testCompareTo() { BitField b1 = new BitField(), b2 = new BitField(); assertTrue(b1.compareTo(b2) == 0); b1.set(0); assertTrue(b1.compareTo(b2) > 0); b2.set(0); assertTrue(b1.compareTo(b2) == 0); b2.set(1); assertTrue(b1.compareTo(b2) < 0); b2.unset(1); b2.set(100); b2.unset(100); assertTrue(b1.compareTo(b2) == 0); b1.set(8); b2.set(8); assertTrue(b1.compareTo(b2) == 0); for(int i=0; i<100; i++) { b1.set(i); b2.set(i); } assertTrue(b1.compareTo(b2) == 0); b1.set(1000); assertTrue(b1.compareTo(b2) > 0); b2.set(1000); assertTrue(b1.compareTo(b2) == 0); b2.set(1001); assertTrue(b1.compareTo(b2) < 0); }