src/core/net/sf/basedb/util/EqualsHelper.java

Code
Comments
Other
Rev Date Author Line
5278 23 Mar 10 nicklas 1 /*
5278 23 Mar 10 nicklas 2   $Id $
5278 23 Mar 10 nicklas 3
5278 23 Mar 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5278 23 Mar 10 nicklas 5
5278 23 Mar 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5278 23 Mar 10 nicklas 7   Available at http://base.thep.lu.se/
5278 23 Mar 10 nicklas 8
5278 23 Mar 10 nicklas 9   BASE is free software; you can redistribute it and/or
5278 23 Mar 10 nicklas 10   modify it under the terms of the GNU General Public License
5278 23 Mar 10 nicklas 11   as published by the Free Software Foundation; either version 3
5278 23 Mar 10 nicklas 12   of the License, or (at your option) any later version.
5278 23 Mar 10 nicklas 13
5278 23 Mar 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5278 23 Mar 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5278 23 Mar 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5278 23 Mar 10 nicklas 17   GNU General Public License for more details.
5278 23 Mar 10 nicklas 18
5278 23 Mar 10 nicklas 19   You should have received a copy of the GNU General Public License
5278 23 Mar 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5278 23 Mar 10 nicklas 21 */
5278 23 Mar 10 nicklas 22 package net.sf.basedb.util;
5278 23 Mar 10 nicklas 23
7120 15 Apr 16 nicklas 24 import java.util.List;
7120 15 Apr 16 nicklas 25
5278 23 Mar 10 nicklas 26 /**
5278 23 Mar 10 nicklas 27   Helper class for implementing equals and hash code calculations in
5278 23 Mar 10 nicklas 28   classes.
5278 23 Mar 10 nicklas 29   @author nicklas
5278 23 Mar 10 nicklas 30   @since 2.15
5278 23 Mar 10 nicklas 31 */
5278 23 Mar 10 nicklas 32 public class EqualsHelper
5278 23 Mar 10 nicklas 33 {
5278 23 Mar 10 nicklas 34
5278 23 Mar 10 nicklas 35   /**
5278 23 Mar 10 nicklas 36     Check if two objects are equal according to their equals
5278 23 Mar 10 nicklas 37     implementation. This method is safe to use with null values
5278 23 Mar 10 nicklas 38     and returns true if both objects are null. Otherwise,
5278 23 Mar 10 nicklas 39     it calls o1.equals(o2).
5278 23 Mar 10 nicklas 40     
5278 23 Mar 10 nicklas 41     @param o1 The first object
5278 23 Mar 10 nicklas 42     @param o2 The second object
5278 23 Mar 10 nicklas 43     @return TRUE if the objects are equal
5278 23 Mar 10 nicklas 44   */
5278 23 Mar 10 nicklas 45   public static boolean equals(Object o1, Object o2)
5278 23 Mar 10 nicklas 46   {
5278 23 Mar 10 nicklas 47     return o1 == o2 || (o1 != null && o1.equals(o2));
5278 23 Mar 10 nicklas 48   }
5278 23 Mar 10 nicklas 49   
5278 23 Mar 10 nicklas 50   /**
5278 23 Mar 10 nicklas 51     Get the hashcode for an object. This method is safe to use
5278 23 Mar 10 nicklas 52     with null and returns 0.
5278 23 Mar 10 nicklas 53     @param o The object to get the hash code for
5278 23 Mar 10 nicklas 54     @return The hash code
5278 23 Mar 10 nicklas 55   */
5278 23 Mar 10 nicklas 56   public static int hashCode(Object o)
5278 23 Mar 10 nicklas 57   {
5278 23 Mar 10 nicklas 58     return o == null ? 0 : o.hashCode();
5278 23 Mar 10 nicklas 59   }
5278 23 Mar 10 nicklas 60   
5320 21 Apr 10 nicklas 61   /**
5320 21 Apr 10 nicklas 62     Hashcode calculation for an array of objects that doesn't
5320 21 Apr 10 nicklas 63     depend on the position in the array. Null objects are 
5320 21 Apr 10 nicklas 64     ignored.
5320 21 Apr 10 nicklas 65     
5320 21 Apr 10 nicklas 66     @param objects An array of objects
5320 21 Apr 10 nicklas 67     @return The combined hash code for objects in the array
5320 21 Apr 10 nicklas 68     @see #invariantEquals(Object[], Object[])
5320 21 Apr 10 nicklas 69   */
6125 14 Sep 12 nicklas 70   @SafeVarargs
5320 21 Apr 10 nicklas 71   public static <T> int hashCode(T... objects)
5320 21 Apr 10 nicklas 72   {
5320 21 Apr 10 nicklas 73     if (objects == null) return 0;
5320 21 Apr 10 nicklas 74     int hashCode = 0;
5320 21 Apr 10 nicklas 75     for (T o : objects)
5320 21 Apr 10 nicklas 76     {
5320 21 Apr 10 nicklas 77       if (o != null) hashCode += o.hashCode();
5320 21 Apr 10 nicklas 78     }
5320 21 Apr 10 nicklas 79     if (hashCode == 0) hashCode = -1;
5320 21 Apr 10 nicklas 80     return hashCode;
5320 21 Apr 10 nicklas 81   }
5320 21 Apr 10 nicklas 82   
5320 21 Apr 10 nicklas 83   /**
5320 21 Apr 10 nicklas 84     Checks if two arrays contains equal elements disregarding
5320 21 Apr 10 nicklas 85     their positions in the array. Eg. [1, 2] is equal to [2, 1]
5320 21 Apr 10 nicklas 86     @param a1 The first array
5320 21 Apr 10 nicklas 87     @param a2 The second array
5320 21 Apr 10 nicklas 88     @return TRUE if both arrays contains the same elements
5320 21 Apr 10 nicklas 89   */
5320 21 Apr 10 nicklas 90   public static <T> boolean invariantEquals(T[] a1, T[] a2)
5320 21 Apr 10 nicklas 91   {
5320 21 Apr 10 nicklas 92     if (a1 == a2) return true;
5320 21 Apr 10 nicklas 93     if (a1 == null || a2 == null) return false;
5320 21 Apr 10 nicklas 94     if (a1.length != a2.length) return false;
5320 21 Apr 10 nicklas 95     boolean[] matched = new boolean[a2.length];
5320 21 Apr 10 nicklas 96     for (int i = 0; i < a1.length; ++i)
5320 21 Apr 10 nicklas 97     {
5320 21 Apr 10 nicklas 98       T e1 = a1[i];
5320 21 Apr 10 nicklas 99       boolean found = false;
5320 21 Apr 10 nicklas 100       for (int j = 0; j < a2.length; ++j)
5320 21 Apr 10 nicklas 101       {
5320 21 Apr 10 nicklas 102         if (!matched[j] && equals(e1, a2[j]))
5320 21 Apr 10 nicklas 103         {
5320 21 Apr 10 nicklas 104           matched[j] = true;
5320 21 Apr 10 nicklas 105           found = true;
5320 21 Apr 10 nicklas 106           break;
5320 21 Apr 10 nicklas 107         }
5320 21 Apr 10 nicklas 108       }
5320 21 Apr 10 nicklas 109       if (!found) return false;
5320 21 Apr 10 nicklas 110     }
5320 21 Apr 10 nicklas 111     return true;
5320 21 Apr 10 nicklas 112   }
5320 21 Apr 10 nicklas 113   
7120 15 Apr 16 nicklas 114   /**
7120 15 Apr 16 nicklas 115     Checks if two lists contains equal elements disregarding
7120 15 Apr 16 nicklas 116     their positions in the array. Eg. [1, 2] is equal to [2, 1]
7120 15 Apr 16 nicklas 117     @param l1 The first list
7120 15 Apr 16 nicklas 118     @param l2 The second list
7120 15 Apr 16 nicklas 119     @return TRUE if both list contains the same elements
7120 15 Apr 16 nicklas 120     @since 3.8
7120 15 Apr 16 nicklas 121   */
7120 15 Apr 16 nicklas 122   public static boolean invariantEquals(List<?> l1, List<?> l2)
7120 15 Apr 16 nicklas 123   {
7120 15 Apr 16 nicklas 124     if (l1 == l2) return true;
7120 15 Apr 16 nicklas 125     if (l1 == null || l2 == null) return false;
7120 15 Apr 16 nicklas 126     if (l1.size() != l2.size()) return false;
7120 15 Apr 16 nicklas 127     
7120 15 Apr 16 nicklas 128     boolean[] matched = new boolean[l2.size()];
7120 15 Apr 16 nicklas 129     for (Object o1 : l1)
7120 15 Apr 16 nicklas 130     {
7120 15 Apr 16 nicklas 131       boolean found = false;
7120 15 Apr 16 nicklas 132       for (int j = 0; j < l2.size(); ++j)
7120 15 Apr 16 nicklas 133       {
7120 15 Apr 16 nicklas 134         if (!matched[j] && equals(o1, l2.get(j)))
7120 15 Apr 16 nicklas 135         {
7120 15 Apr 16 nicklas 136           matched[j] = true;
7120 15 Apr 16 nicklas 137           found = true;
7120 15 Apr 16 nicklas 138           break;
7120 15 Apr 16 nicklas 139         }
7120 15 Apr 16 nicklas 140       }
7120 15 Apr 16 nicklas 141       if (!found) return false;
7120 15 Apr 16 nicklas 142     }
7120 15 Apr 16 nicklas 143     return true;
7120 15 Apr 16 nicklas 144   }
7120 15 Apr 16 nicklas 145
7120 15 Apr 16 nicklas 146   
5278 23 Mar 10 nicklas 147 }