You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2007/02/14 21:44:06 UTC

[jira] Updated: (LANG-321) [patch] Add toArray() method to IntRange and LongRange classes

     [ https://issues.apache.org/jira/browse/LANG-321?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-321:
-------------------------------

    Fix Version/s: 3.0

Seems fine to me. Adding to the 3.0 roadmap - will wait a short while before applying in case anyone else has an opinion.

> [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
>             Fix For: 3.0
>
>
> 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