From 98a38454b675b59587d66dae693222f5eb36c2df Mon Sep 17 00:00:00 2001 From: Ties de Kock Date: Mon, 28 Nov 2022 07:45:17 +0100 Subject: [PATCH 1/2] Rename difference -> minus --- .../net/ripe/ipresource/ImmutableResourceSet.java | 14 ++++++++++++++ .../ripe/ipresource/ImmutableResourceSetTest.java | 13 ++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java b/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java index 7c1e667..63584dc 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 { diff --git a/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java b/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java index 6a57940..c5fff32 100644 --- a/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java +++ b/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java @@ -199,6 +199,13 @@ 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_intersection() { ImmutableResourceSet empty = ImmutableResourceSet.parse(""); @@ -335,9 +342,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))); } } From 81b98cd28865de9ac3d5206b6498e0ae1e1d65e2 Mon Sep 17 00:00:00 2001 From: Ties de Kock Date: Mon, 28 Nov 2022 07:54:54 +0100 Subject: [PATCH 2/2] Add symmetricDifference to ImmutableResourceSet --- .../java/net/ripe/ipresource/ImmutableResourceSet.java | 8 ++++++++ .../net/ripe/ipresource/ImmutableResourceSetTest.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java b/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java index 63584dc..a050f34 100644 --- a/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java +++ b/src/main/java/net/ripe/ipresource/ImmutableResourceSet.java @@ -195,6 +195,14 @@ public ImmutableResourceSet minus(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 c5fff32..518b71f 100644 --- a/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java +++ b/src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java @@ -206,6 +206,14 @@ public void test_minus() { 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("");