比尔 K 是对的。这是一个简单的示例类:
public class IPAddress {
private IPOctet octet1;
private IPOctet octet2;
private IPOctet octet3;
private IPOctet octet4;
private static final String NETWORK_MASK_CHAR = "/24";
public IPAddress(String value) throws Exception{
value = cutOffNetworkMask(value);
String[] octets = value.split("\\.");
if (octets.length < 4) throw new Exception("Invalid input value. Too few octets.");
octet1 = new IPOctet(octets[0]);
octet2 = new IPOctet(octets[1]);
octet3 = new IPOctet(octets[2]);
octet4 = new IPOctet(octets[3]);
}
public boolean hasMatchOf(IPAddress another){
if (!octet1.isMatching(another.getOctet1())) return false;
if (!octet2.isMatching(another.getOctet2())) return false;
if (!octet3.isMatching(another.getOctet3())) return false;
if (!octet4.isMatching(another.getOctet4())) return false;
return true;
}
public IPOctet getOctet1(){ return octet1; }
public IPOctet getOctet2(){ return octet2; }
public IPOctet getOctet3(){ return octet3; }
public IPOctet getOctet4(){ return octet4; }
private static String cutOffNetworkMask(String base){
int index = base.indexOf(NETWORK_MASK_CHAR);
try{
if (index > -1) base = base.substring(0, index);
}catch(IndexOutOfBoundsException ignored){}
return base;
}
}
IPOctet 类:
public class IPOctet {
private static final String RANGE_SYMBOL = "-";
int max = 255;
int min = 0;
public IPOctet(String stringValue) throws Exception {
int index = stringValue.indexOf(RANGE_SYMBOL);
if (index < 0){
// This octet is a single byte;
max = str2int(stringValue);
min = max;
}else{
// This octet has a range.
String[] values = stringValue.split(RANGE_SYMBOL);
if (values.length != 2) throw new Exception("Invalid Input. Range must be like xx-xx");
min = str2int(values[0]);
max = str2int(values[1]);
}
}
public boolean isMatching(IPOctet another) {
if (max < another.getMinValue()) return false;
if (min > another.getMaxValue()) return false;
return true;
}
public int getMinValue(){ return min; }
public int getMaxValue(){ return min; }
private static int str2int(String input) throws Exception{
int result;
try{
result = Integer.parseInt(input);
}catch(NumberFormatException nfe){
throw new Exception("Invalid input. Input is not a number.");
}
validate(result);
return result;
}
private static void validate(int input) throws Exception{
if (input < 0 || input > 255) throw new Exception("Invalid input. Must be between 0 and 255");
}
}
这里有一个简单的测试用例:
public class Main {
public static void main(String[] args){
try{
IPAddress adr1 = new IPAddress("192.168.1.0-255");
IPAddress adr2 = new IPAddress("192.168.1.1/24");
IPAddress adr3 = new IPAddress("1.2.3.4");
System.out.println("adr2 matches adr1: " + adr1.hasMatchOf(adr2));
System.out.println("adr3 matches adr1: " + adr1.hasMatchOf(adr3));
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}
有了它,现在您可以将IPAddress
es 存储在 a ArrayList
or中LinkedList
:
ArrayList<IPAddress> addresses = new ArrayList<>();
addresses.add(new IPAddress("8.8.8.8"));
addresses.add(new IPAddress("198.252.206.16"));
IPAddress mask = new IPAddress("20-200.200-255.100-255.0-255");
for (IPAddress addr : addresses){
if (mask.hasMatchOf(addr)){
...
}else{
..
}
}