MultiKeyComparator implements Comparator<MultiKey>, Serializable { @Override public int compare(MultiKey first, MultiKey second) { if (first.size() != sortConditions.size() || second.size() != sortConditions.size()) { String msg = "Illegal Key size for comparison."; LOG.error(msg); throw new IllegalArgumentException(msg); } for (int i = 0; i < first.size(); i++) { Object valueOne = first.get(i); Object valueTwo = second.get(i); SortEnum sortEnum = sortConditions.get(i).getSortEnum(); int comparisonResult = compareValues(valueOne, valueTwo, sortEnum); if (comparisonResult != 0) { return comparisonResult; } } return 0; } MultiKeyComparator(List<SortCondition> sortConditions); @Override int compare(MultiKey first, MultiKey second);  }
@Test public void testCompare() { Object[] obj1 = new Object[] {1, 1.0, "aaa"}; Object[] obj2 = new Object[] {SupportConst.I_TWO, 1.0, "aaa"}; Object[] obj3 = new Object[] {1, TWO_DOUBLE, "aaa"}; Object[] obj4 = new Object[] {1, 1.0, "ab"}; Object[] obj5 = new Object[] {SupportConst.I_TWO, TWO_DOUBLE, "aaa"}; MultiKey m1 = new MultiKey(obj1); MultiKey m2 = new MultiKey(obj2); MultiKey m3 = new MultiKey(obj3); MultiKey m4 = new MultiKey(obj4); MultiKey m5 = new MultiKey(obj5); sclist.add(new SortCondition("i", SortEnum.ASC)); sclist.add(new SortCondition("d", SortEnum.ASC)); sclist.add(new SortCondition("s", SortEnum.ASC)); mc = new MultiKeyComparator(sclist); Assert.assertEquals(-1, mc.compare(m1, m2)); Assert.assertEquals(-1, mc.compare(m1, m3)); Assert.assertEquals(-1, mc.compare(m1, m4)); Assert.assertEquals(1, mc.compare(m2, m4)); sclist.clear(); sclist.add(new SortCondition("i", SortEnum.ASC)); sclist.add(new SortCondition("d", SortEnum.DESC)); sclist.add(new SortCondition("s", SortEnum.ASC)); mc = new MultiKeyComparator(sclist); Assert.assertEquals(-1, mc.compare(m1, m2)); Assert.assertEquals(1, mc.compare(m1, m3)); Assert.assertEquals(-1, mc.compare(m1, m4)); Assert.assertEquals(1, mc.compare(m2, m5)); sclist.clear(); sclist.add(new SortCondition("i", SortEnum.ASC)); sclist.add(new SortCondition("d", SortEnum.DESC)); mc = new MultiKeyComparator(sclist); try { mc.compare(m1, m2); fail(); } catch (IllegalArgumentException e) { assertTrue(true); } }