More changes to make Froyo compatible.
[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         public static <T> T[] copyOf( T[] original, int length ) {
8                 return copyOfRange(original,0,length);
9         }
10         
11         /**
12          * Implementation of java.util.Arrays.copyOfRange
13          * 
14          * Since Froyo does not include this function it must be implemented here.
15          * 
16          * @param original
17          * @param start
18          * @param end
19          * @return
20          */
21         public static <T> T[] copyOfRange( T[] original, int start, int end ) {
22                 
23                 if ( original == null ) {
24                         throw new NullPointerException();
25                 }
26                 
27                 if ( start < 0 || start > original.length ) {
28                         throw new ArrayIndexOutOfBoundsException();
29                 }
30                 
31                 if ( start > end ) {
32                         throw new IllegalArgumentException();
33                 }
34                 
35                 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
36                 
37                 int index = 0;
38                 int stop = original.length < end ? original.length : end;
39                 for ( int i = start; i < stop; i ++ ) {
40                         if ( i < original.length ) {
41                                 result[index] = original[i];
42                         }
43                         index++;
44                 }
45                 
46                 return result;
47                 
48         }
49
50         public static double[] copyOf( double[] original, int length ) {
51                 return copyOfRange(original,0,length);
52         }
53         
54         public static double[] copyOfRange( double[] original, int start, int end ) {
55                 
56                 if ( original == null ) {
57                         throw new NullPointerException();
58                 }
59                 
60                 if ( start < 0 || start > original.length ) {
61                         throw new ArrayIndexOutOfBoundsException();
62                 }
63                 
64                 if ( start > end ) {
65                         throw new IllegalArgumentException();
66                 }
67                 
68                 double[] result = new double[(end-start)];
69                 
70                 int index = 0;
71                 int stop = original.length < end ? original.length : end;
72                 for ( int i = start; i < stop; i ++ ) {
73                         if ( i < original.length ) {
74                                 result[index] = original[i];
75                         }
76                         index++;
77                 }
78                 
79                 return result;
80                 
81         }
82
83         public static byte[] copyOf( byte[] original, int length ) {
84                 return copyOfRange(original,0,length);
85         }
86         
87         public static byte[] copyOfRange( byte[] original, int start, int end ) {
88                 
89                 if ( original == null ) {
90                         throw new NullPointerException();
91                 }
92                 
93                 if ( start < 0 || start > original.length ) {
94                         throw new ArrayIndexOutOfBoundsException();
95                 }
96                 
97                 if ( start > end ) {
98                         throw new IllegalArgumentException();
99                 }
100                 
101                 byte[] result = new byte[(end-start)];
102                 
103                 int index = 0;
104                 int stop = original.length < end ? original.length : end;
105                 for ( int i = start; i < stop; i ++ ) {
106                         if ( i < original.length ) {
107                                 result[index] = original[i];
108                         }
109                         index++;
110                 }
111                 
112                 return result;
113                 
114         }
115
116 }