The following code will generate a sequence of N mac addresses based on the input from user. The input must be atleast one octet to generate the sequence , ie :
input : A0 , 5
output : A0:00:00:00:00:01 - A0:00:00:00:00:05
My actual code is working fine and I wanted to make it optimized if it could be :
public static List<String> getMacSequences(final String macAddress, final Integer count)
{
List<String> macList = new ArrayList<String>();
final List<Integer> macOctets = new ArrayList<Integer>();
String [] macSplit = macAddress.split(":");
int incPos = macAddress.split(":").length; // need to throw exception when the length is 0
if (incPos >= 1)
{
for (int bp=0; bp <6; bp++)
{
if (bp >= incPos)
{
macOctets.add(0);
}
else
{
macOctets.add(Integer.parseInt(macSplit[bp], 16));
}
}
for (int i = 0 ; i<count; i++)
{
int lastOctet = macOctets.get(5);
macOctets.set(5, ++lastOctet);
for (int j = 5; j >= 0; j--)
{
if (j == 0 && macOctets.get(j) > 255)
{
Collections.fill(macOctets, 0);
}
if (macOctets.get(j) > 255)
{
macOctets.set(j, 0);
Integer macValue = macOctets.get(j - 1);
macOctets.set(j - 1, ++macValue);
}
else{
break;
}
}
macList.add(String.format("%02X:%02X:%02X:%02X:%02X:%02X", macOctets.toArray()));
}
}
return macList;
}
I have done micro-benchmarking to determine the performance for generating 100 mac addresses in sequence and the result of average value for 10 sampling (100 itr/sampling) as follows (in ms) :
7.62 7.43 6.87 7.4 7.24 7.21 7.16 7.59 7.67 8.13
and the overall sampling average is : 7.432
ms