blob: 53048e40323a98bb8d733889ecee540ed6f8c971 [file] [log] [blame]
Jonathan Hartdeda0ba2014-04-03 11:14:12 -07001package net.onrc.onos.core.registry;
Jonathan Hart1530ccc2013-04-03 19:36:02 -07002
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07003import com.google.common.base.Objects;
4import net.onrc.onos.core.util.UnavailableIdException;
5
6import static com.google.common.base.Preconditions.checkArgument;
7
8/**
9 * A class representing an ID space. This class is not thread-safe.
10 */
11public final class IdBlock {
Jonathan Hart12a26aa2014-06-04 14:33:09 -070012 private final long start;
Jonathan Hart12a26aa2014-06-04 14:33:09 -070013 private final long size;
Jonathan Hart1530ccc2013-04-03 19:36:02 -070014
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070015 private long currentId;
16
17 /**
18 * Constructs a new ID block with the specified size and initial value.
19 *
20 * @param start initial value of the block
21 * @param size size of the block
22 * @throws IllegalArgumentException if the size is less than or equal to 0
23 */
24 public IdBlock(long start, long size) {
25 checkArgument(size > 0, "size should be more than 0, but %s", size);
26
Ray Milkey269ffb92014-04-03 14:43:30 -070027 this.start = start;
Ray Milkey269ffb92014-04-03 14:43:30 -070028 this.size = size;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070029
30 this.currentId = start;
Ray Milkey269ffb92014-04-03 14:43:30 -070031 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -070032
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070033 // TODO: consider if this method is needed or not
34 /**
35 * Returns the initial value.
36 *
37 * @return initial value
38 */
Ray Milkey269ffb92014-04-03 14:43:30 -070039 public long getStart() {
40 return start;
41 }
42
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070043 // TODO: consider if this method is needed or not
44 /**
45 * Returns the last value.
46 *
47 * @return last value
48 */
Ray Milkey269ffb92014-04-03 14:43:30 -070049 public long getEnd() {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070050 return start + size - 1;
Ray Milkey269ffb92014-04-03 14:43:30 -070051 }
52
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070053 /**
54 * Returns the block size.
55 *
56 * @return block size
57 */
Ray Milkey269ffb92014-04-03 14:43:30 -070058 public long getSize() {
59 return size;
60 }
61
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070062 /**
63 * Returns the next ID in the block.
64 *
65 * @return next ID
66 * @throws UnavailableIdException if there is no available ID in the block.
67 */
68 public long getNextId() {
69 if (currentId > getEnd()) {
70 throw new UnavailableIdException(String.format(
71 "use all IDs in allocated space (size: %d, end: %d, current: %d)",
72 size, getEnd(), currentId
73 ));
74 }
75 long id = currentId;
76 currentId++;
77
78 return id;
79 }
80
81 @Override
82 public boolean equals(Object o) {
83 if (this == o) {
84 return true;
85 }
86 if (o == null || getClass() != o.getClass()) {
87 return false;
88 }
89
90 IdBlock that = (IdBlock) o;
91 return Objects.equal(this.start, that.start)
92 && Objects.equal(this.size, that.size)
93 && Objects.equal(this.currentId, that.currentId);
94 }
95
96 @Override
97 public int hashCode() {
98 return Objects.hashCode(start, size, currentId);
99 }
100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 @Override
102 public String toString() {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -0700103 return Objects.toStringHelper(getClass())
104 .add("start", start)
105 .add("size", size)
106 .add("currentId", currentId)
107 .toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700109}