Skip to content

Commit

Permalink
ranges and subnets that contain the exact same addresses should be equal
Browse files Browse the repository at this point in the history
  • Loading branch information
maltalex committed Jul 2, 2019
1 parent 9e29529 commit 4ab0c75
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/main/java/com/github/maltalex/ineter/range/IPRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public abstract class IPRange<T extends IPAddress & Comparable<T>> implements It

private static final long serialVersionUID = 1L;

protected static <T> T parseRange(String from, BiFunction<String, String, ? extends T> rangeProducer, Function<String, ? extends T> subnetProducer) {
protected static <T> T parseRange(String from, BiFunction<String, String, ? extends T> rangeProducer,
Function<String, ? extends T> subnetProducer) {
String[] parts = from.split("-");
if (parts.length == 2) {
return rangeProducer.apply(parts[0].trim(), parts[1].trim());
Expand All @@ -29,18 +30,21 @@ protected static <T> T parseRange(String from, BiFunction<String, String, ? exte
}
return rangeProducer.apply(parts[0].trim(), parts[0].trim());
} else {
throw new IllegalArgumentException(String.format("Inappropriate format for address range string %s.", from));
throw new IllegalArgumentException(
String.format("Inappropriate format for address range string %s.", from));
}
}

protected static <T> T parseSubnet(String from, BiFunction<String, Integer, ? extends T> subnetProducer, int singleAddressMask) {
protected static <T> T parseSubnet(String from, BiFunction<String, Integer, ? extends T> subnetProducer,
int singleAddressMask) {
final String[] parts = from.split("/");
if (parts.length == 2) {
return subnetProducer.apply(parts[0].trim(), Integer.parseInt(parts[1].trim()));
} else if (parts.length == 1) {
return subnetProducer.apply(parts[0].trim(), singleAddressMask);
} else {
throw new IllegalArgumentException(String.format("Inappropriate format for address subnet string %s.", from));
throw new IllegalArgumentException(
String.format("Inappropriate format for address subnet string %s.", from));
}
}

Expand All @@ -53,7 +57,8 @@ protected static <T> T parseSubnet(String from, BiFunction<String, Integer, ? ex
* range. To check whether all addresses are contained, use
* {@link IPRange#contains(IPRange)}
*
* @param range the range to check for overlap
* @param range
* the range to check for overlap
* @return true if the given range overlaps with this one
*/
public boolean overlaps(IPRange<T> range) {
Expand All @@ -77,7 +82,8 @@ public boolean contains(T ip) {
* Checks whether this range contains all addresses of a given range. To
* check for partial overlap, use {@link IPRange#overlaps(IPRange)}
*
* @param range range to check
* @param range
* range to check
* @return true if the entire given range is contained within this range
*/
public boolean contains(IPRange<T> range) {
Expand All @@ -101,7 +107,7 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (getClass() != obj.getClass()) {
if (!(obj instanceof IPRange)) {
return false;
}
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -130,7 +136,8 @@ public Iterator<T> iterator() {
* Returns an iterator that optionally skips both the first and last
* addresses in the range
*
* @param trim set to true to skip first and last addresses
* @param trim
* set to true to skip first and last addresses
* @return a new iterator instance
*/
public Iterator<T> iterator(boolean trim) {
Expand All @@ -141,8 +148,10 @@ public Iterator<T> iterator(boolean trim) {
* Returns an iterator that optionally skips the first, last or both
* addresses in the range
*
* @param skipFirst set to true to skip the first address
* @param skipLast set to true to skip the last addresses
* @param skipFirst
* set to true to skip the first address
* @param skipLast
* set to true to skip the last addresses
* @return a new iterator instance
*/
public abstract Iterator<T> iterator(boolean skipFirst, boolean skipLast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void unequalToObject() {
assertFalse(IPv4Subnet.of("1.2.3.0/24").equals(new Object()));
}

@Test
void equalToRangeWithSameAddresses() {
IPv4Subnet subnet1 = IPv4Subnet.of("192.168.1.0/24");
IPv4Range subnet2 = IPv4Range.parse("192.168.1.0-192.168.1.255");
assertEquals(subnet1, subnet2);
assertEquals(subnet2, subnet1);
assertEquals(subnet1.hashCode(), subnet2.hashCode());
}

@Test
void parseCidr() {
final String cidr = "192.168.0.0/24";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void unequalToObject() {
assertFalse(IPv6Subnet.of("::/24").equals(new Object()));
}

@Test
void equalToRangeWithSameAddresses() {
IPv6Subnet subnet1 = IPv6Subnet.of("1234::/16");
IPv6Range subnet2 = IPv6Range.parse("1234::-1234:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertEquals(subnet1, subnet2);
assertEquals(subnet2, subnet1);
assertEquals(subnet1.hashCode(), subnet2.hashCode());
}

@Test
void parseSingleAddress() {
final String address = "1234::";
Expand Down

0 comments on commit 4ab0c75

Please sign in to comment.