Added MessageIdGenerator for providing unique message IDs in the future.
This commit is contained in:
		
							
								
								
									
										41
									
								
								src/main/java/envoy/data/MessageIdGenerator.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/main/java/envoy/data/MessageIdGenerator.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
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
 | 
			
		||||
	 */
 | 
			
		||||
	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++;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user