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

org.infinispan.commons.equivalence.AnyEquivalence Maven / Gradle / Ivy

package org.infinispan.commons.equivalence;

/**
 * A compare function for objects.
 *
 * @author Galder Zamarreño
 * @since 5.3
 */
public final class AnyEquivalence implements Equivalence {

   private static AnyEquivalence OBJECT = new AnyEquivalence();

   public static AnyEquivalence STRING = getInstance(String.class);

   public static AnyEquivalence BYTE = getInstance(Byte.class);

   public static AnyEquivalence SHORT = getInstance(Short.class);

   public static AnyEquivalence INT = getInstance(Integer.class);

   public static AnyEquivalence LONG = getInstance(Long.class);

   public static AnyEquivalence DOUBLE  = getInstance(Double.class);

   public static AnyEquivalence FLOAT = getInstance(Float.class);

   public static AnyEquivalence BOOLEAN = getInstance(Boolean.class);

   // To avoid instantiation
   private AnyEquivalence() {
   }

   @Override
   public int hashCode(Object obj) {
      return obj == null ? 0 : obj.hashCode();
   }

   @Override
   public boolean equals(T obj, Object otherObj) {
      return obj != null && obj.equals(otherObj);
   }

   @Override
   public String toString(Object obj) {
      return String.valueOf(obj);
   }

   @Override
   public boolean isComparable(Object obj) {
      return obj instanceof Comparable;
   }

   @Override
   @SuppressWarnings("unchecked")
   public int compare(T obj, T otherObj) {
      return ((Comparable) obj).compareTo(otherObj);
   }

   @SuppressWarnings("unchecked")
   public static  AnyEquivalence getInstance() {
      return (AnyEquivalence) OBJECT;
   }

   @SuppressWarnings("unchecked")
   public static  AnyEquivalence getInstance(Class classType) {
      return (AnyEquivalence) OBJECT;
   }
}