Sho SHIMIZU | fc932d5 | 2014-08-15 11:22:37 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.util; |
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; |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 4 | |
| 5 | import static com.google.common.base.Preconditions.checkArgument; |
| 6 | |
| 7 | /** |
| 8 | * A class representing an ID space. This class is not thread-safe. |
| 9 | */ |
| 10 | public final class IdBlock { |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 11 | private final long start; |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 12 | private final long size; |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 13 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 14 | private long currentId; |
| 15 | |
| 16 | /** |
| 17 | * Constructs a new ID block with the specified size and initial value. |
| 18 | * |
| 19 | * @param start initial value of the block |
| 20 | * @param size size of the block |
| 21 | * @throws IllegalArgumentException if the size is less than or equal to 0 |
| 22 | */ |
| 23 | public IdBlock(long start, long size) { |
| 24 | checkArgument(size > 0, "size should be more than 0, but %s", size); |
| 25 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | this.start = start; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | this.size = size; |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 28 | |
| 29 | this.currentId = start; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | } |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 31 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 32 | // TODO: consider if this method is needed or not |
| 33 | /** |
| 34 | * Returns the initial value. |
| 35 | * |
| 36 | * @return initial value |
| 37 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | public long getStart() { |
| 39 | return start; |
| 40 | } |
| 41 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 42 | // TODO: consider if this method is needed or not |
| 43 | /** |
| 44 | * Returns the last value. |
| 45 | * |
| 46 | * @return last value |
| 47 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | public long getEnd() { |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 49 | return start + size - 1; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Returns the block size. |
| 54 | * |
| 55 | * @return block size |
| 56 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | public long getSize() { |
| 58 | return size; |
| 59 | } |
| 60 | |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Returns the next ID in the block. |
| 63 | * |
| 64 | * @return next ID |
| 65 | * @throws UnavailableIdException if there is no available ID in the block. |
| 66 | */ |
| 67 | public long getNextId() { |
| 68 | if (currentId > getEnd()) { |
| 69 | throw new UnavailableIdException(String.format( |
| 70 | "use all IDs in allocated space (size: %d, end: %d, current: %d)", |
| 71 | size, getEnd(), currentId |
| 72 | )); |
| 73 | } |
| 74 | long id = currentId; |
| 75 | currentId++; |
| 76 | |
| 77 | return id; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object o) { |
| 82 | if (this == o) { |
| 83 | return true; |
| 84 | } |
| 85 | if (o == null || getClass() != o.getClass()) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | IdBlock that = (IdBlock) o; |
| 90 | return Objects.equal(this.start, that.start) |
| 91 | && Objects.equal(this.size, that.size) |
| 92 | && Objects.equal(this.currentId, that.currentId); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int hashCode() { |
| 97 | return Objects.hashCode(start, size, currentId); |
| 98 | } |
| 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public String toString() { |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 102 | return Objects.toStringHelper(getClass()) |
| 103 | .add("start", start) |
| 104 | .add("size", size) |
| 105 | .add("currentId", currentId) |
| 106 | .toString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | } |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 108 | } |