All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.ibatis.reflection.ArrayUtil Maven / Gradle / Ivy

There is a newer version: 3.5.16_1
Show newest version
/**
 *    Copyright 2009-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.apache.ibatis.reflection;

import java.util.Arrays;

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

  /**
   * Returns a hash code for {@code obj}.
   * 
   * @param obj
   *          The object to get a hash code for. May be an array or null.
   * @return A hash code of {@code obj} or 0 if {@code obj} is null
   */
  public static int hashCode(Object obj) {
    if (obj == null) {
      // for consistency with Arrays#hashCode() and Objects#hashCode()
      return 0;
    }
    final Class clazz = obj.getClass();
    if (!clazz.isArray()) {
      return obj.hashCode();
    }
    final Class componentType = clazz.getComponentType();
    if (long.class.equals(componentType)) {
      return Arrays.hashCode((long[]) obj);
    } else if (int.class.equals(componentType)) {
      return Arrays.hashCode((int[]) obj);
    } else if (short.class.equals(componentType)) {
      return Arrays.hashCode((short[]) obj);
    } else if (char.class.equals(componentType)) {
      return Arrays.hashCode((char[]) obj);
    } else if (byte.class.equals(componentType)) {
      return Arrays.hashCode((byte[]) obj);
    } else if (boolean.class.equals(componentType)) {
      return Arrays.hashCode((boolean[]) obj);
    } else if (float.class.equals(componentType)) {
      return Arrays.hashCode((float[]) obj);
    } else if (double.class.equals(componentType)) {
      return Arrays.hashCode((double[]) obj);
    } else {
      return Arrays.hashCode((Object[]) obj);
    }
  }

  /**
   * Compares two objects. Returns true if
   * 
    *
  • {@code thisObj} and {@code thatObj} are both null
  • *
  • {@code thisObj} and {@code thatObj} are instances of the same type and * {@link Object#equals(Object)} returns true
  • *
  • {@code thisObj} and {@code thatObj} are arrays with the same component type and * equals() method of {@link Arrays} returns true (not deepEquals())
  • *
* * @param thisObj * The left hand object to compare. May be an array or null * @param thatObj * The right hand object to compare. May be an array or null * @return true if two objects are equal; false otherwise. */ public static boolean equals(Object thisObj, Object thatObj) { if (thisObj == null) { return thatObj == null; } else if (thatObj == null) { return false; } final Class clazz = thisObj.getClass(); if (!clazz.equals(thatObj.getClass())) { return false; } if (!clazz.isArray()) { return thisObj.equals(thatObj); } final Class componentType = clazz.getComponentType(); if (long.class.equals(componentType)) { return Arrays.equals((long[]) thisObj, (long[]) thatObj); } else if (int.class.equals(componentType)) { return Arrays.equals((int[]) thisObj, (int[]) thatObj); } else if (short.class.equals(componentType)) { return Arrays.equals((short[]) thisObj, (short[]) thatObj); } else if (char.class.equals(componentType)) { return Arrays.equals((char[]) thisObj, (char[]) thatObj); } else if (byte.class.equals(componentType)) { return Arrays.equals((byte[]) thisObj, (byte[]) thatObj); } else if (boolean.class.equals(componentType)) { return Arrays.equals((boolean[]) thisObj, (boolean[]) thatObj); } else if (float.class.equals(componentType)) { return Arrays.equals((float[]) thisObj, (float[]) thatObj); } else if (double.class.equals(componentType)) { return Arrays.equals((double[]) thisObj, (double[]) thatObj); } else { return Arrays.equals((Object[]) thisObj, (Object[]) thatObj); } } /** * If the {@code obj} is an array, toString() method of {@link Arrays} is called. Otherwise * {@link Object#toString()} is called. Returns "null" if {@code obj} is null. * * @param obj * An object. May be an array or null. * @return String representation of the {@code obj}. */ public static String toString(Object obj) { if (obj == null) { return "null"; } final Class clazz = obj.getClass(); if (!clazz.isArray()) { return obj.toString(); } final Class componentType = obj.getClass().getComponentType(); if (long.class.equals(componentType)) { return Arrays.toString((long[]) obj); } else if (int.class.equals(componentType)) { return Arrays.toString((int[]) obj); } else if (short.class.equals(componentType)) { return Arrays.toString((short[]) obj); } else if (char.class.equals(componentType)) { return Arrays.toString((char[]) obj); } else if (byte.class.equals(componentType)) { return Arrays.toString((byte[]) obj); } else if (boolean.class.equals(componentType)) { return Arrays.toString((boolean[]) obj); } else if (float.class.equals(componentType)) { return Arrays.toString((float[]) obj); } else if (double.class.equals(componentType)) { return Arrays.toString((double[]) obj); } else { return Arrays.toString((Object[]) obj); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy