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

com.github.cowwoc.tokenbucket.SelectionPolicy Maven / Gradle / Ivy

The newest version!
package com.github.cowwoc.tokenbucket;

import com.github.cowwoc.tokenbucket.internal.AbstractContainer;
import com.github.cowwoc.tokenbucket.internal.ContainerSelector;

import java.util.List;

import static com.github.cowwoc.requirements.DefaultRequirements.assertThat;

/**
 * Selects a bucket from a list.
 */
public enum SelectionPolicy
{
	/**
	 * A scheduler that selects the next bucket in a round-robin fashion.
	 *
	 * @see Round robin scheduling
	 */
	ROUND_ROBIN
		{
			@Override
			ContainerSelector createSelector()
			{
				return new ContainerSelector()
				{
					private int index = -1;

					@Override
					public AbstractContainer nextContainer(List containers)
					{
						assertThat(r -> r.requireThat(containers, "containers").isNotEmpty());
						// Wrap around end of list
						index = (index + 1) % containers.size();
						return containers.get(index);
					}
				};
			}
		};

	/**
	 * @return a new {@code ContainerSelector} that implements this policy
	 */
	abstract ContainerSelector createSelector();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy