/** * <p>Compares the pair based on the left element followed by the right element. * The types must be {@code Comparable}.</p> * * @param other the other pair, not null * @return negative if this is less, zero if equal, positive if greater */ @Override public int compareTo(final Pair<L, R> other) { return new CompareToBuilder().append(getLeft(), other.getLeft()) .append(getRight(), other.getRight()).toComparison(); }
/** * <p>Compares two <code>Object</code>s via reflection.</p> * * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code> * is used to bypass normal access control checks. This will fail under a * security manager unless the appropriate permissions are set.</p> * * <ul> * <li>Static fields will not be compared</li> * <li>Transient members will be not be compared, as they are likely derived * fields</li> * <li>Superclass fields will be compared</li> * </ul> * * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>, * they are considered equal.</p> * * @param lhs left-hand object * @param rhs right-hand object * @return a negative integer, zero, or a positive integer as <code>lhs</code> * is less than, equal to, or greater than <code>rhs</code> * @throws NullPointerException if either (but not both) parameters are * <code>null</code> * @throws ClassCastException if <code>rhs</code> is not assignment-compatible * with <code>lhs</code> */ public static int reflectionCompare(final Object lhs, final Object rhs) { return reflectionCompare(lhs, rhs, false, null); }
@Test public void testAppendSuper() { final TestObject o1 = new TestObject(4); final TestObject o2 = new TestObject(5); assertTrue(new CompareToBuilder().appendSuper(0).append(o1, o1).toComparison() == 0); assertTrue(new CompareToBuilder().appendSuper(0).append(o1, o2).toComparison() < 0); assertTrue(new CompareToBuilder().appendSuper(0).append(o2, o1).toComparison() > 0); assertTrue(new CompareToBuilder().appendSuper(-1).append(o1, o1).toComparison() < 0); assertTrue(new CompareToBuilder().appendSuper(-1).append(o1, o2).toComparison() < 0); assertTrue(new CompareToBuilder().appendSuper(1).append(o1, o1).toComparison() > 0); assertTrue(new CompareToBuilder().appendSuper(1).append(o1, o2).toComparison() > 0); }
@Test public void testObjectBuild() { final TestObject o1 = new TestObject(4); final TestObject o2 = new TestObject(4); assertEquals(Integer.valueOf(0), new CompareToBuilder().append(o1, o1).build()); assertEquals(Integer.valueOf(0), new CompareToBuilder().append(o1, o2).build()); o2.setA(5); assertTrue(new CompareToBuilder().append(o1, o2).build().intValue() < 0); assertTrue(new CompareToBuilder().append(o2, o1).build().intValue() > 0); assertTrue(new CompareToBuilder().append(o1, null).build().intValue() > 0); assertEquals(Integer.valueOf(0), new CompareToBuilder().append((Object) null, null).build()); assertTrue(new CompareToBuilder().append(null, o1).build().intValue() < 0); }
@Test(expected=ClassCastException.class) public void testObjectEx2() { final TestObject o1 = new TestObject(4); final Object o2 = new Object(); new CompareToBuilder().append(o1, o2); }
/** * <p>Appends to the <code>builder</code> the comparison of * two <code>Object</code>s.</p> * * <ol> * <li>Check if <code>lhs == rhs</code></li> * <li>Check if either <code>lhs</code> or <code>rhs</code> is <code>null</code>, * a <code>null</code> object is less than a non-<code>null</code> object</li> * <li>Check the object contents</li> * </ol> * * <p><code>lhs</code> must either be an array or implement {@link Comparable}.</p> * * @param lhs left-hand object * @param rhs right-hand object * @return this - used to chain append calls * @throws ClassCastException if <code>rhs</code> is not assignment-compatible * with <code>lhs</code> */ public CompareToBuilder append(final Object lhs, final Object rhs) { return append(lhs, rhs, null); }
/** * Returns a negative Integer, a positive Integer, or zero as * the <code>builder</code> has judged the "left-hand" side * as less than, greater than, or equal to the "right-hand" * side. * * @return final comparison result as an Integer * @see #toComparison() * @since 3.0 */ @Override public Integer build() { return Integer.valueOf(toComparison()); } }
@Override public int compareTo(Matched that) { CompareToBuilder compareToBuilder = new CompareToBuilder(); return compareToBuilder.append(this.topic, that.topic) .append(this.partition, that.partition) .append(this.offset, that.offset) .append(this.stageName, that.stageName) .append(this.stageType, that.stageType) .build(); }
/** * <p>Appends to the <code>builder</code> the deep comparison of * two <code>Object</code> arrays.</p> * * <ol> * <li>Check if arrays are the same using <code>==</code></li> * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li> * <li>Check array length, a short length array is less than a long length array</li> * <li>Check array contents element by element using {@link #append(Object, Object, Comparator)}</li> * </ol> * * <p>This method will also will be called for the top level of multi-dimensional, * ragged, and multi-typed arrays.</p> * * @param lhs left-hand array * @param rhs right-hand array * @return this - used to chain append calls * @throws ClassCastException if <code>rhs</code> is not assignment-compatible * with <code>lhs</code> */ public CompareToBuilder append(final Object[] lhs, final Object[] rhs) { return append(lhs, rhs, null); }
@Override public int compareTo(EmployeeWeekendSequence other) { return new CompareToBuilder() .append(employee, other.employee) .append(firstSundayIndex, other.firstSundayIndex) .append(lastSundayIndex, other.lastSundayIndex) .toComparison(); }
@Override public int compareTo(final Runned that) { CompareToBuilder compareToBuilder = new CompareToBuilder(); return compareToBuilder.append(this.key, that.key).build(); }
private void appendArray(final Object lhs, final Object rhs, final Comparator<?> comparator) { // switch on type of array, to dispatch to the correct handler // handles multi dimensional arrays // throws a ClassCastException if rhs is not the correct array type if (lhs instanceof long[]) { append((long[]) lhs, (long[]) rhs); } else if (lhs instanceof int[]) { append((int[]) lhs, (int[]) rhs); } else if (lhs instanceof short[]) { append((short[]) lhs, (short[]) rhs); } else if (lhs instanceof char[]) { append((char[]) lhs, (char[]) rhs); } else if (lhs instanceof byte[]) { append((byte[]) lhs, (byte[]) rhs); } else if (lhs instanceof double[]) { append((double[]) lhs, (double[]) rhs); } else if (lhs instanceof float[]) { append((float[]) lhs, (float[]) rhs); } else if (lhs instanceof boolean[]) { append((boolean[]) lhs, (boolean[]) rhs); } else { // not an array of primitives // throws a ClassCastException if rhs is not an array append((Object[]) lhs, (Object[]) rhs, comparator); } }
return reflectionCompare(lhs, rhs, compareTransients, null);
public int compareTo(PeriodWrapper other) { return new CompareToBuilder() .append(period, other.period) .toComparison(); }
return reflectionCompare(lhs, rhs, false, null, excludeFields);
@Override public int compareTo(EmployeeConsecutiveAssignmentEnd other) { return new CompareToBuilder() .append(employee, other.employee) .append(shiftDate, other.shiftDate) .toComparison(); }
@Override public int compareTo(final OneTimeTokenAccount o) { return new CompareToBuilder() .append(this.scratchCodes.toArray(), o.getScratchCodes().toArray()) .append(this.validationCode, o.getValidationCode()) .append(this.secretKey, o.getSecretKey()) .append(this.username, o.getUsername()).build(); }
return reflectionCompare(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields));
@Override public int compareTo(EmployeeConsecutiveWeekendAssignmentStart other) { return new CompareToBuilder() .append(employee, other.employee) .append(sundayIndex, other.sundayIndex) .toComparison(); }