diff options
-rw-r--r-- | server/src/com/vaadin/data/util/filter/Between.java | 44 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java | 61 |
2 files changed, 91 insertions, 14 deletions
diff --git a/server/src/com/vaadin/data/util/filter/Between.java b/server/src/com/vaadin/data/util/filter/Between.java index 48a610ed57..c50488c521 100644 --- a/server/src/com/vaadin/data/util/filter/Between.java +++ b/server/src/com/vaadin/data/util/filter/Between.java @@ -15,16 +15,20 @@ */ package com.vaadin.data.util.filter; +import java.util.Arrays; + import com.vaadin.data.Container.Filter; import com.vaadin.data.Item; +import com.vaadin.shared.util.SharedUtil; public class Between implements Filter { private final Object propertyId; - private final Comparable startValue; - private final Comparable endValue; + private final Comparable<?> startValue; + private final Comparable<?> endValue; - public Between(Object propertyId, Comparable startValue, Comparable endValue) { + public Between(Object propertyId, Comparable<?> startValue, + Comparable<?> endValue) { this.propertyId = propertyId; this.startValue = startValue; this.endValue = endValue; @@ -47,9 +51,11 @@ public class Between implements Filter { throws UnsupportedOperationException { Object value = item.getItemProperty(getPropertyId()).getValue(); if (value instanceof Comparable) { - Comparable cval = (Comparable) value; - return cval.compareTo(getStartValue()) >= 0 - && cval.compareTo(getEndValue()) <= 0; + Comparable comparable = (Comparable) value; + return isAfterStartValue(comparable) + && isBeforeEndValue(comparable); + } else if (value == null) { + return getStartValue() == null && getEndValue() == null; } return false; } @@ -61,8 +67,8 @@ public class Between implements Filter { @Override public int hashCode() { - return getPropertyId().hashCode() + getStartValue().hashCode() - + getEndValue().hashCode(); + return Arrays.hashCode(new Object[] { getPropertyId(), getStartValue(), + getEndValue() }); } @Override @@ -78,13 +84,23 @@ public class Between implements Filter { final Between o = (Between) obj; // Checks the properties one by one - boolean propertyIdEqual = (null != getPropertyId()) ? getPropertyId() - .equals(o.getPropertyId()) : null == o.getPropertyId(); - boolean startValueEqual = (null != getStartValue()) ? getStartValue() - .equals(o.getStartValue()) : null == o.getStartValue(); - boolean endValueEqual = (null != getEndValue()) ? getEndValue().equals( - o.getEndValue()) : null == o.getEndValue(); + boolean propertyIdEqual = SharedUtil.equals(getPropertyId(), + o.getPropertyId()); + boolean startValueEqual = SharedUtil.equals(getStartValue(), + o.getStartValue()); + boolean endValueEqual = SharedUtil.equals(getEndValue(), + o.getEndValue()); return propertyIdEqual && startValueEqual && endValueEqual; } + + private boolean isAfterStartValue(Comparable comparable) { + return getStartValue() == null + || comparable.compareTo(getStartValue()) >= 0; + } + + private boolean isBeforeEndValue(Comparable comparable) { + return getEndValue() == null + || comparable.compareTo(getEndValue()) <= 0; + } } diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java index 16171f67bc..6daf730e25 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java @@ -118,4 +118,65 @@ public class BetweenTest { Between b2 = new Between("bar", 0, 1); Assert.assertFalse(b1.equals(b2)); } + + @Test + public void hashCode_nullStartValue_shouldBeEqual() { + Between b1 = new Between("foo", null, 2); + Between b2 = new Between("foo", null, 2); + Assert.assertEquals(b1.hashCode(), b2.hashCode()); + } + + @Test + public void hashCode_nullEndValue_shouldBeEqual() { + Between b1 = new Between("foo", 0, null); + Between b2 = new Between("foo", 0, null); + Assert.assertEquals(b1.hashCode(), b2.hashCode()); + } + + @Test + public void hashCode_nullPropertyId_shouldBeEqual() { + Between b1 = new Between(null, 0, 2); + Between b2 = new Between(null, 0, 2); + Assert.assertEquals(b1.hashCode(), b2.hashCode()); + } + + @Test + public void passesFilter_nullValue_filterIsPassed() { + String id = "id"; + Between between = new Between(id, null, null); + Assert.assertTrue(between.passesFilter(id, + itemWithPropertyValue(id, null))); + } + + @Test + public void passesFilter_nullStartValue_filterIsPassed() { + String id = "id"; + Between between = new Between(id, null, 2); + Assert.assertTrue(between + .passesFilter(id, itemWithPropertyValue(id, 1))); + } + + @Test + public void passesFilter_nullEndValue_filterIsPassed() { + String id = "id"; + Between between = new Between(id, 0, null); + Assert.assertTrue(between + .passesFilter(id, itemWithPropertyValue(id, 1))); + } + + @Test + public void passesFilter_nullStartValueAndEndValue_filterIsPassed() { + String id = "id"; + Between between = new Between(id, null, null); + Assert.assertTrue(between + .passesFilter(id, itemWithPropertyValue(id, 1))); + } + + @Test + public void passesFilter_nullStartValueAndEndValueAndValueIsNotComparable_filterIsNotPassed() { + String id = "id"; + Between between = new Between(id, null, null); + Assert.assertFalse(between.passesFilter(id, + itemWithPropertyValue(id, new Object()))); + } } |