From 8e7e4fddec6cc26c67abf8dc10adad2af6eb988a Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 20 Nov 2012 18:06:57 +0200 Subject: Use compare for Comparable when determining equals for filtering (#10310) Change-Id: Ie6e12b1d606d6ed06ae94527427049621b515eed --- .../src/com/vaadin/data/util/filter/Compare.java | 29 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'server/src') diff --git a/server/src/com/vaadin/data/util/filter/Compare.java b/server/src/com/vaadin/data/util/filter/Compare.java index 7730bace6b..a13a5bfaa7 100644 --- a/server/src/com/vaadin/data/util/filter/Compare.java +++ b/server/src/com/vaadin/data/util/filter/Compare.java @@ -47,7 +47,8 @@ public abstract class Compare implements Filter { * A {@link Compare} filter that accepts items for which the identified * property value is equal to value. * - * For in-memory filters, equals() is used for the comparison. For other + * For in-memory filters, {@link Comparable#compareTo(Object)} or, if not + * Comparable, {@link #equals(Object)} is used for the comparison. For other * containers, the comparison implementation is container dependent and may * use e.g. database comparison operations. * @@ -248,8 +249,7 @@ public abstract class Compare implements Filter { Object value = p.getValue(); switch (getOperation()) { case EQUAL: - return (null == this.value) ? (null == value) : this.value - .equals(value); + return compareEquals(value); case GREATER: return compareValue(value) > 0; case LESS: @@ -263,6 +263,29 @@ public abstract class Compare implements Filter { return false; } + /** + * Checks if the this value equals the given value. Favors Comparable over + * equals to better support e.g. BigDecimal where equals is stricter than + * compareTo. + * + * @param otherValue + * The value to compare to + * @return true if the values are equal, false otherwise + */ + private boolean compareEquals(Object otherValue) { + if (value == null || otherValue == null) { + return (otherValue == value); + } else if (value == otherValue) { + return true; + } else if (value instanceof Comparable + && otherValue.getClass() + .isAssignableFrom(getValue().getClass())) { + return ((Comparable) value).compareTo(otherValue) == 0; + } else { + return value.equals(otherValue); + } + } + @SuppressWarnings({ "unchecked", "rawtypes" }) protected int compareValue(Object value1) { if (null == value) { -- cgit v1.2.3