KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > ArrayUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.lang.reflect.Array JavaDoc;
26
27 /**
28     Provides:
29     - utility to check for equality
30  */

31 public final class ArrayUtil
32 {
33         private
34     ArrayUtil( )
35     {
36         // disallow instantiation
37
}
38     
39         public static boolean
40     arraysEqual( final Object JavaDoc array1, final Object JavaDoc array2 )
41     {
42         boolean equal = array1 == array2;
43         
44         if ( equal )
45         {
46             // same object or both null
47
return( true );
48         }
49         else if ( array1 == null || array2 == null)
50         {
51             return( false );
52         }
53         
54
55         if ( array1.getClass() == array2.getClass() &&
56             ClassUtil.objectIsArray( array1 ) &&
57             Array.getLength( array1 ) == Array.getLength( array2 ) )
58         {
59             equal = true;
60             final int length = Array.getLength( array1 );
61             
62             for( int i = 0; i < length; ++ i )
63             {
64                 final Object JavaDoc a1 = Array.get( array1, i );
65                 final Object JavaDoc a2 = Array.get( array2, i );
66                 
67                 if ( a1 != a2)
68                 {
69                     if ( a1 == null || a2 == null )
70                     {
71                         equal = false;
72                     }
73                     else if ( ClassUtil.objectIsArray( a1 ) )
74                     {
75                         if ( ! arraysEqual( a1, a2 ) )
76                         {
77                             equal = false;
78                         }
79                     }
80                 }
81
82                 if ( ! equal )
83                     break;
84             }
85         }
86         
87         return( equal );
88     }
89
90         public static boolean
91     arrayContainsNulls( final Object JavaDoc[] array )
92     {
93         boolean containsNulls = false;
94         
95         for( int i = 0; i < array.length; ++i )
96         {
97             if ( array[ i ] == null )
98             {
99                 containsNulls = true;
100                 break;
101             }
102         }
103         
104         return( containsNulls );
105     }
106     
107
108         public static <T> T[]
109     newArray( final Class JavaDoc<T> theClass, int numItems )
110     {
111         return (T[])( Array.newInstance( theClass, numItems ) );
112     }
113     
114     /**
115         Create a new array from the original.
116         
117         @param items the original array
118         @param startIndex index of the first item
119         @param numItems
120         @return an array of the same type, containing numItems items
121      */

122         public static <T> T[]
123     newArray(
124         final T[] items,
125         final int startIndex,
126         final int numItems )
127     {
128         final Class JavaDoc<T> theClass = TypeCast.asClass( items.getClass().getComponentType() );
129         
130         final T[] result = newArray( theClass, numItems );
131         System.arraycopy( items, startIndex, result, 0, numItems );
132         
133         return( result );
134     }
135     
136     /**
137         Create a new array consisting of originals and new.
138         
139         @param items1 1st array
140         @param items2 2nd array
141         @return an array of the same type as items1, its elements first
142      */

143         public static <T> T[]
144     newArray(
145         final T[] items1,
146         final T[] items2)
147     {
148         final Class JavaDoc<T> class1 = TypeCast.asClass( items1.getClass().getComponentType() );
149         final Class JavaDoc<T> class2 = TypeCast.asClass( items2.getClass().getComponentType() );
150         
151         if ( ! class1.isAssignableFrom( class2 ) )
152         {
153             throw new IllegalArgumentException JavaDoc();
154         }
155         
156         final int length1 = Array.getLength( items1 );
157         final int length2 = Array.getLength( items2 );
158         final T[] result = newArray( class1, length1 + length2 );
159         System.arraycopy( items1, 0, result, 0, length1 );
160         System.arraycopy( items2, 0, result, length1, length2 );
161         
162         return( result );
163     }
164     
165     /**
166         Create a new array consisting of an original array, and a single new item.
167         
168         @param items an array
169         @param item an item to append
170         @return an array of the same type as items1, its elements first
171      */

172         public static <T> T[]
173     newArray(
174         final T[] items,
175         final T item)
176     {
177         final Class JavaDoc<T> theClass = TypeCast.asClass( items.getClass().getComponentType() );
178         final T[] result = newArray( theClass, items.length + 1 );
179         System.arraycopy( items, 0, result, 0, items.length );
180         
181         result[ result.length - 1 ] = item;
182         return( result );
183     }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
Popular Tags