Class ObjectUtil


  • public final class ObjectUtil
    extends Object
    utility methods for implementing the equals() and hashCode() methods of Objects. All credit for this class goes to Mark Davis (Java Report 5(1), 46; Java Report 5(4), 60).

    All equals() methods in this class take the two fields to compare for equality as arguments and return whether they are equal. Consequently, the equals() method of class AClass should be implemented (taking advantage of the equals() methods in this class) as follows:

     public boolean equals(Object o) {
       if (o == this) return true;
       // if this class AClass is a direct sub-class of Object:
       if (o == null) return false;
       if (!o.getClass().equals(this.getClass())) return false;
       // else if this class AClass extends another class than Object:
       //if (!super.equals(o)) return false;
       
       AClass that = (AClass) o;
       
       // only compare fields of this class AClass (not of super-classes):
       //if (!ObjectUtil.equals(this.field, that.field)) return false;
       
       // this and that are identical if we made it 'til here
       return true;
     }
     

    All hashCode() methods in this class take the current hashCode value as the first argument and the field to take into account as the second argument and return the newly calculated hashCode value (which now includes the influence of the given field). Consequently, the hashCode() method of class AClass should be implemented (taking advantage of the hashCode() methods in this class) as follows:

     public int hashCode() {
       // if this class AClass is a direct sub-class of Object:
       int hc = 0;
       // else if this class AClass extends another class than Object:
       //int hc = super.hashCode();
    
       // only take into account fields of this class AClass (not of super-class):
       //hc = ObjectUtil.hashCode(hc, field);
    
       return hc;
     }
     
    Author:
    Gerald Loeffler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int PRIME
      the current hashCode is always first multiplied with this prime before the hashCode value for a particular field is added.
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean equals​(boolean[] a1, boolean[] a2)  
      static boolean equals​(boolean b1, boolean b2)  
      static boolean equals​(double[] a1, double[] a2)  
      static boolean equals​(double d1, double d2)  
      static boolean equals​(float[] a1, float[] a2)  
      static boolean equals​(float f1, float f2)  
      static boolean equals​(int[] a1, int[] a2)  
      static boolean equals​(int i1, int i2)  
      static boolean equals​(long[] a1, long[] a2)  
      static boolean equals​(long l1, long l2)  
      static boolean equals​(Object[] a1, Object[] a2)  
      static boolean equals​(Object o1, Object o2)  
      static int hashCode​(int currentHashCodeValue, boolean b)  
      static int hashCode​(int currentHashCodeValue, boolean[] a)  
      static int hashCode​(int currentHashCodeValue, double d)  
      static int hashCode​(int currentHashCodeValue, double[] a)  
      static int hashCode​(int currentHashCodeValue, float f)  
      static int hashCode​(int currentHashCodeValue, float[] a)  
      static int hashCode​(int currentHashCodeValue, int i)  
      static int hashCode​(int currentHashCodeValue, int[] a)  
      static int hashCode​(int currentHashCodeValue, long l)  
      static int hashCode​(int currentHashCodeValue, long[] a)  
      static int hashCode​(int currentHashCodeValue, Object o)  
      static int hashCode​(int currentHashCodeValue, Object[] a)  
    • Field Detail

      • PRIME

        public static final int PRIME
        the current hashCode is always first multiplied with this prime before the hashCode value for a particular field is added.
        See Also:
        Constant Field Values
    • Method Detail

      • equals

        public static boolean equals​(boolean b1,
                                     boolean b2)
      • equals

        public static boolean equals​(int i1,
                                     int i2)
      • equals

        public static boolean equals​(long l1,
                                     long l2)
      • equals

        public static boolean equals​(float f1,
                                     float f2)
      • equals

        public static boolean equals​(double d1,
                                     double d2)
      • equals

        public static boolean equals​(boolean[] a1,
                                     boolean[] a2)
      • equals

        public static boolean equals​(int[] a1,
                                     int[] a2)
      • equals

        public static boolean equals​(long[] a1,
                                     long[] a2)
      • equals

        public static boolean equals​(float[] a1,
                                     float[] a2)
      • equals

        public static boolean equals​(double[] a1,
                                     double[] a2)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   boolean b)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   int i)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   long l)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   float f)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   double d)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   boolean[] a)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   int[] a)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   long[] a)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   float[] a)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   double[] a)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   Object[] a)
      • hashCode

        public static int hashCode​(int currentHashCodeValue,
                                   Object o)