From 69e0aac03c38f6cef4ef68f86b9450b5935c99a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Fri, 22 Nov 2013 15:28:09 +0200 Subject: [PATCH] Unify empty ranges to always have a defined position (#12645) Change-Id: Iaeef125159df878d3b49b1a588c19b5d1b479b76 --- .../client/data/AbstractRemoteDataSource.java | 2 +- .../src/com/vaadin/client/ui/grid/Range.java | 24 +++++------- .../com/vaadin/client/ui/grid/RangeTest.java | 38 +++++++++++++++++-- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java b/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java index 5790adada9..bda923ef6f 100644 --- a/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java +++ b/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java @@ -99,7 +99,7 @@ public abstract class AbstractRemoteDataSource implements DataSource { Profiler.enter("AbstractRemoteDataSource.checkCacheCoverage"); - if (!requestedAvailability.intersects(cached)) { + if (!requestedAvailability.intersects(cached) || cached.isEmpty()) { /* * Simple case: no overlap between cached data and needed data. * Clear the cache and request new data diff --git a/client/src/com/vaadin/client/ui/grid/Range.java b/client/src/com/vaadin/client/ui/grid/Range.java index d3ae3c3753..634a182421 100644 --- a/client/src/com/vaadin/client/ui/grid/Range.java +++ b/client/src/com/vaadin/client/ui/grid/Range.java @@ -44,7 +44,7 @@ public final class Range { } /** - * Creates a range of all integers between two integers. + * Creates a range between two integers. *

* The range start is inclusive and the end is exclusive. * So, a range "between" 0 and 5 represents the numbers 0, 1, 2, 3 and 4, @@ -70,7 +70,7 @@ public final class Range { * the first integer to include in the range * @param length * the length of the resulting range - * @return a range of all the integers from start, with + * @return a range starting from start, with * length number of integers following * @throws IllegalArgumentException * if length < 0 @@ -110,7 +110,7 @@ public final class Range { } /** - * Returns the inclusivestart point of this range. + * Returns the inclusive start point of this range. * * @return the start point of this range */ @@ -146,21 +146,15 @@ public final class Range { } /** - * Checks whether this range and another range shares integers. - *

- * An empty range never intersects with any other range. + * Checks whether this range and another range are at least partially + * covering the same values. * * @param other * the other range to check against - * @return true if this and other have any - * integers in common + * @return true if this and other intersect */ public boolean intersects(final Range other) { - if (!isEmpty() && !other.isEmpty()) { - return getStart() < other.getEnd() && other.getStart() < getEnd(); - } else { - return false; - } + return getStart() < other.getEnd() && other.getStart() < getEnd(); } /** @@ -328,9 +322,9 @@ public final class Range { */ public Range[] splitAt(final int integer) { if (integer < start) { - return new Range[] { Range.withLength(integer, 0), this }; + return new Range[] { Range.withLength(start, 0), this }; } else if (integer >= end) { - return new Range[] { this, Range.withLength(integer, 0) }; + return new Range[] { this, Range.withLength(end, 0) }; } else { return new Range[] { new Range(start, integer), new Range(integer, end) }; diff --git a/client/tests/src/com/vaadin/client/ui/grid/RangeTest.java b/client/tests/src/com/vaadin/client/ui/grid/RangeTest.java index 4441ee901d..d73b0fb02f 100644 --- a/client/tests/src/com/vaadin/client/ui/grid/RangeTest.java +++ b/client/tests/src/com/vaadin/client/ui/grid/RangeTest.java @@ -112,6 +112,24 @@ public class RangeTest { splitRanges[1]); } + @Test + public void split_valueBefore() { + Range range = Range.between(10, 20); + Range[] splitRanges = range.splitAt(5); + + assertEquals(Range.between(10, 10), splitRanges[0]); + assertEquals(range, splitRanges[1]); + } + + @Test + public void split_valueAfter() { + Range range = Range.between(10, 20); + Range[] splitRanges = range.splitAt(25); + + assertEquals(range, splitRanges[0]); + assertEquals(Range.between(20, 20), splitRanges[1]); + } + @Test public void emptySplitTest() { final Range range = Range.between(5, 10); @@ -137,10 +155,22 @@ public class RangeTest { .intersects(Range.between(5, 15))); assertTrue("[0..10[ does not intersect [10..20[", !Range.between(0, 10) .intersects(Range.between(10, 20))); - assertTrue("empty range does not intersect with [0..10[", !Range - .between(5, 5).intersects(Range.between(0, 10))); - assertTrue("[0..10[ does not intersect with empty range", !Range - .between(0, 10).intersects(Range.between(5, 5))); + } + + @Test + public void intersects_emptyInside() { + assertTrue("[5..5[ does intersect with [0..10[", Range.between(5, 5) + .intersects(Range.between(0, 10))); + assertTrue("[0..10[ does intersect with [5..5[", Range.between(0, 10) + .intersects(Range.between(5, 5))); + } + + @Test + public void intersects_emptyOutside() { + assertTrue("[15..15[ does not intersect with [0..10[", + !Range.between(15, 15).intersects(Range.between(0, 10))); + assertTrue("[0..10[ does not intersect with [15..15[", + !Range.between(0, 10).intersects(Range.between(15, 15))); } @Test -- 2.39.5