mirror of
https://github.com/opennetworkinglab/onos.git
synced 2025-10-23 05:11:04 +02:00
Refactoring BlockAllocatorBasedIdGenerator.java
Allowing first call to getNewId() to throw an NPE, and handling initial setup in catch block Change-Id: I409aa9c8a309dbbf4fc3738c3870ec4d91831303
This commit is contained in:
parent
bcf1a48e89
commit
390c99701b
@ -21,6 +21,8 @@ import org.onosproject.core.IdBlock;
|
|||||||
import org.onosproject.core.IdGenerator;
|
import org.onosproject.core.IdGenerator;
|
||||||
import org.onosproject.core.UnavailableIdException;
|
import org.onosproject.core.UnavailableIdException;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
|
* Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
|
||||||
* backend.
|
* backend.
|
||||||
@ -37,27 +39,29 @@ public class BlockAllocatorBasedIdGenerator implements IdGenerator {
|
|||||||
* @param allocator the ID block allocator to use
|
* @param allocator the ID block allocator to use
|
||||||
*/
|
*/
|
||||||
protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
|
protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
|
||||||
this.allocator = allocator;
|
this.allocator = checkNotNull(allocator, "allocator cannot be null");
|
||||||
this.initialized = new AtomicBoolean(false);
|
this.initialized = new AtomicBoolean(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getNewId() {
|
public long getNewId() {
|
||||||
try {
|
try {
|
||||||
if (!initialized.get()) {
|
|
||||||
synchronized (allocator) {
|
|
||||||
if (!initialized.get()) {
|
|
||||||
idBlock = allocator.allocateUniqueIdBlock();
|
|
||||||
initialized.set(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return idBlock.getNextId();
|
return idBlock.getNextId();
|
||||||
} catch (UnavailableIdException e) {
|
} catch (UnavailableIdException e) {
|
||||||
synchronized (allocator) {
|
synchronized (allocator) {
|
||||||
idBlock = allocator.allocateUniqueIdBlock();
|
idBlock = allocator.allocateUniqueIdBlock();
|
||||||
}
|
}
|
||||||
return idBlock.getNextId();
|
return idBlock.getNextId();
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
synchronized (allocator) {
|
||||||
|
if (!initialized.get()) {
|
||||||
|
idBlock = allocator.allocateUniqueIdBlock();
|
||||||
|
initialized.set(true);
|
||||||
|
return idBlock.getNextId();
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user