Details
-
New Feature
-
Status: Closed
-
Trivial
-
Resolution: Fixed
-
None
-
None
-
None
Description
I've found it useful to use the range class to create an int[] or long[] array of a sequence on numbers.
Index: src/test/org/apache/commons/lang/math/IntRangeTest.java
===================================================================
— src/test/org/apache/commons/lang/math/IntRangeTest.java (revision 506779)
+++ src/test/org/apache/commons/lang/math/IntRangeTest.java (working copy)
@@ -21,6 +21,8 @@
import junit.framework.Test;
import junit.framework.TestSuite;
+import java.util.Arrays;
+
/**
- Test cases for the
{@link IntRange}
class.
*
@@ -160,6 +162,13 @@
assertEquals(false, big.containsInteger(Integer.MAX_VALUE - 3));
}
+ public void testToArray() {
+ int[] threeItems = new IntRange(3, 5).toArray();
+ assertTrue(Arrays.equals(new int[]
+ int[] oneItem = new IntRange(4).toArray();
+ assertTrue(Arrays.equals(new int[]{4}, oneItem));
+ }
+
//--------------------------------------------------------------------------
}
Index: src/test/org/apache/commons/lang/math/LongRangeTest.java
===================================================================
— src/test/org/apache/commons/lang/math/LongRangeTest.java (revision 506779)
+++ src/test/org/apache/commons/lang/math/LongRangeTest.java (working copy)
@@ -21,6 +21,8 @@
import junit.framework.Test;
import junit.framework.TestSuite;
+import java.util.Arrays;
+
/**
* Test cases for the {@link LongRange} class.
*
@@ -148,6 +150,12 @@
assertEquals(false, big.containsLong(Long.MAX_VALUE - 3));
}
+ public void testToArray() {
+ long[] threeItems = new LongRange(3, 5).toArray();
+ assertTrue(Arrays.equals(new long[]{3, 4, 5}
, threeItems));
+ long[] oneItem = new LongRange(4).toArray();
+ assertTrue(Arrays.equals(new long[]
, oneItem));
+ }
//--------------------------------------------------------------------------
}
Index: src/java/org/apache/commons/lang/math/IntRange.java
===================================================================
— src/java/org/apache/commons/lang/math/IntRange.java (revision 506779)
+++ src/java/org/apache/commons/lang/math/IntRange.java (working copy)
@@ -381,4 +381,16 @@
return toString;
}
+ /**
+ * <p>A built in array containing all the integer values in the range.</p>
+ *
+ * @return the <code>int[]</code> representation of this range
+ */
+ public int[] toArray() {
+ int[] array = new int[max - min + 1];
+ for(int i = 0; i < array.length; i++)
+ return array;
+ }
}
Index: src/java/org/apache/commons/lang/math/LongRange.java
===================================================================
— src/java/org/apache/commons/lang/math/LongRange.java (revision 506779)
+++ src/java/org/apache/commons/lang/math/LongRange.java (working copy)
@@ -394,4 +394,16 @@
return toString;
}
+ /**
+ * <p>A built in array containing all the integer values in the range.</p>
+ *
+ * @return the <code>long[]</code> representation of this range
+ */
+ public long[] toArray() {
+ long[] array = new long[(int)(max - min + 1L)];
+ for(int i = 0; i < array.length; i++) {+ array[i] = min + i;+ }
+ return array;
+ }
}