@Test public void testCompareValues() { int i1 = 1; int i2 = SupportConst.I_TWO; double d1 = 1.0; double d2 = TWO_DOUBLE; String s1 = "aaaa"; String s2 = "b"; Assert.assertEquals(-1, MultiKeyComparator.compareValues(i1, i2, SortEnum.ASC)); Assert.assertEquals(1, MultiKeyComparator.compareValues(i1, i2, SortEnum.DESC)); Assert.assertEquals(1, MultiKeyComparator.compareValues(i1, null, SortEnum.ASC)); Assert.assertEquals(-1, MultiKeyComparator.compareValues(i1, null, SortEnum.DESC)); Assert.assertEquals(-1, MultiKeyComparator.compareValues(s1, s2, SortEnum.ASC)); Assert.assertEquals(1, MultiKeyComparator.compareValues(s1, s2, SortEnum.DESC)); Assert.assertEquals(1, MultiKeyComparator.compareValues(s1, null, SortEnum.ASC)); Assert.assertEquals(-1, MultiKeyComparator.compareValues(s1, null, SortEnum.DESC)); Assert.assertEquals(-1, MultiKeyComparator.compareValues(d1, d2, SortEnum.ASC)); Assert.assertEquals(1, MultiKeyComparator.compareValues(d1, d2, SortEnum.DESC)); Assert.assertEquals(1, MultiKeyComparator.compareValues(d1, null, SortEnum.ASC)); Assert.assertEquals(-1, MultiKeyComparator.compareValues(d1, null, SortEnum.DESC)); try { MultiKeyComparator.compareValues(i1, s1, SortEnum.ASC); fail(); } catch (ClassCastException e) { assertTrue(true); } }
@SuppressWarnings("unchecked") protected static int compareValues(Object valueOne, Object valueTwo, SortEnum sortEnum) { if (valueOne == null || valueTwo == null) { if (valueOne == null && valueTwo == null) { return 0; } if (valueOne == null) { if (sortEnum == SortEnum.ASC) { return -1; } return 1; } if (sortEnum == SortEnum.ASC) { return 1; } return -1; } Comparable<Object> comparable1; if (valueOne instanceof Comparable) { comparable1 = (Comparable<Object>)valueOne; } else { throw new ClassCastException("Cannot sort objects of type " + valueOne.getClass()); } if (sortEnum == SortEnum.ASC) { return comparable1.compareTo(valueTwo); } return -1 * comparable1.compareTo(valueTwo); }
MultiKeyComparator implements Comparator<MultiKey>, Serializable { @SuppressWarnings("unchecked") protected static int compareValues(Object valueOne, Object valueTwo, SortEnum sortEnum) { if (valueOne == null || valueTwo == null) { if (valueOne == null && valueTwo == null) { return 0; } if (valueOne == null) { if (sortEnum == SortEnum.ASC) { return -1; } return 1; } if (sortEnum == SortEnum.ASC) { return 1; } return -1; } Comparable<Object> comparable1; if (valueOne instanceof Comparable) { comparable1 = (Comparable<Object>)valueOne; } else { throw new ClassCastException("Cannot sort objects of type " + valueOne.getClass()); } if (sortEnum == SortEnum.ASC) { return comparable1.compareTo(valueTwo); } return -1 * comparable1.compareTo(valueTwo); } }
MultiKeyComparator implements Comparator<MultiKey>, Serializable { @SuppressWarnings("unchecked") protected static int compareValues(Object valueOne, Object valueTwo, SortEnum sortEnum) { if (valueOne == null || valueTwo == null) { if (valueOne == null && valueTwo == null) { return 0; } if (valueOne == null) { if (sortEnum == SortEnum.ASC) { return -1; } return 1; } if (sortEnum == SortEnum.ASC) { return 1; } return -1; } Comparable<Object> comparable1; if (valueOne instanceof Comparable) { comparable1 = (Comparable<Object>)valueOne; } else { throw new ClassCastException("Cannot sort objects of type " + valueOne.getClass()); } if (sortEnum == SortEnum.ASC) { return comparable1.compareTo(valueTwo); } return -1 * comparable1.compareTo(valueTwo); } MultiKeyComparator(List<SortCondition> sortConditions); }
MultiKeyComparator implements Comparator<MultiKey>, Serializable { @SuppressWarnings("unchecked") protected static int compareValues(Object valueOne, Object valueTwo, SortEnum sortEnum) { if (valueOne == null || valueTwo == null) { if (valueOne == null && valueTwo == null) { return 0; } if (valueOne == null) { if (sortEnum == SortEnum.ASC) { return -1; } return 1; } if (sortEnum == SortEnum.ASC) { return 1; } return -1; } Comparable<Object> comparable1; if (valueOne instanceof Comparable) { comparable1 = (Comparable<Object>)valueOne; } else { throw new ClassCastException("Cannot sort objects of type " + valueOne.getClass()); } if (sortEnum == SortEnum.ASC) { return comparable1.compareTo(valueTwo); } return -1 * comparable1.compareTo(valueTwo); } MultiKeyComparator(List<SortCondition> sortConditions); @Override int compare(MultiKey first, MultiKey second); }
MultiKeyComparator implements Comparator<MultiKey>, Serializable { @SuppressWarnings("unchecked") protected static int compareValues(Object valueOne, Object valueTwo, SortEnum sortEnum) { if (valueOne == null || valueTwo == null) { if (valueOne == null && valueTwo == null) { return 0; } if (valueOne == null) { if (sortEnum == SortEnum.ASC) { return -1; } return 1; } if (sortEnum == SortEnum.ASC) { return 1; } return -1; } Comparable<Object> comparable1; if (valueOne instanceof Comparable) { comparable1 = (Comparable<Object>)valueOne; } else { throw new ClassCastException("Cannot sort objects of type " + valueOne.getClass()); } if (sortEnum == SortEnum.ASC) { return comparable1.compareTo(valueTwo); } return -1 * comparable1.compareTo(valueTwo); } MultiKeyComparator(List<SortCondition> sortConditions); @Override int compare(MultiKey first, MultiKey second);  }