Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.registry; |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 2 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 3 | import com.google.common.base.Objects; |
| 4 | import net.onrc.onos.core.util.UnavailableIdException; |
| 5 | |
| 6 | import static com.google.common.base.Preconditions.checkArgument; |
| 7 | |
| 8 | /** |
| 9 | * A class representing an ID space. This class is not thread-safe. |
| 10 | */ |
| 11 | public final class IdBlock { |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 12 | private final long start; |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 13 | private final long size; |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 14 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 15 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | this.start = start; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | this.size = size; |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 29 | |
| 30 | this.currentId = start; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | } |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 32 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 33 | // TODO: consider if this method is needed or not |
| 34 | /** |
| 35 | * Returns the initial value. |
| 36 | * |
| 37 | * @return initial value |
| 38 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | public long getStart() { |
| 40 | return start; |
| 41 | } |
| 42 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 43 | // TODO: consider if this method is needed or not |
| 44 | /** |
| 45 | * Returns the last value. |
| 46 | * |
| 47 | * @return last value |
| 48 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | public long getEnd() { |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 50 | return start + size - 1; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 53 | /** |
| 54 | * Returns the block size. |
| 55 | * |
| 56 | * @return block size |
| 57 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | public long getSize() { |
| 59 | return size; |
| 60 | } |
| 61 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 62 | /** |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public String toString() { |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame^] | 103 | return Objects.toStringHelper(getClass()) |
| 104 | .add("start", start) |
| 105 | .add("size", size) |
| 106 | .add("currentId", currentId) |
| 107 | .toString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | } |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 109 | } |