50 lines
1.1 KiB
Java
50 lines
1.1 KiB
Java
package envoy.data;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import dev.kske.eventbus.IEvent;
|
|
|
|
/**
|
|
* Generates increasing IDs between two numbers.
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public final class IDGenerator implements IEvent, Serializable {
|
|
|
|
private final long end;
|
|
private long current;
|
|
|
|
private static final long serialVersionUID = 0L;
|
|
|
|
/**
|
|
* Creates an instance of {@link IDGenerator}.
|
|
*
|
|
* @param begin the first ID
|
|
* @param size the amount of IDs to provide
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public IDGenerator(long begin, long size) {
|
|
current = begin;
|
|
end = begin + size;
|
|
}
|
|
|
|
@Override
|
|
public String toString() { return String.format("IDGenerator[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++;
|
|
}
|
|
}
|