This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
2019-12-31 17:10:12 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates increasing IDs between two numbers.<br>
|
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-common</strong><br>
|
|
|
|
* File: <strong>MessageIdGenerator.java</strong><br>
|
|
|
|
* Created: <strong>31.12.2019</strong><br>
|
|
|
|
*
|
|
|
|
* @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
|
2019-12-31 17:10:45 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-31 17:10:12 +01:00
|
|
|
*/
|
|
|
|
public MessageIdGenerator(long begin, long end) {
|
|
|
|
current = begin;
|
|
|
|
this.end = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {@code true} if there are unused IDs remaining
|
2019-12-31 17:10:45 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-31 17:10:12 +01:00
|
|
|
*/
|
|
|
|
public boolean hasNext() { return current < end; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the next ID
|
2019-12-31 17:10:45 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-31 17:10:12 +01:00
|
|
|
*/
|
|
|
|
public long next() {
|
|
|
|
if (!hasNext()) throw new IllegalStateException("All IDs have been used");
|
|
|
|
return current++;
|
|
|
|
}
|
|
|
|
}
|