blob: 1d544f64cf9b2bd9b2390c59eaa91dd652a92ad8 [file] [log] [blame]
Sho SHIMIZUfc932d52014-08-15 11:22:37 -07001package net.onrc.onos.core.util;
Jonathan Hart1530ccc2013-04-03 19:36:02 -07002
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07003import com.google.common.base.Objects;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07004
5import static com.google.common.base.Preconditions.checkArgument;
6
7/**
8 * A class representing an ID space. This class is not thread-safe.
9 */
10public final class IdBlock {
Jonathan Hart12a26aa2014-06-04 14:33:09 -070011 private final long start;
Jonathan Hart12a26aa2014-06-04 14:33:09 -070012 private final long size;
Jonathan Hart1530ccc2013-04-03 19:36:02 -070013
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070014 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 Milkey269ffb92014-04-03 14:43:30 -070026 this.start = start;
Ray Milkey269ffb92014-04-03 14:43:30 -070027 this.size = size;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070028
29 this.currentId = start;
Ray Milkey269ffb92014-04-03 14:43:30 -070030 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -070031
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070032 // TODO: consider if this method is needed or not
33 /**
34 * Returns the initial value.
35 *
36 * @return initial value
37 */
Ray Milkey269ffb92014-04-03 14:43:30 -070038 public long getStart() {
39 return start;
40 }
41
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070042 // TODO: consider if this method is needed or not
43 /**
44 * Returns the last value.
45 *
46 * @return last value
47 */
Ray Milkey269ffb92014-04-03 14:43:30 -070048 public long getEnd() {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070049 return start + size - 1;
Ray Milkey269ffb92014-04-03 14:43:30 -070050 }
51
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070052 /**
53 * Returns the block size.
54 *
55 * @return block size
56 */
Ray Milkey269ffb92014-04-03 14:43:30 -070057 public long getSize() {
58 return size;
59 }
60
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070061 /**
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 Milkey269ffb92014-04-03 14:43:30 -0700100 @Override
101 public String toString() {
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -0700102 return Objects.toStringHelper(getClass())
103 .add("start", start)
104 .add("size", size)
105 .add("currentId", currentId)
106 .toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700108}