KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > ExtArrays


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.List JavaDoc;
22 import org.aopalliance.intercept.Interceptor;
23 import java.lang.reflect.Array JavaDoc;
24
25
26
27 /**
28  * Various often used array functions
29  */

30 public class ExtArrays
31 {
32     public static final Object JavaDoc[] emptyObjectArray = new Object JavaDoc[0];
33     public static final String JavaDoc[] emptyStringArray = new String JavaDoc[0];
34     public static final Class JavaDoc[] emptyClassArray = new Class JavaDoc[0];
35     public static final Interceptor[] emptyInterceptorArray = new Interceptor[0];
36
37     /**
38      * Insert an object into an array
39      *
40      * @param position of the object to insert (>=0 and <=array.length)
41      * @param toInsert the object to insert
42      * @param array the array
43      * @return a new array of size array.length+1, where item at
44      * position "position" is toInsert. The actual type of the
45      * returned array is the same as the parameter <code>array</code>,
46      * or the type of <code>toInsert</code>, or Object[].
47      *
48      * @see #add(int,Object,Object[],Class)
49      * @see #add(Object,Object[])
50      */

51     public static Object JavaDoc[] add(int position, Object JavaDoc toInsert, Object JavaDoc[] array) {
52         Class JavaDoc type = array.getClass().getComponentType();
53         if (!type.isInstance(toInsert)) {
54             if (toInsert.getClass().isAssignableFrom(type))
55                 type = toInsert.getClass();
56             else
57                 type = Object JavaDoc.class;
58         }
59         return add(position,toInsert,array,type);
60     }
61
62     /**
63      * Insert an object into an array
64      *
65      * @param position of the object to insert (>=0 and <=array.length)
66      * @param toInsert the object to insert
67      * @param array the array
68      * @param type component type of the array to return
69      * @return a new array of size array.length+1, where item at
70      * position "position" is toInsert.
71      *
72      * @see #add(int,Object,Object[])
73      */

74     public static Object JavaDoc[] add(int position, Object JavaDoc toInsert, Object JavaDoc[] array, Class JavaDoc type) {
75         Object JavaDoc[] newArray = (Object JavaDoc[])
76             Array.newInstance(type,array.length+1);
77         if (position>0)
78             System.arraycopy(array,0,newArray,0,position);
79         newArray[position] = toInsert;
80         if (position<array.length) {
81             System.arraycopy(array,position,
82                              newArray,position+1,
83                              array.length-position);
84         }
85         return newArray;
86     }
87
88     /**
89      * Append an object at the end of an array
90      * @param toAppend the object to add
91      * @param array the array
92      * @return a new array, of length array.length+1 and whose last item
93      * is toAppend
94      *
95      * @see #add(int,Object,Object[],Class)
96      * @see #add(Object,Object[],Class)
97      */

98     public static Object JavaDoc[] add(Object JavaDoc toAppend, Object JavaDoc[] array) {
99         return add(array.length,toAppend,array);
100     }
101
102     /**
103      * Append an object at the end of an array
104      * @param toAppend the object to add
105      * @param array the array
106      * @param type component type of the array to return
107      * @return a new array, of length array.length+1 and whose last item
108      * is toAppend
109      *
110      * @see #add(Object,Object[])
111      * @see #add(int,Object,Object[])
112      */

113     public static Object JavaDoc[] add(Object JavaDoc toAppend, Object JavaDoc[] array, Class JavaDoc type) {
114         return add(array.length,toAppend,array,type);
115     }
116
117     /**
118      * Returns the index of a value in an array of Object.
119      *
120      * @param array the array
121      * @param value the searched value
122      * @return Returns the lowest integer value i such that
123      * array[i]==value, or -1.
124      */

125     public static int indexOf(Object JavaDoc[] array, Object JavaDoc value) {
126         for (int i=0; i<array.length; i++) {
127             if (value==array[i]) {
128                 return i;
129             }
130         }
131         return -1;
132     }
133
134     /**
135      * Tells wether an array of objects contains a given value
136      * @param array the array to search the value in
137      * @param value the object to search for
138      * @return true if array contains value
139      */

140     public static boolean contains(Object JavaDoc[] array, Object JavaDoc value) {
141         return indexOf(array,value)!=-1;
142     }
143
144     /**
145      * Tests equality of some elements of two arrays of bytes.
146      * @param a first array of bytes
147      * @param offseta start comparison in first array with this offset
148      * @param b second array
149      * @param offsetb start comparison in second array with this offset
150      * @param length number of bytes to compare
151      */

152     public static boolean equals(byte[] a, int offseta,
153                                  byte[] b, int offsetb,
154                                  int length) {
155         for (int i=0; i<length; i++) {
156             if (a[offseta+i]!=b[offsetb+i])
157                 return false;
158         }
159         return true;
160     }
161
162     /**
163      * Builds a List out of an array of bytes
164      * @return a List whose elements are Byte objects
165      */

166     public static List JavaDoc asList(byte[] array) {
167         Object JavaDoc[] objects = new Object JavaDoc[array.length];
168         for (int i=0; i<array.length; i++) {
169             objects[i] = new Byte JavaDoc(array[i]);
170         }
171         return Arrays.asList(objects);
172     }
173
174     /**
175      *
176      */

177     public static Object JavaDoc subArray(Object JavaDoc[] array, int start) {
178         Object JavaDoc result =
179             Array.newInstance(
180                 array.getClass().getComponentType(),
181                 array.length-start);
182         System.arraycopy(array, start, result, 0, array.length-start);
183         return result;
184     }
185 }
186
Popular Tags