blob: 42bcbf643ffd596a9d25ecc6d1e9cd3da7d19e94 [file] [log] [blame]
Sho SHIMIZUfc932d52014-08-15 11:22:37 -07001package net.onrc.onos.core.util;
Jonathan Hart1530ccc2013-04-03 19:36:02 -07002
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -07003import java.util.concurrent.atomic.AtomicLong;
4
5import javax.annotation.concurrent.ThreadSafe;
6
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07007import com.google.common.base.Objects;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07008
9import static com.google.common.base.Preconditions.checkArgument;
10
11/**
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070012 * A class representing an ID space.
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070013 */
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070014@ThreadSafe
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070015public final class IdBlock {
Jonathan Hart12a26aa2014-06-04 14:33:09 -070016 private final long start;
Jonathan Hart12a26aa2014-06-04 14:33:09 -070017 private final long size;
Jonathan Hart1530ccc2013-04-03 19:36:02 -070018
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070019 private AtomicLong currentId;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070020
21 /**
22 * Constructs a new ID block with the specified size and initial value.
23 *
24 * @param start initial value of the block
25 * @param size size of the block
26 * @throws IllegalArgumentException if the size is less than or equal to 0
27 */
28 public IdBlock(long start, long size) {
29 checkArgument(size > 0, "size should be more than 0, but %s", size);
30
Ray Milkey269ffb92014-04-03 14:43:30 -070031 this.start = start;
Ray Milkey269ffb92014-04-03 14:43:30 -070032 this.size = size;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070033
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070034 this.currentId = new AtomicLong(start);
Ray Milkey269ffb92014-04-03 14:43:30 -070035 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -070036
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070037 // TODO: consider if this method is needed or not
38 /**
39 * Returns the initial value.
40 *
41 * @return initial value
42 */
Ray Milkey269ffb92014-04-03 14:43:30 -070043 public long getStart() {
44 return start;
45 }
46
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070047 // TODO: consider if this method is needed or not
48 /**
49 * Returns the last value.
50 *
51 * @return last value
52 */
Ray Milkey269ffb92014-04-03 14:43:30 -070053 public long getEnd() {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070054 return start + size - 1;
Ray Milkey269ffb92014-04-03 14:43:30 -070055 }
56
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070057 /**
58 * Returns the block size.
59 *
60 * @return block size
61 */
Ray Milkey269ffb92014-04-03 14:43:30 -070062 public long getSize() {
63 return size;
64 }
65
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070066 /**
67 * Returns the next ID in the block.
68 *
69 * @return next ID
70 * @throws UnavailableIdException if there is no available ID in the block.
71 */
72 public long getNextId() {
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070073 final long id = currentId.getAndIncrement();
74 if (id > getEnd()) {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070075 throw new UnavailableIdException(String.format(
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070076 "used all IDs in allocated space (size: %d, end: %d, current: %d)",
77 size, getEnd(), id
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070078 ));
79 }
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070080
81 return id;
82 }
83
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070084 // TODO: Do we really need equals and hashCode? Should it contain currentId?
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070085 @Override
86 public boolean equals(Object o) {
87 if (this == o) {
88 return true;
89 }
90 if (o == null || getClass() != o.getClass()) {
91 return false;
92 }
93
94 IdBlock that = (IdBlock) o;
95 return Objects.equal(this.start, that.start)
96 && Objects.equal(this.size, that.size)
Yuta HIGUCHI992c70e2014-08-19 23:28:24 -070097 && Objects.equal(this.currentId.get(), that.currentId.get());
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070098 }
99
100 @Override
101 public int hashCode() {
102 return Objects.hashCode(start, size, currentId);
103 }
104
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 @Override
106 public String toString() {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -0700107 return Objects.toStringHelper(getClass())
108 .add("start", start)
109 .add("size", size)
110 .add("currentId", currentId)
111 .toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700113}