I'm trying to write a method that returns a char value representing the class of an IPv4 based on the first octet of the IP in decimal, but I think using too many 'if' for that is a waste of performance, specially when checking a lot of IP's.
Here's the code:
// Devuelve un char ('a', 'b', 'c', 'd' o 'e') representando la clase de la IPv4
public static char getIPv4Class(String ip) {
// Obtiene el primer octeto de la IP en decimal y lo parsea a entero
int octeto1 = Integer.parseInt(ip.substring(0, ip.indexOf(".")));
if (octeto1 >= 0 && octeto1 <= 127) {
return 'a';
} else if (octeto1 >= 128 && octeto1 <= 191) {
return 'b';
} else if (octeto1 >= 192 && octeto1 <= 223) {
return 'c';
} else if (octeto1 >= 224 && octeto1 <= 239) {
return 'd';
} else {
return ('e');
}
}
Also, even if the IP is validated in another method and the first octet cannot be more than 255 in decimal, the 'E' class only goes to 247.255.255.255, but I can't return null if the first octet is greather than 247.
Aucun commentaire:
Enregistrer un commentaire