1 32 package net.sf.retrotranslator.runtime.java.util; 33 34 import java.util.*; 35 36 39 public class _Arrays { 40 41 private static final String NULL = "null"; 42 private static final String EMPTY_ARRAY = "[]"; 43 private static final char LEFT_BRACKET = '['; 44 private static final char RIGHT_BRACKET = ']'; 45 private static final String SEPARATOR = ", "; 46 47 public static boolean deepEquals(Object [] a1, Object [] a2) { 48 return isEqual(a1, a2); 49 } 50 51 public static int deepHashCode(Object [] a) { 52 if (a == null) return 0; 53 int hashCode = 1; 54 for (Object element : a) { 55 hashCode = 31 * hashCode + getHashCode(element); 56 } 57 return hashCode; 58 } 59 60 public static String deepToString(Object [] a) { 61 if (a == null) return NULL; 62 StringBuilder builder = new StringBuilder (); 63 appendArray(builder, a, new HashSet<Object []>()); 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 [] a) { 132 if (a == null) return 0; 133 int hashCode = 1; 134 for (Object 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 toString(boolean[] a) { 150 if (a == null) return NULL; 151 if (a.length == 0) return EMPTY_ARRAY; 152 StringBuilder builder = new StringBuilder (); 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 toString(byte[] a) { 161 if (a == null) return NULL; 162 if (a.length == 0) return EMPTY_ARRAY; 163 StringBuilder builder = new StringBuilder (); 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 toString(char[] a) { 172 if (a == null) return NULL; 173 if (a.length == 0) return EMPTY_ARRAY; 174 StringBuilder builder = new StringBuilder (); 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 toString(double[] a) { 183 if (a == null) return NULL; 184 if (a.length == 0) return EMPTY_ARRAY; 185 StringBuilder builder = new StringBuilder (); 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 toString(float[] a) { 194 if (a == null) return NULL; 195 if (a.length == 0) return EMPTY_ARRAY; 196 StringBuilder builder = new StringBuilder (); 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 toString(int[] a) { 205 if (a == null) return NULL; 206 if (a.length == 0) return EMPTY_ARRAY; 207 StringBuilder builder = new StringBuilder (); 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 toString(long[] a) { 216 if (a == null) return NULL; 217 if (a.length == 0) return EMPTY_ARRAY; 218 StringBuilder builder = new StringBuilder (); 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 toString(Object [] a) { 227 if (a == null) return NULL; 228 if (a.length == 0) return EMPTY_ARRAY; 229 StringBuilder builder = new StringBuilder (); 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 toString(short[] a) { 238 if (a == null) return NULL; 239 if (a.length == 0) return EMPTY_ARRAY; 240 StringBuilder builder = new StringBuilder (); 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 builder, Object [] a, Set<Object []> 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 builder, Object o, Set<Object []> history) { 266 if (o instanceof Object []) { 267 appendArray(builder, (Object []) o, history); 268 } else { 269 builder.append(getString(o)); 270 } 271 } 272 273 private static int getHashCode(Object o) { 274 if (o == null) return 0; 275 if (o instanceof Object []) return deepHashCode((Object []) 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 getString(Object 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 o1, Object o2) { 301 if (o1 == o2) return true; 302 if (o1 == null || o2 == null) return false; 303 if (o1 instanceof Object [] && o2 instanceof Object []) { 304 Object [] a1 = (Object []) o1; 305 Object [] a2 = (Object []) 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 |