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
12
13
14
15 @JsonIgnore
16 String getUniqueId();
17
18
19
20
21
22
23
24
25
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
68
69
70
71
72
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
82
83
84
85
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
99
100
101
102
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 }