52 lines
1.2 KiB
Java
52 lines
1.2 KiB
Java
package envoy.data;
|
|
|
|
import java.io.Serializable;
|
|
|
|
/**
|
|
* Generates increasing IDs between two numbers.<br>
|
|
* <br>
|
|
* Project: <strong>envoy-common</strong><br>
|
|
* File: <strong>IdGenerator.java</strong><br>
|
|
* Created: <strong>31.12.2019</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public class IdGenerator implements Serializable {
|
|
|
|
private final long end;
|
|
private long current;
|
|
|
|
private static final long serialVersionUID = -1517378307055845147L;
|
|
|
|
/**
|
|
* Creates an instance of {@link IdGenerator}.
|
|
*
|
|
* @param begin the first ID
|
|
* @param end the last ID
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public IdGenerator(long begin, long end) {
|
|
current = begin;
|
|
this.end = end;
|
|
}
|
|
|
|
@Override
|
|
public String toString() { return String.format("MessageIdGenerator[current=%d,end=%d]", current, end); }
|
|
|
|
/**
|
|
* @return {@code true} if there are unused IDs remaining
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public boolean hasNext() { return current < end; }
|
|
|
|
/**
|
|
* @return the next ID
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public long next() {
|
|
if (!hasNext()) throw new IllegalStateException("All IDs have been used");
|
|
return current++;
|
|
}
|
|
}
|