1 /***
2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p>
3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
4 * conditions are met:<p>
5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the
8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written
9 * permission.<p>
10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p>
16 */
17
18 package com.codestreet.messageforge;
19
20 /***
21 * Class to compare arrays for equality. We cannot use <tt>java.util.Arrays.equals</tt>
22 * because it is not supported in JDK1.1.
23 * @author Jawaid Hakim.
24 */
25 public abstract class RArrayCompare
26 {
27 /***
28 * Compare <tt>short</tt> arrays for equality.
29 * @param first First array.
30 * @param second Second array.
31 * @return <tt>true</tt> if the two arrays have the same length and
32 * identical entries. Otherwise, returns <tt>false</tt>.
33 */
34 public static boolean equals(short[] first, short[] second)
35 {
36 // Sanity check
37 if (first == second)
38 return true;
39
40 boolean ret = (first != null && second != null && first.length == second.length);
41 if (ret)
42 {
43 // Sanity check
44 if (first == second)
45 return ret;
46
47 for (int i = first.length - 1; ret && i >= 0; --i)
48 ret = (first[i] == second[i]);
49 }
50 return ret;
51 }
52
53 /***
54 * Compare <tt>int</tt> arrays for equality.
55 * @param first First array.
56 * @param second Second array.
57 * @return <tt>true</tt> if the two arrays have the same length and
58 * identical entries. Otherwise, returns <tt>false</tt>.
59 */
60 public static boolean equals(int[] first, int[] second)
61 {
62 // Sanity check
63 if (first == second)
64 return true;
65
66 boolean ret = (first != null && second != null && first.length == second.length);
67 if (ret)
68 {
69 for (int i = first.length - 1; ret && i >= 0; --i)
70 ret = (first[i] == second[i]);
71 }
72 return ret;
73 }
74
75 /***
76 * Compare <tt>long<</tt> arrays for equality.
77 * @param first First array.
78 * @param second Second array.
79 * @return <tt>true</tt> if the two arrays have the same length and
80 * identical entries. Otherwise, returns <tt>false</tt>.
81 */
82 public static boolean equals(long[] first, long[] second)
83 {
84 // Sanity check
85 if (first == second)
86 return true;
87
88 boolean ret = (first != null && second != null && first.length == second.length);
89 if (ret)
90 {
91 for (int i = first.length - 1; ret && i >= 0; --i)
92 ret = (first[i] == second[i]);
93 }
94 return ret;
95 }
96
97 /***
98 * Compare <tt>float<</tt> arrays for equality.
99 * @param first First array.
100 * @param second Second array.
101 * @return <tt>true</tt> if the two arrays have the same length and
102 * identical entries. Otherwise, returns <tt>false</tt>.
103 */
104 public static boolean equals(float[] first, float[] second)
105 {
106 // Sanity check
107 if (first == second)
108 return true;
109
110 boolean ret = (first != null && second != null && first.length == second.length);
111 if (ret)
112 {
113 for (int i = first.length - 1; ret && i >= 0; --i)
114 ret = (Float.floatToIntBits(first[i]) == Float.floatToIntBits(second[i]));
115 }
116 return ret;
117 }
118
119 /***
120 * Compare <tt>double<</tt> arrays for equality.
121 * @param first First array.
122 * @param second Second array.
123 * @return <tt>true</tt> if the two arrays have the same length and
124 * identical entries. Otherwise, returns <tt>false</tt>.
125 */
126 public static boolean equals(double[] first, double[] second)
127 {
128 // Sanity check
129 if (first == second)
130 return true;
131
132 boolean ret = (first != null && second != null && first.length == second.length);
133 if (ret)
134 {
135 for (int i = first.length - 1; ret && i >= 0; --i)
136 ret = (Double.doubleToLongBits(first[i]) == Double.doubleToLongBits(second[i]));
137 }
138 return ret;
139 }
140
141 /***
142 * Compare <tt>char<</tt> arrays for equality.
143 * @param first First array.
144 * @param second Second array.
145 * @return <tt>true</tt> if the two arrays have the same length and
146 * identical entries. Otherwise, returns <tt>false</tt>.
147 */
148 public static boolean equals(char[] first, char[] second)
149 {
150 // Sanity check
151 if (first == second)
152 return true;
153
154 boolean ret = (first != null && second != null && first.length == second.length);
155 if (ret)
156 {
157 for (int i = first.length - 1; ret && i >= 0; --i)
158 ret = (first[i] == second[i]);
159 }
160 return ret;
161 }
162
163 /***
164 * Compare <tt>byte<</tt> arrays for equality.
165 * @param first First array.
166 * @param second Second array.
167 * @return <tt>true</tt> if the two arrays have the same length and
168 * identical entries. Otherwise, returns <tt>false</tt>.
169 */
170 public static boolean equals(byte[] first, byte[] second)
171 {
172 // Sanity check
173 if (first == second)
174 return true;
175
176 boolean ret = (first != null && second != null && first.length == second.length);
177 if (ret)
178 {
179 for (int i = first.length - 1; ret && i >= 0; --i)
180 ret = (first[i] == second[i]);
181 }
182 return ret;
183 }
184
185 /***
186 * Compare <tt>boolean</tt> arrays for equality.
187 * @param first First array.
188 * @param second Second array.
189 * @return <tt>true</tt> if the two arrays have the same length and
190 * identical entries. Otherwise, returns <tt>false</tt>.
191 */
192 public static boolean equals(boolean[] first, boolean[] second)
193 {
194 // Sanity check
195 if (first == second)
196 return true;
197
198 boolean ret = (first != null && second != null && first.length == second.length);
199 if (ret)
200 {
201 for (int i = first.length - 1; ret && i >= 0; --i)
202 ret = (first[i] == second[i]);
203 }
204 return ret;
205 }
206
207 /***
208 * Compare <tt>String</tt> arrays for equality.
209 * @param first First array.
210 * @param second Second array.
211 * @return <tt>true</tt> if the two arrays have the same length and
212 * identical entries. Otherwise, returns <tt>false</tt>. Calls <tt>equals</tt>
213 * on the entries to test for equality.
214 */
215 public static boolean equals(String[] first, String[] second)
216 {
217 // Sanity check
218 if (first == second)
219 return true;
220
221 boolean ret = (first != null && second != null && first.length == second.length);
222 if (ret)
223 {
224 for (int i = first.length - 1; ret && i >= 0; --i)
225 ret = (first[i].equals(second[i]));
226 }
227 return ret;
228 }
229
230 /***
231 * Compare <tt>Object</tt> arrays for equality.
232 * @param first First array.
233 * @param second Second array.
234 * @return <tt>true</tt> if the two arrays have the same length and
235 * equal entries. Otherwise, returns <tt>false</tt>. Calls <tt>equals</tt>
236 * on the entries to test for equality.
237 */
238 public static boolean equals(Object[] first, Object[] second)
239 {
240 // Sanity check
241 if (first == second)
242 return true;
243
244 boolean ret = (first != null && second != null && first.length == second.length);
245 if (ret)
246 {
247 for (int i = first.length - 1; ret && i >= 0; --i)
248 ret = (first[i].equals(second[i]));
249 }
250 return ret;
251 }
252 }
This page was automatically generated by Maven