diff --git a/src/main/java/envoy/data/MessageIdGenerator.java b/src/main/java/envoy/data/MessageIdGenerator.java new file mode 100644 index 0000000..ba52e83 --- /dev/null +++ b/src/main/java/envoy/data/MessageIdGenerator.java @@ -0,0 +1,41 @@ +package envoy.data; + +/** + * Generates increasing IDs between two numbers.
+ *
+ * Project: envoy-common
+ * File: MessageIdGenerator.java
+ * Created: 31.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ +public class MessageIdGenerator { + + private final long end; + private long current; + + /** + * Creates an instance of {@link MessageIdGenerator}. + * + * @param begin the first ID + * @param end the last ID + */ + public MessageIdGenerator(long begin, long end) { + current = begin; + this.end = end; + } + + /** + * @return {@code true} if there are unused IDs remaining + */ + public boolean hasNext() { return current < end; } + + /** + * @return the next ID + */ + public long next() { + if (!hasNext()) throw new IllegalStateException("All IDs have been used"); + return current++; + } +}