|
| 1 | +/** |
| 2 | + * Copyright 2009-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.ibatis.reflection; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +/** |
| 22 | + * Provides hashCode, equals and toString methods that can handle array. |
| 23 | + */ |
| 24 | +public class ArrayUtil { |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns a hash code for {@code obj}. |
| 28 | + * |
| 29 | + * @param obj |
| 30 | + * The object to get a hash code for. May be an array or <code>null</code>. |
| 31 | + * @return A hash code of {@code obj} or 0 if {@code obj} is <code>null</code> |
| 32 | + */ |
| 33 | + public static int hashCode(Object obj) { |
| 34 | + if (obj == null) { |
| 35 | + // for consistency with Arrays#hashCode() and Objects#hashCode() |
| 36 | + return 0; |
| 37 | + } |
| 38 | + final Class<?> clazz = obj.getClass(); |
| 39 | + if (!clazz.isArray()) { |
| 40 | + return obj.hashCode(); |
| 41 | + } |
| 42 | + final Class<?> componentType = clazz.getComponentType(); |
| 43 | + if (long.class.equals(componentType)) { |
| 44 | + return Arrays.hashCode((long[]) obj); |
| 45 | + } else if (int.class.equals(componentType)) { |
| 46 | + return Arrays.hashCode((int[]) obj); |
| 47 | + } else if (short.class.equals(componentType)) { |
| 48 | + return Arrays.hashCode((short[]) obj); |
| 49 | + } else if (char.class.equals(componentType)) { |
| 50 | + return Arrays.hashCode((char[]) obj); |
| 51 | + } else if (byte.class.equals(componentType)) { |
| 52 | + return Arrays.hashCode((byte[]) obj); |
| 53 | + } else if (boolean.class.equals(componentType)) { |
| 54 | + return Arrays.hashCode((boolean[]) obj); |
| 55 | + } else if (float.class.equals(componentType)) { |
| 56 | + return Arrays.hashCode((float[]) obj); |
| 57 | + } else if (double.class.equals(componentType)) { |
| 58 | + return Arrays.hashCode((double[]) obj); |
| 59 | + } else { |
| 60 | + return Arrays.hashCode((Object[]) obj); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Compares two objects. Returns <code>true</code> if |
| 66 | + * <ul> |
| 67 | + * <li>{@code thisObj} and {@code thatObj} are both <code>null</code></li> |
| 68 | + * <li>{@code thisObj} and {@code thatObj} are instances of the same type and |
| 69 | + * {@link Object#equals(Object)} returns <code>true</code></li> |
| 70 | + * <li>{@code thisObj} and {@code thatObj} are arrays with the same component type and |
| 71 | + * equals() method of {@link Arrays} returns <code>true</code> (not deepEquals())</li> |
| 72 | + * </ul> |
| 73 | + * |
| 74 | + * @param thisObj |
| 75 | + * The left hand object to compare. May be an array or <code>null</code> |
| 76 | + * @param thatObj |
| 77 | + * The right hand object to compare. May be an array or <code>null</code> |
| 78 | + * @return <code>true</code> if two objects are equal; <code>false</code> otherwise. |
| 79 | + */ |
| 80 | + public static boolean equals(Object thisObj, Object thatObj) { |
| 81 | + if (thisObj == null) { |
| 82 | + return thatObj == null; |
| 83 | + } else if (thatObj == null) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + final Class<?> clazz = thisObj.getClass(); |
| 87 | + if (!clazz.equals(thatObj.getClass())) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + if (!clazz.isArray()) { |
| 91 | + return thisObj.equals(thatObj); |
| 92 | + } |
| 93 | + final Class<?> componentType = clazz.getComponentType(); |
| 94 | + if (long.class.equals(componentType)) { |
| 95 | + return Arrays.equals((long[]) thisObj, (long[]) thatObj); |
| 96 | + } else if (int.class.equals(componentType)) { |
| 97 | + return Arrays.equals((int[]) thisObj, (int[]) thatObj); |
| 98 | + } else if (short.class.equals(componentType)) { |
| 99 | + return Arrays.equals((short[]) thisObj, (short[]) thatObj); |
| 100 | + } else if (char.class.equals(componentType)) { |
| 101 | + return Arrays.equals((char[]) thisObj, (char[]) thatObj); |
| 102 | + } else if (byte.class.equals(componentType)) { |
| 103 | + return Arrays.equals((byte[]) thisObj, (byte[]) thatObj); |
| 104 | + } else if (boolean.class.equals(componentType)) { |
| 105 | + return Arrays.equals((boolean[]) thisObj, (boolean[]) thatObj); |
| 106 | + } else if (float.class.equals(componentType)) { |
| 107 | + return Arrays.equals((float[]) thisObj, (float[]) thatObj); |
| 108 | + } else if (double.class.equals(componentType)) { |
| 109 | + return Arrays.equals((double[]) thisObj, (double[]) thatObj); |
| 110 | + } else { |
| 111 | + return Arrays.equals((Object[]) thisObj, (Object[]) thatObj); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * If the {@code obj} is an array, toString() method of {@link Arrays} is called. Otherwise |
| 117 | + * {@link Object#toString()} is called. Returns "null" if {@code obj} is <code>null</code>. |
| 118 | + * |
| 119 | + * @param obj |
| 120 | + * An object. May be an array or <code>null</code>. |
| 121 | + * @return String representation of the {@code obj}. |
| 122 | + */ |
| 123 | + public static String toString(Object obj) { |
| 124 | + if (obj == null) { |
| 125 | + return "null"; |
| 126 | + } |
| 127 | + final Class<?> clazz = obj.getClass(); |
| 128 | + if (!clazz.isArray()) { |
| 129 | + return obj.toString(); |
| 130 | + } |
| 131 | + final Class<?> componentType = obj.getClass().getComponentType(); |
| 132 | + if (long.class.equals(componentType)) { |
| 133 | + return Arrays.toString((long[]) obj); |
| 134 | + } else if (int.class.equals(componentType)) { |
| 135 | + return Arrays.toString((int[]) obj); |
| 136 | + } else if (short.class.equals(componentType)) { |
| 137 | + return Arrays.toString((short[]) obj); |
| 138 | + } else if (char.class.equals(componentType)) { |
| 139 | + return Arrays.toString((char[]) obj); |
| 140 | + } else if (byte.class.equals(componentType)) { |
| 141 | + return Arrays.toString((byte[]) obj); |
| 142 | + } else if (boolean.class.equals(componentType)) { |
| 143 | + return Arrays.toString((boolean[]) obj); |
| 144 | + } else if (float.class.equals(componentType)) { |
| 145 | + return Arrays.toString((float[]) obj); |
| 146 | + } else if (double.class.equals(componentType)) { |
| 147 | + return Arrays.toString((double[]) obj); |
| 148 | + } else { |
| 149 | + return Arrays.toString((Object[]) obj); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments