Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propose rename of set operation and add symmetric difference #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/net/ripe/ipresource/ImmutableResourceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public ImmutableResourceSet remove(IpResource value) {
return new Builder(this).remove(value).build();
}

/**
* @return $this \cup that$
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think \cup is more understandable than union :)

*/
public ImmutableResourceSet union(ImmutableResourceSet that) {
if (this.isEmpty()) {
return that;
Expand All @@ -145,6 +148,9 @@ public ImmutableResourceSet union(ImmutableResourceSet that) {
}
}

/**
* @return $this \cap that$
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

*/
public ImmutableResourceSet intersection(ImmutableResourceSet that) {
if (this.isEmpty()) {
return this;
Expand Down Expand Up @@ -173,14 +179,30 @@ 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 {
return new Builder(this).removeAll(that).build();
}
}

/**
* @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);
}
Expand Down
21 changes: 18 additions & 3 deletions src/test/java/net/ripe/ipresource/ImmutableResourceSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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)));
}

}
Expand Down