commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brian Egge (JIRA)" <j...@apache.org>
Subject [jira] Created: (LANG-321) [patch] Add toArray() method to IntRange and LongRange classes
Date Tue, 13 Feb 2007 01:38:05 GMT
[patch] Add toArray() method to IntRange and LongRange classes
--------------------------------------------------------------

                 Key: LANG-321
                 URL: https://issues.apache.org/jira/browse/LANG-321
             Project: Commons Lang
          Issue Type: New Feature
            Reporter: Brian Egge
            Priority: Trivial


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[]{3, 4, 5}, threeItems));
+        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[]{4}, 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++) {
+            array[i] = min + 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;
+    }
 }


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message