Make IdBlock thread safe.
Change-Id: I46f414074221e8a71b8d5628a1c2fe2184c52236
diff --git a/src/main/java/net/onrc/onos/core/util/IdBlock.java b/src/main/java/net/onrc/onos/core/util/IdBlock.java
index 1d544f6..42bcbf6 100644
--- a/src/main/java/net/onrc/onos/core/util/IdBlock.java
+++ b/src/main/java/net/onrc/onos/core/util/IdBlock.java
@@ -1,17 +1,22 @@
package net.onrc.onos.core.util;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.annotation.concurrent.ThreadSafe;
+
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkArgument;
/**
- * A class representing an ID space. This class is not thread-safe.
+ * A class representing an ID space.
*/
+@ThreadSafe
public final class IdBlock {
private final long start;
private final long size;
- private long currentId;
+ private AtomicLong currentId;
/**
* Constructs a new ID block with the specified size and initial value.
@@ -26,7 +31,7 @@
this.start = start;
this.size = size;
- this.currentId = start;
+ this.currentId = new AtomicLong(start);
}
// TODO: consider if this method is needed or not
@@ -65,18 +70,18 @@
* @throws UnavailableIdException if there is no available ID in the block.
*/
public long getNextId() {
- if (currentId > getEnd()) {
+ final long id = currentId.getAndIncrement();
+ if (id > getEnd()) {
throw new UnavailableIdException(String.format(
- "use all IDs in allocated space (size: %d, end: %d, current: %d)",
- size, getEnd(), currentId
+ "used all IDs in allocated space (size: %d, end: %d, current: %d)",
+ size, getEnd(), id
));
}
- long id = currentId;
- currentId++;
return id;
}
+ // TODO: Do we really need equals and hashCode? Should it contain currentId?
@Override
public boolean equals(Object o) {
if (this == o) {
@@ -89,7 +94,7 @@
IdBlock that = (IdBlock) o;
return Objects.equal(this.start, that.start)
&& Objects.equal(this.size, that.size)
- && Objects.equal(this.currentId, that.currentId);
+ && Objects.equal(this.currentId.get(), that.currentId.get());
}
@Override