toSet(T[] array) {
+ return Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new));
+ }
+
+ /**
+ * Subtracts from set A all elements in common with those in set B.
*
* [0,1,2], [1,2,3] -> [0]
*/
- public static Set subtract(Set a, Set b) {
- Set res = new LinkedHashSet<>(a);
+ public static Set subtract(Set a, Set b) {
+ Set res = new LinkedHashSet<>(a);
res.removeAll(b);
return res;
}
@@ -36,10 +117,35 @@ public static Set subtract(Set a, Set b) {
*
* [0,1,2], [1,2,3] -> [0,3]
*/
- public static Set disjoint(Set a, Set b) {
- Set res = new LinkedHashSet<>();
+ public static Set disjoint(Set a, Set b) {
+ Set res = new LinkedHashSet<>();
res.addAll(subtract(a, b));
res.addAll(subtract(b, a));
return res;
}
+
+ /**
+ * Returns the delta between an initial set A and final set B.
+ *
+ * [0,1,2], [1,2,3] -> added [3] & removed [0]
+ */
+ public static Delta delta(Set a, Set b) {
+ return new Delta<>(subtract(b, a), subtract(a, b));
+ }
+
+ public static class Delta {
+
+ public Set added;
+ public Set rmved;
+
+ public Delta(Set added, Set rmved) {
+ this.added = added;
+ this.rmved = rmved;
+ }
+
+ /** Returns {@code true} if both added and removed sets are empty. */
+ public boolean isEmpty() {
+ return added.isEmpty() & rmved.isEmpty();
+ }
+ }
}