]> git.gag.com Git - debian/openrocket/commitdiff
Implementation of java.util.Arrays.copyOfRange required for Froyo.
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Sat, 24 Mar 2012 01:37:42 +0000 (01:37 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Sat, 24 Mar 2012 01:37:42 +0000 (01:37 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/branches/froyo_12.03@481 180e2498-e6e9-4542-8430-84ac67f01cd8

core/src/net/sf/openrocket/util/ArrayUtils.java [new file with mode: 0644]
core/test/net/sf/openrocket/util/ArrayUtilsTest.java [new file with mode: 0644]

diff --git a/core/src/net/sf/openrocket/util/ArrayUtils.java b/core/src/net/sf/openrocket/util/ArrayUtils.java
new file mode 100644 (file)
index 0000000..4202c53
--- /dev/null
@@ -0,0 +1,46 @@
+package net.sf.openrocket.util;
+
+import java.lang.reflect.Array;
+
+public class ArrayUtils {
+
+       /**
+        * Implementation of java.util.Arrays.copyOfRange
+        * 
+        * Since Froyo does not include this function it must be implemented here.
+        * 
+        * @param original
+        * @param start
+        * @param end
+        * @return
+        */
+       public static <T> T[] copyOfRange( T[] original, int start, int end ) {
+               
+               if ( original == null ) {
+                       throw new NullPointerException();
+               }
+               
+               if ( start < 0 || start > original.length ) {
+                       throw new ArrayIndexOutOfBoundsException();
+               }
+               
+               if ( start > end ) {
+                       throw new IllegalArgumentException();
+               }
+               
+               T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
+               
+               int index = 0;
+               int stop = original.length < end ? original.length : end;
+               for ( int i = start; i < stop; i ++ ) {
+                       if ( i < original.length ) {
+                               result[index] = original[i];
+                       }
+                       index++;
+               }
+               
+               return result;
+               
+       }
+       
+}
diff --git a/core/test/net/sf/openrocket/util/ArrayUtilsTest.java b/core/test/net/sf/openrocket/util/ArrayUtilsTest.java
new file mode 100644 (file)
index 0000000..aefc218
--- /dev/null
@@ -0,0 +1,84 @@
+package net.sf.openrocket.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+public class ArrayUtilsTest {
+
+       @Test(expected=NullPointerException.class)
+       public void testCopyOfRange_NullArg() {
+               ArrayUtils.copyOfRange( (Byte[]) null, 0 , 14);
+       }
+       
+       @Test(expected=ArrayIndexOutOfBoundsException.class)
+       public void testCopyOfRange_StartTooBig() {
+               Integer[] original = new Integer[5];
+               ArrayUtils.copyOfRange( original, 8 , 14);
+       }
+       
+       @Test(expected=ArrayIndexOutOfBoundsException.class)
+       public void testCopyOfRange_StartTooSmall() {
+               Integer[] original = new Integer[5];
+               ArrayUtils.copyOfRange( original, -1 , 14);
+       }
+       
+       @Test(expected=IllegalArgumentException.class)
+       public void testCopyOfRange_IllegalRange() {
+               Integer[] original = new Integer[5];
+               ArrayUtils.copyOfRange( original, 5, 0 );
+       }
+       
+       @Test
+       public void testCopyOfRange() {
+               Integer[] original = new Integer[5];
+               for ( int i =0; i < 5; i++ ) {
+                       original[i] = i;
+               }
+               Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
+               assertEquals( 0, copy.length );
+               
+               copy = ArrayUtils.copyOfRange( original, 2, 2 );
+               assertEquals( 0, copy.length );
+               
+               copy = ArrayUtils.copyOfRange( original, 0, 2 );
+               assertEquals( 2, copy.length );
+               for( int i =0; i< 2; i++ ) {
+                       assertEquals( original[i], copy[i] );
+               }
+
+               copy = ArrayUtils.copyOfRange( original, 2, 5 );
+               assertEquals( 3, copy.length );
+               for( int i =0; i< 3; i++ ) {
+                       assertEquals( original[i+2], copy[i] );
+               }
+
+               copy = ArrayUtils.copyOfRange( original, 2, 15 );
+               assertEquals( 13, copy.length );
+               for( int i =0; i< 3; i++ ) {
+                       assertEquals( original[i+2], copy[i] );
+               }
+               for ( int i=3; i< 13; i++ ) {
+                       assertNull(copy[i]);
+               }
+
+       }
+
+       @Test
+       public void testCopyOfRange_ZeroSize() {
+               Integer[] original = new Integer[0];
+
+               Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
+               assertEquals( 0, copy.length );
+               
+               copy = ArrayUtils.copyOfRange( original, 0, 2 );
+               assertEquals( 2, copy.length );
+               for( int i =0; i< 2; i++ ) {
+                       assertEquals( null, copy[i] );
+               }
+
+       }
+       
+
+}