4202c53ce8623389b872f8184e69a866b171b793
[debian/openrocket] / core / src / net / sf / openrocket / util / ArrayUtils.java
1 package net.sf.openrocket.util;
2
3 import java.lang.reflect.Array;
4
5 public class ArrayUtils {
6
7         /**
8          * Implementation of java.util.Arrays.copyOfRange
9          * 
10          * Since Froyo does not include this function it must be implemented here.
11          * 
12          * @param original
13          * @param start
14          * @param end
15          * @return
16          */
17         public static <T> T[] copyOfRange( T[] original, int start, int end ) {
18                 
19                 if ( original == null ) {
20                         throw new NullPointerException();
21                 }
22                 
23                 if ( start < 0 || start > original.length ) {
24                         throw new ArrayIndexOutOfBoundsException();
25                 }
26                 
27                 if ( start > end ) {
28                         throw new IllegalArgumentException();
29                 }
30                 
31                 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
32                 
33                 int index = 0;
34                 int stop = original.length < end ? original.length : end;
35                 for ( int i = start; i < stop; i ++ ) {
36                         if ( i < original.length ) {
37                                 result[index] = original[i];
38                         }
39                         index++;
40                 }
41                 
42                 return result;
43                 
44         }
45         
46 }