From 392bb663003a2255627307937cb3b0dc31eb03bc Mon Sep 17 00:00:00 2001 From: Ties de Kock Date: Mon, 28 Nov 2022 07:54:54 +0100 Subject: [PATCH] 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 bb93816..a953578 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("");