Create an implementaion of IntentIdGenerator
- Define interface to allocate IdBlock in IdBlockAllocator
- Implement IdBlockAllocatorBasedIntentIdGenerator
- Refactor IdBlock class
Change-Id: I21fa21ae625e3d7e137a7f846bb5a0c1bdb8df9a
diff --git a/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java b/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java
new file mode 100644
index 0000000..9f2eceb
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java
@@ -0,0 +1,34 @@
+package net.onrc.onos.core.newintent;
+
+import net.onrc.onos.api.newintent.IntentId;
+import net.onrc.onos.api.newintent.IntentIdGenerator;
+import net.onrc.onos.core.registry.IdBlock;
+import net.onrc.onos.core.util.UnavailableIdException;
+import net.onrc.onos.core.util.IdBlockAllocator;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * An implementation of {@link IntentIdGenerator},
+ * which uses {@link IdBlockAllocator#allocateUniqueIdBlock()}.
+ */
+public class IdBlockAllocatorBasedIntentIdGenerator implements IntentIdGenerator {
+
+ private final IdBlockAllocator allocator;
+ private IdBlock idBlock;
+
+ public IdBlockAllocatorBasedIntentIdGenerator(IdBlockAllocator allocator) {
+ this.allocator = checkNotNull(allocator);
+ this.idBlock = allocator.allocateUniqueIdBlock();
+ }
+
+ @Override
+ public synchronized IntentId getNewId() {
+ try {
+ return new IntentId(idBlock.getNextId());
+ } catch (UnavailableIdException e) {
+ idBlock = allocator.allocateUniqueIdBlock();
+ return new IntentId(idBlock.getNextId());
+ }
+ }
+}