/** * See "Effective Java" under "Consider Implementing Comparable". * * @param x an object to compare * @param y an object to compare * @param z an object to compare * @param testTransients Whether to include transients in the comparison * @param excludeFields fields to exclude */ private void assertReflectionCompareContract( final Object x, final Object y, final Object z, final boolean testTransients, final String[] excludeFields) { // signum assertTrue( reflectionCompareSignum(x, y, testTransients, excludeFields) == -reflectionCompareSignum(y, x, testTransients, excludeFields)); // transitive if (CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields) > 0 && CompareToBuilder.reflectionCompare(y, z, testTransients, null, excludeFields) > 0) { assertTrue(CompareToBuilder.reflectionCompare(x, z, testTransients, null, excludeFields) > 0); } // un-named if (CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields) == 0) { assertTrue( reflectionCompareSignum(x, z, testTransients, excludeFields) == -reflectionCompareSignum(y, z, testTransients, excludeFields)); } // strongly recommended but not strictly required assertTrue( CompareToBuilder.reflectionCompare(x, y, testTransients) == 0 == EqualsBuilder.reflectionEquals(x, y, testTransients)); }
@Override public int compare(ResultModel o1, ResultModel o2) { CompareToBuilder builder = new CompareToBuilder(); builder.append(o1.getScore(), o2.getScore()); builder.append(o1.getNinerCount(), o2.getNinerCount()); return builder.toComparison() * -1; }
@Override public int compareTo(final Entry<T> o) { final CompareToBuilder b = new CompareToBuilder(); b.append(-this.count, -o.count); b.append(this.value, o.value); return b.toComparison(); }
private void testReflectionHierarchyCompare( final boolean testTransients, final String[] excludeFields) { final TestObject to1 = new TestObject(1); final TestObject to2 = new TestObject(2); final TestObject to3 = new TestObject(3); final TestSubObject tso1 = new TestSubObject(1, 1); final TestSubObject tso2 = new TestSubObject(2, 2); final TestSubObject tso3 = new TestSubObject(3, 3); assertReflectionCompareContract(to1, to1, to1, false, excludeFields); assertReflectionCompareContract(to1, to2, to3, false, excludeFields); assertReflectionCompareContract(tso1, tso1, tso1, false, excludeFields); assertReflectionCompareContract(tso1, tso2, tso3, false, excludeFields); assertReflectionCompareContract("1", "2", "3", false, excludeFields); assertTrue( 0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(1, 0), testTransients)); assertTrue( 0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(0, 1), testTransients)); // root class assertXYZCompareOrder(to1, to2, to3, true, null); // subclass assertXYZCompareOrder(tso1, tso2, tso3, true, null); }
@Override public int compare(RowKey key1, RowKey key2) { CompareToBuilder builder = new CompareToBuilder(); for (String name : sortFields) { builder.append(key1.getColumnValue(name), key2.getColumnValue(name)); } return builder.toComparison(); }
@Test public void testReflectionCompare() { final TestObject o1 = new TestObject(4); final TestObject o2 = new TestObject(4); assertTrue(CompareToBuilder.reflectionCompare(o1, o1) == 0); assertTrue(CompareToBuilder.reflectionCompare(o1, o2) == 0); o2.setA(5); assertTrue(CompareToBuilder.reflectionCompare(o1, o2) < 0); assertTrue(CompareToBuilder.reflectionCompare(o2, o1) > 0); }
/* (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override public int compareTo(RoutingProcessorConfiguration o) { CompareToBuilder builder = (new CompareToBuilder()) .append(getRoutingInstances().size(), o.getRoutingInstances().size()); if (builder.toComparison() == 0) { Iterator<RoutingInstanceConfiguration> lit = getRoutingInstances().iterator(); Iterator<RoutingInstanceConfiguration> rit = o.getRoutingInstances().iterator(); while (lit.hasNext()) builder.append(lit.next(), rit.next()); } return builder.toComparison(); }
private static void reflectionAppend( Object obj, Object obj1, Class class1, CompareToBuilder comparetobuilder, boolean flag, String as[]) { Field afield[] = class1.getDeclaredFields(); AccessibleObject.setAccessible(afield, true); int i = 0; do { if (i >= afield.length || comparetobuilder.comparison != 0) { return; } Field field = afield[i]; if (!ArrayUtils.contains(as, field.getName()) && field.getName().indexOf('$') == -1 && (flag || !Modifier.isTransient(field.getModifiers())) && !Modifier.isStatic(field.getModifiers())) { try { comparetobuilder.append(field.get(obj), field.get(obj1)); } catch (IllegalAccessException illegalaccessexception) { throw new InternalError("Unexpected IllegalAccessException"); } } i++; } while (true); }
/** * Returns the signum of the result of comparing x and y with <code> * CompareToBuilder.reflectionCompare</code> * * @param lhs The "left-hand-side" of the comparison. * @param rhs The "right-hand-side" of the comparison. * @param testTransients Whether to include transients in the comparison * @param excludeFields fields to exclude * @return int The signum */ private int reflectionCompareSignum( final Object lhs, final Object rhs, final boolean testTransients, final String[] excludeFields) { return BigInteger.valueOf(CompareToBuilder.reflectionCompare(lhs, rhs, testTransients)) .signum(); }
private void assertXYZCompareOrder( final Object x, final Object y, final Object z, final boolean testTransients, final String[] excludeFields) { assertTrue(0 == CompareToBuilder.reflectionCompare(x, x, testTransients, null, excludeFields)); assertTrue(0 == CompareToBuilder.reflectionCompare(y, y, testTransients, null, excludeFields)); assertTrue(0 == CompareToBuilder.reflectionCompare(z, z, testTransients, null, excludeFields)); assertTrue(0 > CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields)); assertTrue(0 > CompareToBuilder.reflectionCompare(x, z, testTransients, null, excludeFields)); assertTrue(0 > CompareToBuilder.reflectionCompare(y, z, testTransients, null, excludeFields)); assertTrue(0 < CompareToBuilder.reflectionCompare(y, x, testTransients, null, excludeFields)); assertTrue(0 < CompareToBuilder.reflectionCompare(z, x, testTransients, null, excludeFields)); assertTrue(0 < CompareToBuilder.reflectionCompare(z, y, testTransients, null, excludeFields)); }
public static int reflectionCompare( Object obj, Object obj1, boolean flag, Class class1, String as[]) { if (obj == obj1) { return 0; } if (obj == null || obj1 == null) { throw new NullPointerException(); } Class class2 = obj.getClass(); if (!class2.isInstance(obj1)) { throw new ClassCastException(); } CompareToBuilder comparetobuilder = new CompareToBuilder(); reflectionAppend(obj, obj1, class2, comparetobuilder, flag, as); do { if (class2.getSuperclass() == null || class2 == class1) { return comparetobuilder.toComparison(); } class2 = class2.getSuperclass(); reflectionAppend(obj, obj1, class2, comparetobuilder, flag, as); } while (true); }
public int compareTo(Object o) { return CompareToBuilder.reflectionCompare(this, o); }
@Test(expected = ClassCastException.class) public void testReflectionCompareEx2() { final TestObject o1 = new TestObject(4); final Object o2 = new Object(); CompareToBuilder.reflectionCompare(o1, o2); }
@Test(expected = NullPointerException.class) public void testReflectionCompareEx1() { final TestObject o1 = new TestObject(4); CompareToBuilder.reflectionCompare(o1, null); }