From 390c99701b3af3c9c4ec33ce115fc385fbd9a440 Mon Sep 17 00:00:00 2001 From: Brian O'Connor Date: Wed, 24 Jun 2015 16:08:09 -0400 Subject: [PATCH] Refactoring BlockAllocatorBasedIdGenerator.java Allowing first call to getNewId() to throw an NPE, and handling initial setup in catch block Change-Id: I409aa9c8a309dbbf4fc3738c3870ec4d91831303 --- .../impl/BlockAllocatorBasedIdGenerator.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java b/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java index ca483b4bf6..d48c0d67b4 100644 --- a/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java +++ b/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java @@ -21,6 +21,8 @@ import org.onosproject.core.IdBlock; import org.onosproject.core.IdGenerator; import org.onosproject.core.UnavailableIdException; +import static com.google.common.base.Preconditions.checkNotNull; + /** * Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as * backend. @@ -37,27 +39,29 @@ public class BlockAllocatorBasedIdGenerator implements IdGenerator { * @param allocator the ID block allocator to use */ protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) { - this.allocator = allocator; + this.allocator = checkNotNull(allocator, "allocator cannot be null"); this.initialized = new AtomicBoolean(false); } @Override public long getNewId() { try { - if (!initialized.get()) { - synchronized (allocator) { - if (!initialized.get()) { - idBlock = allocator.allocateUniqueIdBlock(); - initialized.set(true); - } - } - } return idBlock.getNextId(); } catch (UnavailableIdException e) { synchronized (allocator) { idBlock = allocator.allocateUniqueIdBlock(); } return idBlock.getNextId(); + } catch (NullPointerException e) { + synchronized (allocator) { + if (!initialized.get()) { + idBlock = allocator.allocateUniqueIdBlock(); + initialized.set(true); + return idBlock.getNextId(); + } else { + throw e; + } + } } } }