Author: desruisseaux
Date: Wed Feb 20 21:19:02 2013
New Revision: 1448423
URL: http://svn.apache.org/r1448423
Log:
API adjustment and added test cases.
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/RangeSet.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/RangeSet.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/RangeSet.java?rev=1448423&r1=1448422&r2=1448423&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/RangeSet.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/RangeSet.java
Wed Feb 20 21:19:02 2013
@@ -157,9 +157,9 @@ public class RangeSet<E extends Comparab
* The type of elements in the ranges. If the element are numbers,
* then the value is the wrapper type (not the primitive type).
*
- * @see #getElementType()
+ * @see Range#getElementType()
*/
- final Class<E> elementType;
+ protected final Class<E> elementType;
/**
* The primitive type, as one of {@code DOUBLE}, {@code FLOAT}, {@code
LONG}, {@code INTEGER},
@@ -212,7 +212,7 @@ public class RangeSet<E extends Comparab
* @param elementType The type of the range elements.
* @return A new range set for range elements of the given type.
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked","rawtypes"})
public static <E extends Comparable<? super E>> RangeSet<E> create(final
Class<E> elementType) {
ArgumentChecks.ensureNonNull("elementType", elementType);
if (Number.class.isAssignableFrom(elementType)) {
@@ -222,11 +222,11 @@ public class RangeSet<E extends Comparab
}
/**
- * Returns the type of elements in this collection.
+ * Returns the type of elements in this collection, which is always {@code
Range}.
*/
@Override
@SuppressWarnings("unchecked")
- public Class<Range<E>> getElementType() {
+ public final Class<Range<E>> getElementType() {
return (Class) Range.class;
}
@@ -396,7 +396,7 @@ public class RangeSet<E extends Comparab
* is empty}, or delegates to {@link #add(Comparable, Comparable)}
otherwise.</p>
*
* @param range The range to add.
- * @return {@code true} if this set changed (either in size or in values)
as a result of this method call.
+ * @return {@code true} if this set changed as a result of this method
call.
* @throws IllegalArgumentException If the given range uses unsupported
<cite>include</cite> or
* <cite>exclude</cite> attributes.
*/
@@ -417,7 +417,7 @@ public class RangeSet<E extends Comparab
*
* @param minValue The minimal value, inclusive.
* @param maxValue The maximal value, exclusive.
- * @return {@code true} if this set changed (either in size or in values)
as a result of this method call.
+ * @return {@code true} if this set changed as a result of this method
call.
* @throws IllegalArgumentException if {@code minValue} is greater than
{@code maxValue}.
*/
public boolean add(final E minValue, final E maxValue) throws
IllegalArgumentException {
@@ -519,11 +519,13 @@ public class RangeSet<E extends Comparab
* In other words, invoking this method may <strong>increase</strong> the
* {@linkplain #size() size} of this set.
*
- * <p>The default implementation does nothing if the given range
{@linkplain Range#isEmpty()
- * is empty}, or delegates to {@link #remove(Comparable, Comparable)}
otherwise.</p>
+ * <p>The default implementation does nothing if the given object is
{@code null}, or is not an
+ * instance of {@code Range}, or {@linkplain Range#isEmpty() is empty}, or
its element type is
+ * not equals to the element type of the ranges of this set. Otherwise
this method delegates to
+ * {@link #remove(Comparable, Comparable)}.</p>
*
* @param object The range to remove.
- * @return {@code true} if this set changed (either in size or in values)
as a result of this method call.
+ * @return {@code true} if this set changed as a result of this method
call.
* @throws IllegalArgumentException If the given range uses unsupported
<cite>include</cite> or
* <cite>exclude</cite> attributes.
*/
@@ -547,7 +549,7 @@ public class RangeSet<E extends Comparab
*
* @param minValue The minimal value, inclusive.
* @param maxValue The maximal value, exclusive.
- * @return {@code true} if this set changed (either in size or in values)
as a result of this method call.
+ * @return {@code true} if this set changed as a result of this method
call.
* @throws IllegalArgumentException if {@code minValue} is greater than
{@code maxValue}.
*/
public boolean remove(final E minValue, final E maxValue) throws
IllegalArgumentException {
@@ -569,15 +571,13 @@ public class RangeSet<E extends Comparab
* exclusive. The returned values always increase with {@code index}.
*
* @param index The range index, from 0 inclusive to {@link #size() size}
exclusive.
- * @return The minimum value for the range at the specified index.
+ * @return The minimum value for the range at the specified index,
inclusive.
* @throws IndexOutOfBoundsException if {@code index} is out of bounds.
* @throws ClassCastException if range elements are not convertible to
numbers.
*
* @see org.apache.sis.measure.NumberRange#getMinDouble()
*/
- public final double getMinDouble(int index)
- throws IndexOutOfBoundsException, ClassCastException
- {
+ public double getMinDouble(int index) throws IndexOutOfBoundsException,
ClassCastException {
if ((index *= 2) >= length) {
throw new IndexOutOfBoundsException();
}
@@ -590,15 +590,13 @@ public class RangeSet<E extends Comparab
* exclusive. The returned values always increase with {@code index}.
*
* @param index The range index, from 0 inclusive to {@link #size size}
exclusive.
- * @return The maximum value for the range at the specified index.
+ * @return The maximum value for the range at the specified index,
exclusive.
* @throws IndexOutOfBoundsException if {@code index} is out of bounds.
* @throws ClassCastException if range elements are not convertible to
numbers.
*
* @see org.apache.sis.measure.NumberRange#getMaxDouble()
*/
- public final double getMaxDouble(int index)
- throws IndexOutOfBoundsException, ClassCastException
- {
+ public double getMaxDouble(int index) throws IndexOutOfBoundsException,
ClassCastException {
if ((index *= 2) >= length) {
throw new IndexOutOfBoundsException();
}
@@ -621,6 +619,9 @@ public class RangeSet<E extends Comparab
if ((index & 1) == 0) {
return -1;
}
+ } else if ((index & 1) != 0) {
+ // The value is equals to a maximal value, which are exclusives.
+ return -1;
}
index /= 2; // Round toward 0 (odd index are maximum values).
return index;
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java?rev=1448423&r1=1448422&r2=1448423&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
Wed Feb 20 21:19:02 2013
@@ -40,10 +40,22 @@ import static org.apache.sis.test.Assert
@DependsOn(org.apache.sis.measure.RangeTest.class)
public final strictfp class RangeSetTest extends TestCase {
/**
+ * Asserts that the two given values are equals to the expected one.
+ * This method is used for testing {@link RangeSet#first()} and {@link
RangeSet#last()}
+ * in same time than the values from the iterator.
+ */
+ private static void assertEqual(final Range<?> expected,
+ final Range<?> fromIterator, final Range<?> fromGetter)
+ {
+ assertEquals("Value from iterator", expected, fromIterator);
+ assertEquals("Value from getter", expected, fromGetter);
+ }
+
+ /**
* Tests {@link RangeSet#add(Range)} using integer values.
*/
@Test
- public void testIntegers() {
+ public void testRangeOfIntegers() {
final RangeSet<Integer> ranges = RangeSet.create(Integer.class);
assertTrue(ranges.isEmpty());
/*
@@ -88,19 +100,19 @@ public final strictfp class RangeSetTest
* Verify the RangeSet content.
*/
final Iterator<Range<Integer>> it = ranges.iterator();
- assertEquals(NumberRange.create(-20, true, -10, false), it.next());
+ assertEqual (NumberRange.create(-20, true, -10, false), it.next(),
ranges.first());
assertEquals(NumberRange.create( -5, true, 25, false), it.next());
assertEquals(NumberRange.create( 28, true, 35, false), it.next());
assertEquals(NumberRange.create( 40, true, 50, false), it.next());
- assertEquals(NumberRange.create( 60, true, 70, false), it.next());
+ assertEqual (NumberRange.create( 60, true, 70, false), it.next(),
ranges.last());
assertFalse(it.hasNext());
}
/**
- * Tests {@link RangeSet} using date values.
+ * Tests {@link RangeSet#add(Range)} using date values.
*/
@Test
- public void testDates() {
+ public void testRangeOfDates() {
final RangeSet<Date> ranges = RangeSet.create(Date.class);
assertTrue(ranges.isEmpty());
/*
@@ -123,16 +135,16 @@ public final strictfp class RangeSetTest
* Verify the RangeSet content.
*/
final Iterator<Range<Date>> it = ranges.iterator();
- assertEquals(new Range<>(Date.class, lastWeek, true, other, false),
it.next());
- assertEquals(new Range<>(Date.class, yesterday, true, now, false),
it.next());
+ assertEqual(new Range<>(Date.class, lastWeek, true, other, false),
it.next(), ranges.first());
+ assertEqual(new Range<>(Date.class, yesterday, true, now, false),
it.next(), ranges.last());
assertFalse(it.hasNext());
}
/**
- * Tests {@link RangeSet} using string values.
+ * Tests {@link RangeSet#add(Range)} using string values.
*/
@Test
- public void testStrings() {
+ public void testRangeOfStrings() {
final RangeSet<String> ranges = RangeSet.create(String.class);
assertTrue(ranges.isEmpty());
assertTrue(ranges.add("FAA", "FBB"));
@@ -153,12 +165,47 @@ public final strictfp class RangeSetTest
* Verify the RangeSet content.
*/
final Iterator<Range<String>> it = ranges.iterator();
- assertEquals(new Range<>(String.class, "FAA", true, "FCC", false),
it.next());
- assertEquals(new Range<>(String.class, "GAA", true, "GBB", false),
it.next());
+ assertEqual(new Range<>(String.class, "FAA", true, "FCC", false),
it.next(), ranges.first());
+ assertEqual(new Range<>(String.class, "GAA", true, "GBB", false),
it.next(), ranges.last());
assertFalse(it.hasNext());
}
/**
+ * Tests the {@link RangeSet#indexOfRange(Comparable)} method.
+ */
+ @Test
+ public void testIndexOfRange() {
+ final RangeSet<Integer> ranges = RangeSet.create(Integer.class);
+ assertTrue(ranges.add( 40, 50));
+ assertTrue(ranges.add( 28, 35));
+ assertTrue(ranges.add(-20, -10));
+ assertTrue(ranges.add( 60, 70));
+ assertTrue(ranges.add( -5, 25));
+ assertEquals( 0, ranges.indexOfRange(-15));
+ assertEquals( 1, ranges.indexOfRange( 20));
+ assertEquals( 2, ranges.indexOfRange( 28));
+ assertEquals( 3, ranges.indexOfRange( 49));
+ assertEquals( 4, ranges.indexOfRange( 69));
+ assertEquals(-1, ranges.indexOfRange( 70));
+ assertEquals(-1, ranges.indexOfRange( 26));
+ assertEquals(-1, ranges.indexOfRange(-30));
+ }
+
+ /**
+ * Tests {@link RangeSet#clone()}.
+ */
+ @Test
+ public void testClone() {
+ final RangeSet<Integer> ranges = RangeSet.create(Integer.class);
+ assertTrue(ranges.add(-20, -10));
+ assertTrue(ranges.add( 40, 50));
+ final RangeSet<Integer> clone = ranges.clone();
+ assertEquals("The clone shall be equals to the original set.", ranges,
clone);
+ assertTrue(ranges.add(60, 70));
+ assertFalse("Modifying the original set shall not modify the clone.",
ranges.equals(clone));
+ }
+
+ /**
* Tests serialization.
*/
@Test
@@ -178,7 +225,7 @@ public final strictfp class RangeSetTest
* @throws InterruptedException If the test has been interrupted.
*/
@Performance
- public void testPerformance() throws InterruptedException {
+ public void stress() throws InterruptedException {
final Random r = new Random(5638743);
for (int p=0; p<10; p++) {
final long start = System.nanoTime();