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

org.fuwjin.util.ObjectUtils Maven / Gradle / Ivy

There is a newer version: 0.9.8.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2011 Michael Doberenz.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Michael Doberenz - initial API and implementation
 ******************************************************************************/
package org.fuwjin.util;

import java.util.Arrays;

/**
 * Utility methods for java.lang.Object methods.
 */
public abstract class ObjectUtils {
   /**
    * Assistant for overwriting Object methods efficiently.
    */
   public interface ObjectHelper {
      /**
       * Returns the identity of the object. The array returned must never be
       * altered.
       * @return the identity
       */
      Object[] identity();
   }

   /**
    * Returns a hash code appropriate for obj.
    * @param obj the object to hash
    * @return the hash code
    */
   public static int hash(final ObjectHelper obj) {
      return Arrays.hashCode(obj.identity());
   }

   /**
    * Returns true if obj and other are equal, false otherwise.
    * @param obj the object
    * @param other the test object
    * @return true if obj equals other, false otherwise
    */
   public static boolean isEqual(final ObjectHelper obj, final Object other) {
      if(obj == other) {
         return true;
      }
      return other != null && obj.getClass().equals(other.getClass())
            && Arrays.deepEquals(obj.identity(), ((ObjectHelper)other).identity());
   }

   /**
    * Returns a String suitable for debugging.
    * @param obj the object to stringify
    * @return the string
    */
   public static String toString(final ObjectHelper obj) {
      return obj.getClass().getCanonicalName() + Arrays.toString(obj.identity());
   }

   private ObjectUtils() {
      // utility class
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy