throws ResourceRequestDeniedException {
final int needed;
final int len = macPrefix.length();
switch (len) {
case 0: throw new ResourceRequestDeniedException("prefix length 0?");
case 1: needed = 11; break;
case 2: needed = 10; break;
case 3: needed = 10; break;
case 4: needed = 9; break;
case 5: needed = 8; break;
case 6: needed = 8; break;
case 7: needed = 7; break;
case 8: needed = 6; break;
case 9: needed = 6; break;
case 10: needed = 5; break;
case 11: needed = 4; break;
case 12: needed = 4; break;
case 13: needed = 3; break;
case 14: needed = 2; break;
case 15: needed = 2; break;
case 16: needed = 1; break;
case 17: needed = 0; break;
default: throw new ResourceRequestDeniedException("prefix length >17?");
}
if (needed == 0) {
logger.warn("prefix is full MAC address, conflict detection OFF");
return macPrefix;
}
synchronized (list_lock) {
final String result;
if (needed < 4) {
// slow, but tries every single permutation
result = _pickNewSerially(macs, macPrefix, needed);
} else {
// Faster. Does not get at every single permutation but is very
// unlikely to fail when address range is 16^4 or higher
result = _pickNewWithRandomComponent(macs, macPrefix, needed);
}
if (result == null) {
throw new ResourceRequestDeniedException(
"no unique MAC address is available");
} else {
macs.add(result);
return result;
}