1 package de.dlr.shepard.util;
2
3 import java.util.List;
4
5 import com.fasterxml.jackson.annotation.JsonIgnore;
6
7 @FunctionalInterface
8 public interface HasId {
9
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. These lists are equal if both
20 * are sorted in the same way and each object is equal when compared by its
21 * unique ID. Other attributes are ignored.
22 *
23 * @param a The first list
24 * @param b The second list
25 * @return True of both lists are equal
26 */
27 static boolean equalsHelper(List<? extends HasId> a, List<? extends HasId> b) {
28 if (a == null && b == null)
29 return true;
30 if (a == null || b == null)
31 return false;
32 if (a.size() != b.size())
33 return false;
34 // TODO: Should we sort these lists?
35 for (int i = 0; i < a.size(); i++) {
36 if (!equalsHelper(a.get(i), b.get(i)))
37 return false;
38 }
39 return true;
40 }
41
42 /**
43 * This function compares two objects. These objects are equal if their unique
44 * ID is equal. Other attributes are ignored.
45 *
46 * @param a The first object
47 * @param b The second object
48 * @return True if both objects are equal
49 */
50 static boolean equalsHelper(HasId a, HasId b) {
51 if (a == null && b == null)
52 return true;
53 if (a == null || b == null)
54 return false;
55 return a.getUniqueId().equals(b.getUniqueId());
56 }
57
58 /**
59 * This function calculates the hash code of a list of objects. Only the unique
60 * ID is included in the calculation.
61 *
62 * @param a The list of objects
63 * @return The calculated hash code
64 */
65 static int hashcodeHelper(List<? extends HasId> a) {
66 if (a == null)
67 return 0;
68 final int prime = 31;
69 int result = 1;
70 for (HasId element : a) {
71 result = prime * result + hashcodeHelper(element);
72 }
73 return result;
74 }
75
76 /**
77 * This function calculates the hash code of a object. Only the unique ID is
78 * included in the calculation.
79 *
80 * @param a The object
81 * @return The calculated hash code
82 */
83 static int hashcodeHelper(HasId a) {
84 if (a == null)
85 return 0;
86 return a.getUniqueId().hashCode();
87 }
88
89 }