KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > _Arrays


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.util;
33
34 import java.util.*;
35
36 /**
37  * @author Taras Puchko
38  */

39 public class _Arrays {
40
41     private static final String JavaDoc NULL = "null";
42     private static final String JavaDoc EMPTY_ARRAY = "[]";
43     private static final char LEFT_BRACKET = '[';
44     private static final char RIGHT_BRACKET = ']';
45     private static final String JavaDoc SEPARATOR = ", ";
46
47     public static boolean deepEquals(Object JavaDoc[] a1, Object JavaDoc[] a2) {
48         return isEqual(a1, a2);
49     }
50
51     public static int deepHashCode(Object JavaDoc[] a) {
52         if (a == null) return 0;
53         int hashCode = 1;
54         for (Object JavaDoc element : a) {
55             hashCode = 31 * hashCode + getHashCode(element);
56         }
57         return hashCode;
58     }
59
60     public static String JavaDoc deepToString(Object JavaDoc[] a) {
61         if (a == null) return NULL;
62         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
63         appendArray(builder, a, new HashSet<Object JavaDoc[]>());
64         return builder.toString();
65     }
66
67     public static int hashCode(boolean[] a) {
68         if (a == null) return 0;
69         int hashCode = 1;
70         for (boolean element : a) {
71             hashCode = 31 * hashCode + (element ? 1231 : 1237);
72         }
73         return hashCode;
74     }
75
76     public static int hashCode(byte[] a) {
77         if (a == null) return 0;
78         int hashCode = 1;
79         for (byte element : a) {
80             hashCode = 31 * hashCode + element;
81         }
82         return hashCode;
83     }
84
85     public static int hashCode(char[] a) {
86         if (a == null) return 0;
87         int hashCode = 1;
88         for (char element : a) {
89             hashCode = 31 * hashCode + element;
90         }
91         return hashCode;
92     }
93
94     public static int hashCode(double[] a) {
95         if (a == null) return 0;
96         int hashCode = 1;
97         for (double element : a) {
98             long longBits = Double.doubleToLongBits(element);
99             hashCode = 31 * hashCode + (int) (longBits ^ (longBits >>> 32));
100         }
101         return hashCode;
102     }
103
104     public static int hashCode(float[] a) {
105         if (a == null) return 0;
106         int hashCode = 1;
107         for (float element : a) {
108             hashCode = 31 * hashCode + Float.floatToIntBits(element);
109         }
110         return hashCode;
111     }
112
113     public static int hashCode(int[] a) {
114         if (a == null) return 0;
115         int hashCode = 1;
116         for (int element : a) {
117             hashCode = 31 * hashCode + element;
118         }
119         return hashCode;
120     }
121
122     public static int hashCode(long[] a) {
123         if (a == null) return 0;
124         int hashCode = 1;
125         for (long element : a) {
126             hashCode = 31 * hashCode + (int) (element ^ (element >>> 32));
127         }
128         return hashCode;
129     }
130
131     public static int hashCode(Object JavaDoc[] a) {
132         if (a == null) return 0;
133         int hashCode = 1;
134         for (Object JavaDoc element : a) {
135             hashCode = 31 * hashCode + (element == null ? 0 : element.hashCode());
136         }
137         return hashCode;
138     }
139
140     public static int hashCode(short[] a) {
141         if (a == null) return 0;
142         int hashCode = 1;
143         for (short element : a) {
144             hashCode = 31 * hashCode + element;
145         }
146         return hashCode;
147     }
148
149     public static String JavaDoc toString(boolean[] a) {
150         if (a == null) return NULL;
151         if (a.length == 0) return EMPTY_ARRAY;
152         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
153         builder.append(LEFT_BRACKET).append(a[0]);
154         for (int i = 1; i < a.length; i++) {
155             builder.append(SEPARATOR).append(a[i]);
156         }
157         return builder.append(RIGHT_BRACKET).toString();
158     }
159
160     public static String JavaDoc toString(byte[] a) {
161         if (a == null) return NULL;
162         if (a.length == 0) return EMPTY_ARRAY;
163         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
164         builder.append(LEFT_BRACKET).append(a[0]);
165         for (int i = 1; i < a.length; i++) {
166             builder.append(SEPARATOR).append(a[i]);
167         }
168         return builder.append(RIGHT_BRACKET).toString();
169     }
170
171     public static String JavaDoc toString(char[] a) {
172         if (a == null) return NULL;
173         if (a.length == 0) return EMPTY_ARRAY;
174         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
175         builder.append(LEFT_BRACKET).append(a[0]);
176         for (int i = 1; i < a.length; i++) {
177             builder.append(SEPARATOR).append(a[i]);
178         }
179         return builder.append(RIGHT_BRACKET).toString();
180     }
181
182     public static String JavaDoc toString(double[] a) {
183         if (a == null) return NULL;
184         if (a.length == 0) return EMPTY_ARRAY;
185         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
186         builder.append(LEFT_BRACKET).append(a[0]);
187         for (int i = 1; i < a.length; i++) {
188             builder.append(SEPARATOR).append(a[i]);
189         }
190         return builder.append(RIGHT_BRACKET).toString();
191     }
192
193     public static String JavaDoc toString(float[] a) {
194         if (a == null) return NULL;
195         if (a.length == 0) return EMPTY_ARRAY;
196         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
197         builder.append(LEFT_BRACKET).append(a[0]);
198         for (int i = 1; i < a.length; i++) {
199             builder.append(SEPARATOR).append(a[i]);
200         }
201         return builder.append(RIGHT_BRACKET).toString();
202     }
203
204     public static String JavaDoc toString(int[] a) {
205         if (a == null) return NULL;
206         if (a.length == 0) return EMPTY_ARRAY;
207         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
208         builder.append(LEFT_BRACKET).append(a[0]);
209         for (int i = 1; i < a.length; i++) {
210             builder.append(SEPARATOR).append(a[i]);
211         }
212         return builder.append(RIGHT_BRACKET).toString();
213     }
214
215     public static String JavaDoc toString(long[] a) {
216         if (a == null) return NULL;
217         if (a.length == 0) return EMPTY_ARRAY;
218         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
219         builder.append(LEFT_BRACKET).append(a[0]);
220         for (int i = 1; i < a.length; i++) {
221             builder.append(SEPARATOR).append(a[i]);
222         }
223         return builder.append(RIGHT_BRACKET).toString();
224     }
225
226     public static String JavaDoc toString(Object JavaDoc[] a) {
227         if (a == null) return NULL;
228         if (a.length == 0) return EMPTY_ARRAY;
229         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
230         builder.append(LEFT_BRACKET).append(a[0]);
231         for (int i = 1; i < a.length; i++) {
232             builder.append(SEPARATOR).append(a[i]);
233         }
234         return builder.append(RIGHT_BRACKET).toString();
235     }
236
237     public static String JavaDoc toString(short[] a) {
238         if (a == null) return NULL;
239         if (a.length == 0) return EMPTY_ARRAY;
240         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
241         builder.append(LEFT_BRACKET).append(a[0]);
242         for (int i = 1; i < a.length; i++) {
243             builder.append(SEPARATOR).append(a[i]);
244         }
245         return builder.append(RIGHT_BRACKET).toString();
246     }
247
248     private static void appendArray(StringBuilder JavaDoc builder, Object JavaDoc[] a, Set<Object JavaDoc[]> history) {
249         int length = a.length;
250         if (length == 0) {
251             builder.append(EMPTY_ARRAY);
252             return;
253         }
254         if (!history.add(a)) {
255             builder.append("[...]");
256             return;
257         }
258         appendObject(builder.append(LEFT_BRACKET), a[0], history);
259         for (int i = 1; i < length; i++) {
260             appendObject(builder.append(SEPARATOR), a[i], history);
261         }
262         builder.append(RIGHT_BRACKET);
263     }
264
265     private static void appendObject(StringBuilder JavaDoc builder, Object JavaDoc o, Set<Object JavaDoc[]> history) {
266         if (o instanceof Object JavaDoc[]) {
267             appendArray(builder, (Object JavaDoc[]) o, history);
268         } else {
269             builder.append(getString(o));
270         }
271     }
272
273     private static int getHashCode(Object JavaDoc o) {
274         if (o == null) return 0;
275         if (o instanceof Object JavaDoc[]) return deepHashCode((Object JavaDoc[]) o);
276         if (o instanceof boolean[]) return hashCode((boolean[]) o);
277         if (o instanceof byte[]) return hashCode((byte[]) o);
278         if (o instanceof char[]) return hashCode((char[]) o);
279         if (o instanceof double[]) return hashCode((double[]) o);
280         if (o instanceof float[]) return hashCode((float[]) o);
281         if (o instanceof int[]) return hashCode((int[]) o);
282         if (o instanceof long[]) return hashCode((long[]) o);
283         if (o instanceof short[]) return hashCode((short[]) o);
284         return o.hashCode();
285     }
286
287     private static String JavaDoc getString(Object JavaDoc o) {
288         if (o == null) return null;
289         if (o instanceof boolean[]) return toString((boolean[]) o);
290         if (o instanceof byte[]) return toString((byte[]) o);
291         if (o instanceof char[]) return toString((char[]) o);
292         if (o instanceof double[]) return toString((double[]) o);
293         if (o instanceof float[]) return toString((float[]) o);
294         if (o instanceof int[]) return toString((int[]) o);
295         if (o instanceof long[]) return toString((long[]) o);
296         if (o instanceof short[]) return toString((short[]) o);
297         return o.toString();
298     }
299
300     private static boolean isEqual(Object JavaDoc o1, Object JavaDoc o2) {
301         if (o1 == o2) return true;
302         if (o1 == null || o2 == null) return false;
303         if (o1 instanceof Object JavaDoc[] && o2 instanceof Object JavaDoc[]) {
304             Object JavaDoc[] a1 = (Object JavaDoc[]) o1;
305             Object JavaDoc[] a2 = (Object JavaDoc[]) o2;
306             int length = a1.length;
307             if (length != a2.length) return false;
308             for (int i = 0; i < length; i++) {
309                 if (!isEqual(a1[i], a2[i])) return false;
310             }
311             return true;
312         }
313         if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
314             return Arrays.equals(((boolean[]) o1), ((boolean[]) o2));
315         }
316         if (o1 instanceof byte[] && o2 instanceof byte[]) {
317             return Arrays.equals(((byte[]) o1), ((byte[]) o2));
318         }
319         if (o1 instanceof char[] && o2 instanceof char[]) {
320             return Arrays.equals(((char[]) o1), ((char[]) o2));
321         }
322         if (o1 instanceof double[] && o2 instanceof double[]) {
323             return Arrays.equals(((double[]) o1), ((double[]) o2));
324         }
325         if (o1 instanceof float[] && o2 instanceof float[]) {
326             return Arrays.equals(((float[]) o1), ((float[]) o2));
327         }
328         if (o1 instanceof int[] && o2 instanceof int[]) {
329             return Arrays.equals(((int[]) o1), ((int[]) o2));
330         }
331         if (o1 instanceof long[] && o2 instanceof long[]) {
332             return Arrays.equals(((long[]) o1), ((long[]) o2));
333         }
334         if (o1 instanceof short[] && o2 instanceof short[]) {
335             return Arrays.equals(((short[]) o1), ((short[]) o2));
336         }
337         return o1.equals(o2);
338     }
339
340 }
341
Popular Tags