From b84f05e1114856d0832c6187279b9832908e4347 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 31 Dec 2019 18:10:12 +0200 Subject: [PATCH] Added MessageIdGenerator for providing unique message IDs in the future. --- .../java/envoy/data/MessageIdGenerator.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/envoy/data/MessageIdGenerator.java 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++; + } +}