Skip to content

Commit

Permalink
Merge pull request #298 from Lixiongyou/master
Browse files Browse the repository at this point in the history
Add SubnetAddressIterable & SubnetAddressIterator
  • Loading branch information
garydgregory authored Oct 13, 2024
2 parents 42348a5 + a75f769 commit e6db671
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/main/java/org/apache/commons/net/util/SubnetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.commons.net.util;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -223,6 +225,54 @@ public String toString() {
}
}

/**
* Allows an object to be the target of the "for-each loop" statement for a SubnetInfo.
*
* @since 3.12.0
*/
public static class SubnetAddressIterable implements Iterable<String> {

private final SubnetInfo subnetInfo;

public SubnetAddressIterable(final SubnetInfo subnetInfo) {
this.subnetInfo = subnetInfo;
}

@Override
public Iterator<String> iterator() {
return new SubnetAddressIterator(subnetInfo);
}
}

/**
* Iterates over a SubnetInfo.
*
* @since 3.12.0
*/
public static class SubnetAddressIterator implements Iterator<String> {

private final SubnetInfo subnetInfo;

private final AtomicInteger currentAddress;

public SubnetAddressIterator(final SubnetInfo subnetInfo) {
this.subnetInfo = subnetInfo;
currentAddress = new AtomicInteger(subnetInfo.low());
}

@Override
public boolean hasNext() {
return subnetInfo.getAddressCount() > 0 && currentAddress.get() <= subnetInfo.high();
}

@Override
public String next() {
String address = subnetInfo.format(subnetInfo.toArray(currentAddress.get()));
currentAddress.incrementAndGet();
return address;
}
}

private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})";
private static final String SLASH_FORMAT = IP_ADDRESS + "/(\\d{1,2})"; // 0 -> 32
private static final Pattern ADDRESS_PATTERN = Pattern.compile(IP_ADDRESS);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/apache/commons/net/SubnetUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("deprecation") // deliberate use of deprecated methods
public class SubnetUtilsTest {

Expand Down Expand Up @@ -407,6 +410,21 @@ public void testParseSimpleNetmaskInclusive() {
}
}

@Test
public void testSubnetAddressIterable() {
SubnetUtils utils = new SubnetUtils("192.168.1.0/24");
List<String> addressList = new ArrayList<>();
for (String address : new SubnetUtils.SubnetAddressIterable(utils.getInfo())) {
addressList.add(address);
}
assertEquals(254, addressList.size());
assertFalse(addressList.contains("192.168.1.0"));
assertTrue(addressList.contains("192.168.1.1"));
assertTrue(addressList.contains("192.168.1.127"));
assertTrue(addressList.contains("192.168.1.254"));
assertFalse(addressList.contains("192.168.1.255"));
}

@Test
public void testPrevious() {
final SubnetUtils utils = new SubnetUtils("192.168.0.1/29");
Expand Down

0 comments on commit e6db671

Please sign in to comment.