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

reactor.fn.Suppliers Maven / Gradle / Ivy

There is a newer version: 3.6.10
Show newest version
/*
 * Copyright (c) 2011-2014 Pivotal Software, Inc.
 *
 *  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 reactor.fn;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Helper class for working tying {@link Supplier Suppliers} to {@link Iterable Iterables} and other types of
 * collections.
 *
 * @author Jon Brisbin
 */
public abstract class Suppliers {

	private Suppliers() {
	}

	/**
	 * Wrap the given object that will supply the given object every time {@link Supplier#get()} is
	 * called.
	 *
	 * @param obj
	 * 		the object to supply
	 * @param 
	 * 		type of the supplied object
	 *
	 * @return the new {@link Supplier}
	 */
	public static  Supplier supply(final T obj) {
		return new Supplier() {
			@Override
			public T get() {
				return obj;
			}
		};
	}

	/**
	 * Supply the given object only once, the first time {@link Supplier#get()} is invoked.
	 *
	 * @param obj
	 * 		the object to supply
	 * @param 
	 * 		type of the supplied object
	 *
	 * @return the new {@link Supplier}
	 */
	@SuppressWarnings("unchecked")
	public static  Supplier supplyOnce(final T obj) {
		return drain(Arrays.asList(obj));
	}

	/**
	 * Supply the given object to callers only as long as the given {@link Predicate} returns true.
	 *
	 * @param obj
	 * 		the object to supply
	 * @param predicate
	 * 		the predicate to check to determine whether or not to supply the given value
	 * @param 
	 * 		type of the supplied object
	 *
	 * @return the new {@link Supplier}
	 */
	public static  Supplier supplyWhile(final T obj, final Predicate predicate) {
		return new Supplier() {
			@Override
			public T get() {
				if(predicate.test(obj)) {
					return obj;
				} else {
					return null;
				}
			}
		};
	}

	/**
	 * Create a {@link Supplier} that continually round-robin load balances each call to {@link
	 * Supplier#get()} by iterating over the objects. When the end is reached, it wraps around to the first object and
	 * keeps providing objects to callers.
	 *
	 * @param objs
	 * 		the objects to load-balance
	 * @param 
	 * 		type of the supplied object
	 *
	 * @return the new {@link Supplier}
	 */
	public static  Supplier roundRobin(final T... objs) {
		final AtomicInteger count = new AtomicInteger();
		final int len = objs.length;

		return new Supplier() {
			@Override public T get() {
				return objs[count.getAndIncrement() % len];
			}
		};
	}

	/**
	 * Filter the given {@link Iterable} using the given {@link Predicate} so that calls to the return {@link
	 * Supplier#get()} will provide only items from the original collection which pass the predicate
	 * test.
	 *
	 * @param src
	 * 		the source of objects to filter
	 * @param predicate
	 * 		the {@link Predicate} to test items against
	 * @param 
	 * 		type of the source
	 *
	 * @return the new {@link Supplier}
	 */
	public static  Supplier filter(final Iterable src, final Predicate predicate) {
		return new Supplier() {
			Iterator iter = src.iterator();

			@Override
			public T get() {
				if(!iter.hasNext()) {
					return null;
				}
				T obj;
				do {
					obj = iter.next();
				} while(!predicate.test(obj));
				return obj;
			}
		};
	}

	/**
	 * Create a {@link Supplier} which drains the contents of the given {@link java.lang.Iterable} by
	 * internally creating an {@link java.util.Iterator} and delegating each call of {@link
	 * Supplier#get()} to {@link java.util.Iterator#next()}.
	 *
	 * @param c
	 * 		the collection to consume
	 * @param 
	 * 		type of the source
	 *
	 * @return the new {@link Supplier}
	 */
	public static  Supplier drain(Iterable c) {
		final Iterator iter = c.iterator();

		return new Supplier() {
			@Override
			public T get() {
				return (iter.hasNext() ? iter.next() : null);
			}
		};
	}

	/**
	 * Create a {@link Supplier} which drains all of the given {@link java.lang.Iterable Iterables}.
	 *
	 * @param iters
	 * 		the collections to consume
	 * @param 
	 * 		type of the source
	 *
	 * @return the new {@link Supplier}
	 *
	 * @see #drain(Iterable)
	 */
	public static  Supplier drainAll(Iterable> iters) {
		List> ls = new ArrayList>();
		for(Iterable iter : iters) {
			ls.add(drain(iter));
		}
		return collect(ls);
	}

	/**
	 * Create a {@link Supplier} that aggregates the given list of suppliers by calling each one, in
	 * turn, until the supplier returns {@code null}. The aggregator then goes on to the next supplier in the list and
	 * delegates calls to that supplier, and so on, until the end of the list is reached.
	 *
	 * @param suppliers
	 * 		the list of suppliers to delegate to
	 * @param 
	 * 		type of the source
	 *
	 * @return the new {@link Supplier}
	 */
	public static  Supplier collect(List> suppliers) {
		final ListIterator> iter = suppliers.listIterator();

		return new Supplier() {
			@Override
			public synchronized T get() {
				if(iter.hasNext()) {
					T obj = iter.next().get();
					if(null != obj) {
						return obj;
					}
				} else if(iter.hasPrevious()) {
					// rewind
					while(iter.hasPrevious()) {
						iter.previous();
					}
					return get();
				}
				return null;
			}
		};
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy