ArrayUtil.java

  1. /*
  2.  *    Copyright 2009-2023 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.  *       https://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. package org.apache.ibatis.reflection;

  17. import java.util.Arrays;

  18. /**
  19.  * Provides hashCode, equals and toString methods that can handle array.
  20.  */
  21. public class ArrayUtil {

  22.   /**
  23.    * Returns a hash code for {@code obj}.
  24.    *
  25.    * @param obj
  26.    *          The object to get a hash code for. May be an array or <code>null</code>.
  27.    *
  28.    * @return A hash code of {@code obj} or 0 if {@code obj} is <code>null</code>
  29.    */
  30.   public static int hashCode(Object obj) {
  31.     if (obj == null) {
  32.       // for consistency with Arrays#hashCode() and Objects#hashCode()
  33.       return 0;
  34.     }
  35.     final Class<?> clazz = obj.getClass();
  36.     if (!clazz.isArray()) {
  37.       return obj.hashCode();
  38.     }
  39.     final Class<?> componentType = clazz.getComponentType();
  40.     if (long.class.equals(componentType)) {
  41.       return Arrays.hashCode((long[]) obj);
  42.     }
  43.     if (int.class.equals(componentType)) {
  44.       return Arrays.hashCode((int[]) obj);
  45.     } else if (short.class.equals(componentType)) {
  46.       return Arrays.hashCode((short[]) obj);
  47.     } else if (char.class.equals(componentType)) {
  48.       return Arrays.hashCode((char[]) obj);
  49.     } else if (byte.class.equals(componentType)) {
  50.       return Arrays.hashCode((byte[]) obj);
  51.     } else if (boolean.class.equals(componentType)) {
  52.       return Arrays.hashCode((boolean[]) obj);
  53.     } else if (float.class.equals(componentType)) {
  54.       return Arrays.hashCode((float[]) obj);
  55.     } else if (double.class.equals(componentType)) {
  56.       return Arrays.hashCode((double[]) obj);
  57.     } else {
  58.       return Arrays.hashCode((Object[]) obj);
  59.     }
  60.   }

  61.   /**
  62.    * Compares two objects. Returns <code>true</code> if
  63.    * <ul>
  64.    * <li>{@code thisObj} and {@code thatObj} are both <code>null</code></li>
  65.    * <li>{@code thisObj} and {@code thatObj} are instances of the same type and {@link Object#equals(Object)} returns
  66.    * <code>true</code></li>
  67.    * <li>{@code thisObj} and {@code thatObj} are arrays with the same component type and equals() method of
  68.    * {@link Arrays} returns <code>true</code> (not deepEquals())</li>
  69.    * </ul>
  70.    *
  71.    * @param thisObj
  72.    *          The left hand object to compare. May be an array or <code>null</code>
  73.    * @param thatObj
  74.    *          The right hand object to compare. May be an array or <code>null</code>
  75.    *
  76.    * @return <code>true</code> if two objects are equal; <code>false</code> otherwise.
  77.    */
  78.   public static boolean equals(Object thisObj, Object thatObj) {
  79.     if (thisObj == null) {
  80.       return thatObj == null;
  81.     }
  82.     if (thatObj == null) {
  83.       return false;
  84.     }
  85.     final Class<?> clazz = thisObj.getClass();
  86.     if (!clazz.equals(thatObj.getClass())) {
  87.       return false;
  88.     }
  89.     if (!clazz.isArray()) {
  90.       return thisObj.equals(thatObj);
  91.     }
  92.     final Class<?> componentType = clazz.getComponentType();
  93.     if (long.class.equals(componentType)) {
  94.       return Arrays.equals((long[]) thisObj, (long[]) thatObj);
  95.     }
  96.     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.    * If the {@code obj} is an array, toString() method of {@link Arrays} is called. Otherwise {@link Object#toString()}
  116.    * is called. Returns "null" if {@code obj} is <code>null</code>.
  117.    *
  118.    * @param obj
  119.    *          An object. May be an array or <code>null</code>.
  120.    *
  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.     }
  135.     if (int.class.equals(componentType)) {
  136.       return Arrays.toString((int[]) obj);
  137.     } else if (short.class.equals(componentType)) {
  138.       return Arrays.toString((short[]) obj);
  139.     } else if (char.class.equals(componentType)) {
  140.       return Arrays.toString((char[]) obj);
  141.     } else if (byte.class.equals(componentType)) {
  142.       return Arrays.toString((byte[]) obj);
  143.     } else if (boolean.class.equals(componentType)) {
  144.       return Arrays.toString((boolean[]) obj);
  145.     } else if (float.class.equals(componentType)) {
  146.       return Arrays.toString((float[]) obj);
  147.     } else if (double.class.equals(componentType)) {
  148.       return Arrays.toString((double[]) obj);
  149.     } else {
  150.       return Arrays.toString((Object[]) obj);
  151.     }
  152.   }

  153.   private ArrayUtil() {
  154.   }

  155. }