Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-321

[patch] Add toArray() method to IntRange and LongRange classes

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • None
    • 2.4
    • 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[]

      {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;
      + }
      }

      Attachments

        1. LANG-321.patch
          3 kB
          Ben Speakmon

        Activity

          People

            Unassigned Unassigned
            brianegge Brian Egge
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: