All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bboxdb.network.client.SequenceNumberGenerator Maven / Gradle / Ivy

There is a newer version: 1.0.0-rc3
Show newest version
/*******************************************************************************
 *
 *    Copyright (C) 2015-2018 the BBoxDB project
 *  
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License. 
 *    
 *******************************************************************************/
package org.bboxdb.network.client;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public class SequenceNumberGenerator {

	/**
	 * The next unused free request id
	 */
	protected final AtomicInteger sequenceNumber = new AtomicInteger();
	
	/**
	 * The used numbers
	 */
	protected final Set usedNumbers = new HashSet<>();
	
	/**
	 * The amount of tried to find an empty sequence number
	 */
	private final static int MAX_TRIES = 10000;
	
	public SequenceNumberGenerator() {
		sequenceNumber.set(0);
	}
	
	/**
	 * Get the next free sequence number
	 * 
	 * @return The sequence number
	 */
	public synchronized short getNextSequenceNummber() {
		short nextNumber = (short) sequenceNumber.getAndIncrement();
		int numberTry = 1;
				
		// Check if sequence number is unused
		while(usedNumbers.contains(nextNumber)) {
			
			if(numberTry > MAX_TRIES) {
				throw new IllegalArgumentException("Unable to get sequence number with tries: " + MAX_TRIES);
			}
			
			nextNumber = (short) sequenceNumber.getAndIncrement();
			numberTry++;
		}
		
		usedNumbers.add(nextNumber);
		return nextNumber;
	}
	
	/**
	 * Get the current sequence number
	 * 
	 * @return The sequence number
	 */
	public short getLastGeneratedNumber() {
		return sequenceNumber.shortValue();
	}
	
	/**
	 * Release the given sequence number
	 * @param sequenceNumber
	 * @return
	 */
	public synchronized boolean releaseNumber(final short sequenceNumber) {
		return usedNumbers.remove(sequenceNumber);
	}
	
	/**
	 * Get the amount of used numbers
	 * @return 
	 */
	public int getUsedNumbers() {
		return usedNumbers.size();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy