View Javadoc
1   package de.dlr.shepard.common.util;
2   
3   import com.fasterxml.jackson.annotation.JsonIgnore;
4   import java.util.HashSet;
5   import java.util.List;
6   import java.util.Objects;
7   
8   @FunctionalInterface
9   public interface HasId {
10    /**
11     * Returns a specific unique identifier for this object
12     *
13     * @return String of the unique identifier
14     */
15    @JsonIgnore
16    String getUniqueId();
17  
18    /**
19     * This function compares two lists of objects.
20     * These lists are considered equal iff the sets of their unique Ids are identical.
21     * In particular, this does not take the order of the elements and their multiplicity into account.
22     *
23     * @param a The first list
24     * @param b The second list
25     * @return True iff both lists are equal by means described above
26     */
27    static boolean areEqualSetsByUniqueId(List<? extends HasId> a, List<? extends HasId> b) {
28      if (a == null && b == null) return true;
29      if (a == null || b == null) return false;
30      HashSet<String> IdSetA = new HashSet<String>();
31      HashSet<String> IdSetB = new HashSet<String>();
32      boolean hasNullA = false;
33      boolean hasNullB = false;
34      for (int i = 0; i < a.size(); i++) {
35        if (a.get(i) != null) IdSetA.add(a.get(i).getUniqueId());
36        else hasNullA = true;
37      }
38      for (int i = 0; i < b.size(); i++) {
39        if (b.get(i) != null) IdSetB.add(b.get(i).getUniqueId());
40        else hasNullB = true;
41      }
42      if ((hasNullA && !hasNullB) || (!hasNullA && hasNullB)) return false;
43      return IdSetA.equals(IdSetB);
44    }
45  
46    static boolean areEqualSets(long[] firstArray, long[] secondArray) {
47      if (firstArray == null && secondArray == null) return true;
48      if (firstArray == null || secondArray == null) return false;
49      HashSet<Long> firstArraySet = new HashSet<Long>();
50      HashSet<Long> secondArraySet = new HashSet<Long>();
51      for (int i = 0; i < firstArray.length; i++) firstArraySet.add(firstArray[i]);
52      for (int i = 0; i < secondArray.length; i++) secondArraySet.add(secondArray[i]);
53      return firstArraySet.equals(secondArraySet);
54    }
55  
56    static boolean areEqualSets(String[] firstArray, String[] secondArray) {
57      if (firstArray == null && secondArray == null) return true;
58      if (firstArray == null || secondArray == null) return false;
59      HashSet<String> firstArraySet = new HashSet<String>();
60      HashSet<String> secondArraySet = new HashSet<String>();
61      for (int i = 0; i < firstArray.length; i++) firstArraySet.add(firstArray[i]);
62      for (int i = 0; i < secondArray.length; i++) secondArraySet.add(secondArray[i]);
63      return firstArraySet.equals(secondArraySet);
64    }
65  
66    /**
67     * This function compares two objects. These objects are equal if their unique
68     * ID is equal. Other attributes are ignored.
69     *
70     * @param a The first object
71     * @param b The second object
72     * @return True if both objects are equal
73     */
74    static boolean equalsHelper(HasId a, HasId b) {
75      if (a == null && b == null) return true;
76      if (a == null || b == null) return false;
77      return a.getUniqueId().equals(b.getUniqueId());
78    }
79  
80    /**
81     * This function calculates the hash code of a list of objects. Only the unique
82     * ID is included in the calculation.
83     *
84     * @param a The list of objects
85     * @return The calculated hash code
86     */
87    static int hashcodeHelper(List<? extends HasId> a) {
88      if (a == null) return 0;
89      final int prime = 31;
90      int result = 1;
91      for (HasId element : a) {
92        result = prime * result + hashcodeHelper(element);
93      }
94      return result;
95    }
96  
97    /**
98     * This function calculates the hash code of a object. Only the unique ID is
99     * included in the calculation.
100    *
101    * @param a The object
102    * @return The calculated hash code
103    */
104   static int hashcodeHelper(HasId a) {
105     if (a == null) return 0;
106     return a.getUniqueId().hashCode();
107   }
108 
109   static int hashcodeHelper(long[] array) {
110     if (array == null) return Objects.hash(array);
111     HashSet<Long> arrayAsHashSet = new HashSet<Long>();
112     for (int i = 0; i < array.length; i++) arrayAsHashSet.add(array[i]);
113     return arrayAsHashSet.hashCode();
114   }
115 
116   static int hashcodeHelper(String[] array) {
117     if (array == null) return Objects.hash(array);
118     HashSet<String> arrayAsHashSet = new HashSet<String>();
119     for (int i = 0; i < array.length; i++) arrayAsHashSet.add(array[i]);
120     return arrayAsHashSet.hashCode();
121   }
122 }