This commit is contained in:
Looly 2023-02-09 19:20:54 +08:00
parent 8d98c049f2
commit 48c60ea8d6
2 changed files with 63 additions and 10 deletions

View File

@ -114,21 +114,41 @@ public class Ipv4Util {
* @return 区间地址 * @return 区间地址
*/ */
public static List<String> list(final String ipFrom, final String ipTo) { public static List<String> list(final String ipFrom, final String ipTo) {
final int[] ipf = Convert.convert(int[].class, StrUtil.splitToArray(ipFrom, CharUtil.DOT)); // 确定ip数量
final int[] ipt = Convert.convert(int[].class, StrUtil.splitToArray(ipTo, CharUtil.DOT)); final int count = countByIpRange(ipFrom, ipTo);
final int[] from = Convert.convert(int[].class, StrUtil.splitToArray(ipFrom, CharUtil.DOT));
final int[] to = Convert.convert(int[].class, StrUtil.splitToArray(ipTo, CharUtil.DOT));
final List<String> ips = new ArrayList<>(); final List<String> ips = new ArrayList<>(count);
for (int a = ipf[0]; a <= ipt[0]; a++) { // 是否是循环的第一个值
for (int b = (a == ipf[0] ? ipf[1] : 0); b <= (a == ipt[0] ? ipt[1] boolean aIsStart = true, bIsStart = true, cIsStart = true;
: 255); b++) { // 是否是循环的最后一个值
for (int c = (b == ipf[1] ? ipf[2] : 0); c <= (b == ipt[1] ? ipt[2] boolean aIsEnd, bIsEnd, cIsEnd;
: 255); c++) { // 循环的结束值
for (int d = (c == ipf[2] ? ipf[3] : 0); d <= (c == ipt[2] ? ipt[3] final int aEnd = to[0];
: 255); d++) { int bEnd;
int cEnd;
int dEnd;
for (int a = from[0]; a <= aEnd; a++) {
aIsEnd = (a == aEnd);
// 本次循环的结束结束值
bEnd = aIsEnd ? to[1] : 255;
for (int b = (aIsStart ? from[1] : 0); b <= bEnd; b++) {
// 在上一个循环是最后值的基础上进行判断
bIsEnd = aIsEnd && (b == bEnd);
cEnd = bIsEnd ? to[2] : 255;
for (int c = (bIsStart ? from[2] : 0); c <= cEnd; c++) {
// 在之前循环是最后值的基础上进行判断
cIsEnd = bIsEnd && (c == cEnd);
dEnd = cIsEnd ? to[3] : 255;
for (int d = (cIsStart ? from[3] : 0); d <= dEnd; d++) {
ips.add(a + "." + b + "." + c + "." + d); ips.add(a + "." + b + "." + c + "." + d);
} }
cIsStart = false;
} }
bIsStart = false;
} }
aIsStart = false;
} }
return ips; return ips;
} }

View File

@ -47,6 +47,31 @@ public class Ipv4UtilTest {
final int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0"); final int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0");
final List<String> list = Ipv4Util.list("192.168.100.2", maskBit, false); final List<String> list = Ipv4Util.list("192.168.100.2", maskBit, false);
Assert.assertEquals(254, list.size()); Assert.assertEquals(254, list.size());
testGenerateIpList("10.1.0.1", "10.2.1.2");
testGenerateIpList("10.2.1.1", "10.2.1.2");
testGenerateIpList("10.2.0.1", "10.2.1.2");
testGenerateIpList("10.1.0.1", "10.2.1.2");
testGenerateIpList("10.1.2.1", "10.2.1.2");
testGenerateIpList("10.2.1.2", "10.2.1.2");
testGenerateIpList("10.2.0.2", "10.2.1.2");
testGenerateIpList("10.1.1.2", "10.2.1.2");
testGenerateIpList("10.1.2.2", "10.2.1.2");
testGenerateIpList("10.2.0.3", "10.2.1.2");
testGenerateIpList("10.1.0.3", "10.2.1.2");
testGenerateIpList("10.1.1.3", "10.2.1.2");
testGenerateIpList("10.1.2.3", "10.2.1.2");
testGenerateIpList("9.255.2.1", "10.2.1.2");
testGenerateIpList("9.255.2.2", "10.2.1.2");
testGenerateIpList("9.255.2.3", "10.2.1.2");
testGenerateIpList("9.255.1.2", "10.2.1.2");
testGenerateIpList("9.255.0.2", "10.2.1.2");
testGenerateIpList("9.255.3.2", "10.2.1.2");
} }
@Test @Test
@ -86,4 +111,12 @@ public class Ipv4UtilTest {
l = Ipv4Util.ipv4ToLong("255.255.255.255"); l = Ipv4Util.ipv4ToLong("255.255.255.255");
Assert.assertEquals(4294967295L, l); Assert.assertEquals(4294967295L, l);
} }
@SuppressWarnings("SameParameterValue")
private void testGenerateIpList(final String fromIp, final String toIp) {
Assert.assertEquals(
Ipv4Util.countByIpRange(fromIp, toIp),
Ipv4Util.list(fromIp, toIp).size()
);
}
} }