diff --git a/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java b/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java index 7c1e667..a050f34 100644 --- a/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java +++ b/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java @@ -133,6 +133,9 @@ public ImmutableResourceSet remove(IpResource value) { return new Builder(this).remove(value).build(); } + /** + * @return $this \cup that$ + */ public ImmutableResourceSet union(ImmutableResourceSet that) { if (this.isEmpty()) { return that; @@ -145,6 +148,9 @@ public ImmutableResourceSet union(ImmutableResourceSet that) { } } + /** + * @return $this \cap that$ + */ public ImmutableResourceSet intersection(ImmutableResourceSet that) { if (this.isEmpty()) { return this; @@ -173,7 +179,15 @@ public ImmutableResourceSet intersection(ImmutableResourceSet that) { } } + @Deprecated public ImmutableResourceSet difference(ImmutableResourceSet that) { + return this.minus(that); + } + + /** + * @return $this \setminus that$ + */ + public ImmutableResourceSet minus(ImmutableResourceSet that) { if (!this.intersects(that)) { return this; } else { @@ -181,6 +195,14 @@ public ImmutableResourceSet difference(ImmutableResourceSet that) { } } + /** + * @return $this \Delta that$ + */ + public ImmutableResourceSet symmetricDifference(ImmutableResourceSet that) { + // $this \Delta that = (this \setminus that) \cup (that \setminus this)$ + return (this.minus(that)).union((that.minus(this))); + } + public ImmutableResourceSet complement() { return universal().difference(this); } diff --git a/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java b/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java index 6a57940..518b71f 100644 --- a/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java +++ b/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java @@ -199,6 +199,21 @@ public void test_difference() { assertEquals(ImmutableResourceSet.parse("AS3333-AS3334, AS3336-AS4444, 10.0.0.0-10.4.255.255, 10.6.0.0-10.255.255.255"), difference); } + @Test + public void test_minus() { + ImmutableResourceSet a = ImmutableResourceSet.parse("AS3333-AS4444,10.0.0.0/8"); + ImmutableResourceSet difference = a.minus(ImmutableResourceSet.parse("10.5.0.0/16, AS3335")); + assertEquals(ImmutableResourceSet.parse("AS3333-AS3334, AS3336-AS4444, 10.0.0.0-10.4.255.255, 10.6.0.0-10.255.255.255"), difference); + } + + @Test + public void test_symmetricDifference() { + ImmutableResourceSet a = ImmutableResourceSet.parse("AS64512-AS64513"); + ImmutableResourceSet b = ImmutableResourceSet.parse("AS64513-AS64514"); + ImmutableResourceSet symDiff = a.symmetricDifference(b); + assertEquals(ImmutableResourceSet.parse("AS64512, AS64514"), symDiff); + } + @Test public void test_intersection() { ImmutableResourceSet empty = ImmutableResourceSet.parse(""); @@ -335,9 +350,9 @@ public void difference_laws() { ImmutableResourceSet b = randomSet(i); ImmutableResourceSet c = randomSet(i); - assertEquals(c.difference(a.intersection(b)), (c.difference(a)).union(c.difference(b))); - assertEquals(c.difference(a.union(b)), (c.difference(a)).intersection(c.difference(b))); - assertEquals(c.difference(b.difference(a)), (a.intersection(c)).union(c.difference(b))); + assertEquals(c.minus(a.intersection(b)), (c.minus(a)).union(c.minus(b))); + assertEquals(c.minus(a.union(b)), (c.minus(a)).intersection(c.minus(b))); + assertEquals(c.minus(b.minus(a)), (a.intersection(c)).union(c.minus(b))); } }