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
}
/**
- * 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,
* 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 < 0
}
/**
- * 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
*/
}
/**
- * 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();
}
/**
*/
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) };
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);
.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