blob: e231394ccdc0b6a1b453ff93bb2bbc8378a3e966 [file] [log] [blame]
package org.onlab.onos.core.impl;
import org.onlab.onos.core.IdBlock;
import org.onlab.onos.core.IdGenerator;
import org.onlab.onos.core.UnavailableIdException;
/**
* Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
* backend.
*/
public class BlockAllocatorBasedIdGenerator implements IdGenerator {
protected final IdBlockAllocator allocator;
protected IdBlock idBlock;
/**
* Constructs an ID generator which use {@link IdBlockAllocator} as backend.
*
* @param allocator
*/
protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
this.allocator = allocator;
this.idBlock = allocator.allocateUniqueIdBlock();
}
@Override
public long getNewId() {
try {
return idBlock.getNextId();
} catch (UnavailableIdException e) {
synchronized (allocator) {
idBlock = allocator.allocateUniqueIdBlock();
return idBlock.getNextId();
}
}
}
}