]> source.dussan.org Git - vaadin-framework.git/commitdiff
Unify empty ranges to always have a defined position (#12645)
authorLeif Åstrand <leif@vaadin.com>
Fri, 22 Nov 2013 13:28:09 +0000 (15:28 +0200)
committerVaadin Code Review <review@vaadin.com>
Fri, 22 Nov 2013 13:45:59 +0000 (13:45 +0000)
Change-Id: Iaeef125159df878d3b49b1a588c19b5d1b479b76

client/src/com/vaadin/client/data/AbstractRemoteDataSource.java
client/src/com/vaadin/client/ui/grid/Range.java
client/tests/src/com/vaadin/client/ui/grid/RangeTest.java

index 5790adada95d2f6642e20c61d72d2d21547d4de0..bda923ef6fdd18285ab022132792e1225acb71e4 100644 (file)
@@ -99,7 +99,7 @@ public abstract class AbstractRemoteDataSource<T> implements DataSource<T> {
 
         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
index d3ae3c37535ebdbcd1724304db557578ce5ea820..634a182421d43aa8f12176cd193ff4fb3fbde2ae 100644 (file)
@@ -44,7 +44,7 @@ public final class Range {
     }
 
     /**
-     * Creates a range of all integers between two integers.
+     * Creates a range between two integers.
      * <p>
      * The range start is <em>inclusive</em> and the end is <em>exclusive</em>.
      * 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 <code>start</code>, with
+     * @return a range starting from <code>start</code>, with
      *         <code>length</code> number of integers following
      * @throws IllegalArgumentException
      *             if length &lt; 0
@@ -110,7 +110,7 @@ public final class Range {
     }
 
     /**
-     * Returns the <em>inclusive</em>start point of this range.
+     * Returns the <em>inclusive</em> 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.
-     * <p>
-     * 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 <code>true</code> if this and <code>other</code> have any
-     *         integers in common
+     * @return <code>true</code> if this and <code>other</code> 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) };
index 4441ee901d3637019b0643b3e8e92464a59907ac..d73b0fb02fad7eb0c39248fa3d4fbf835c6301a9 100644 (file)
@@ -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