From bc2ecc9f2fa153fff86332d57ea3eca28beb9436 Mon Sep 17 00:00:00 2001 From: maltalex Date: Fri, 18 Aug 2023 21:46:15 -0400 Subject: [PATCH] withRemoved bugfix, tests, version bump --- publish.gradle | 2 +- src/main/java/com/github/maltalex/ineter/range/IPv4Range.java | 3 +++ src/main/java/com/github/maltalex/ineter/range/IPv6Range.java | 3 +++ .../java/com/github/maltalex/ineter/range/IPv4RangeTest.java | 4 ++-- .../java/com/github/maltalex/ineter/range/IPv6RangeTest.java | 4 ++-- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/publish.gradle b/publish.gradle index 53dca1c..2c0071a 100644 --- a/publish.gradle +++ b/publish.gradle @@ -3,7 +3,7 @@ apply plugin: 'signing' group = 'com.github.maltalex' archivesBaseName = 'ineter' -version = '0.3.0' +version = '0.3.1' task javadocJar(type: Jar) { classifier = 'javadoc' diff --git a/src/main/java/com/github/maltalex/ineter/range/IPv4Range.java b/src/main/java/com/github/maltalex/ineter/range/IPv4Range.java index b2d61d2..c6583fd 100644 --- a/src/main/java/com/github/maltalex/ineter/range/IPv4Range.java +++ b/src/main/java/com/github/maltalex/ineter/range/IPv4Range.java @@ -224,6 +224,9 @@ public List withRemoved(Collection ranges) { List merged = IPv4Range.merge(ranges); ret.add(this); for (IPv4Range toRemove : merged) { + if (ret.isEmpty()) { + break; + } IPv4Range next = ret.remove(ret.size() - 1); // a bit faster than calling withRemoved() one range at a time if (toRemove.getFirst().compareTo(next.getFirst()) > 0) { diff --git a/src/main/java/com/github/maltalex/ineter/range/IPv6Range.java b/src/main/java/com/github/maltalex/ineter/range/IPv6Range.java index 9783046..13dd829 100644 --- a/src/main/java/com/github/maltalex/ineter/range/IPv6Range.java +++ b/src/main/java/com/github/maltalex/ineter/range/IPv6Range.java @@ -253,6 +253,9 @@ public List withRemoved(Collection ranges) { List merged = IPv6Range.merge(ranges); ret.add(this); for (IPv6Range toRemove : merged) { + if (ret.isEmpty()) { + break; + } IPv6Range next = ret.remove(ret.size() - 1); // a bit faster than calling withRemoved() one range at a time if (toRemove.getFirst().compareTo(next.getFirst()) > 0) { diff --git a/src/test/java/com/github/maltalex/ineter/range/IPv4RangeTest.java b/src/test/java/com/github/maltalex/ineter/range/IPv4RangeTest.java index 6040b0e..aace65f 100644 --- a/src/test/java/com/github/maltalex/ineter/range/IPv4RangeTest.java +++ b/src/test/java/com/github/maltalex/ineter/range/IPv4RangeTest.java @@ -393,8 +393,8 @@ void withRemovedCollectionEmpty() { IPv4Range.parse("10.0.0.0/24").withRemoved(Arrays.asList(IPv4Range.parse("10.0.0.0/24")))); assertEquals(emptyList(), IPv4Range.parse("10.0.0.0/24").withRemoved(Arrays.asList(IPv4Range.parse("0.0.0.0/0")))); - assertEquals(emptyList(), - IPv4Range.parse("10.0.0.0/24").withRemoved(Arrays.asList(IPv4Range.parse("9.0.0.0-11.0.0.0")))); + assertEquals(emptyList(), IPv4Range.parse("10.0.0.0/24") + .withRemoved(Arrays.asList(IPv4Range.parse("9.0.0.0-11.0.0.0"), IPv4Range.parse("13.0.0.0/24")))); } @ParameterizedTest diff --git a/src/test/java/com/github/maltalex/ineter/range/IPv6RangeTest.java b/src/test/java/com/github/maltalex/ineter/range/IPv6RangeTest.java index 6a61869..5cb633b 100644 --- a/src/test/java/com/github/maltalex/ineter/range/IPv6RangeTest.java +++ b/src/test/java/com/github/maltalex/ineter/range/IPv6RangeTest.java @@ -388,8 +388,8 @@ void withRemovedCollectionEmpty() { assertEquals(emptyList(), IPv6Range.parse("1234::/16").withRemoved(Arrays.asList(IPv6Range.parse("1234::/16")))); assertEquals(emptyList(), IPv6Range.parse("1234::/16").withRemoved(Arrays.asList(IPv6Range.parse("::/0")))); - assertEquals(emptyList(), - IPv6Range.parse("1234::/16").withRemoved(Arrays.asList(IPv6Range.parse("1230::-1240::")))); + assertEquals(emptyList(), IPv6Range.parse("1234::/16") + .withRemoved(Arrays.asList(IPv6Range.parse("1230::-1240::"), IPv6Range.parse("2222::0/16")))); } @ParameterizedTest