GramKeyGroupComparator extends WritableComparator { @SuppressWarnings("unchecked") @Override public int compare(WritableComparable a, WritableComparable b) { GramKey gka = (GramKey) a; GramKey gkb = (GramKey) b; return WritableComparator.compareBytes(gka.getBytes(), 0, gka.getPrimaryLength(), gkb.getBytes(), 0, gkb.getPrimaryLength()); } protected  GramKeyGroupComparator(); @SuppressWarnings("unchecked") @Override int compare(WritableComparable a, WritableComparable b);  }
@Test public void testComparator() { byte[] foo = new byte[1]; foo[0] = (byte) 1; byte[] empty = new byte[0]; GramKey a = new GramKey(new Gram("foo", 1, Gram.Type.HEAD), empty); GramKey b = new GramKey(new Gram("foo", 1, Gram.Type.HEAD), foo); GramKey c = new GramKey(new Gram("foo", 2, Gram.Type.HEAD), empty); GramKey d = new GramKey(new Gram("foo", 1, Gram.Type.TAIL), empty); GramKey e = new GramKey(new Gram("bar", 5, Gram.Type.HEAD), empty); GramKeyGroupComparator cmp = new GramKeyGroupComparator(); Assert.assertEquals(0, cmp.compare(a, b)); Assert.assertEquals(0, cmp.compare(a, c)); Assert.assertTrue(0 > cmp.compare(a, d)); Assert.assertTrue(0 < cmp.compare(a, e)); Assert.assertTrue(0 < cmp.compare(d, e)); }