Applying Ben's patch of Brian Egge's improvement in LANG-321

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@594388 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2007-11-13 01:29:10 +00:00
parent 930b389bac
commit da6569b4a6
4 changed files with 44 additions and 0 deletions

View File

@ -381,4 +381,17 @@ public String toString() {
return toString;
}
/**
* <p>Returns an 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;
}
}

View File

@ -394,4 +394,17 @@ public String toString() {
return toString;
}
/**
* <p>Returns an array containing all the long 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;
}
}

View File

@ -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 @@ public void testContainsIntegerBig() {
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));
}
//--------------------------------------------------------------------------
}

View File

@ -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,13 @@ public void testContainsLongBig() {
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));
}
//--------------------------------------------------------------------------
}