From 1ee57db3f6806f9a5979780e9caa382238673697 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Sat, 24 Mar 2012 01:37:42 +0000 Subject: [PATCH] Implementation of java.util.Arrays.copyOfRange required for Froyo. git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/branches/froyo_12.03@481 180e2498-e6e9-4542-8430-84ac67f01cd8 --- .../net/sf/openrocket/util/ArrayUtils.java | 46 ++++++++++ .../sf/openrocket/util/ArrayUtilsTest.java | 84 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 core/src/net/sf/openrocket/util/ArrayUtils.java create mode 100644 core/test/net/sf/openrocket/util/ArrayUtilsTest.java diff --git a/core/src/net/sf/openrocket/util/ArrayUtils.java b/core/src/net/sf/openrocket/util/ArrayUtils.java new file mode 100644 index 00000000..4202c53c --- /dev/null +++ b/core/src/net/sf/openrocket/util/ArrayUtils.java @@ -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[] 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 index 00000000..aefc2185 --- /dev/null +++ b/core/test/net/sf/openrocket/util/ArrayUtilsTest.java @@ -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] ); + } + + } + + +} -- 2.47.2