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

org.d2ab.collection.Iterables Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Daniel Skogquist Åborg
 *
 * 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.d2ab.collection;

import org.d2ab.collection.SizedIterable.FixedSizedIterable;
import org.d2ab.collection.SizedIterable.SizeType;
import org.d2ab.iterator.ArrayIterator;
import org.d2ab.iterator.Iterators;
import org.d2ab.util.Pair;

import java.util.*;
import java.util.Map.Entry;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static org.d2ab.collection.SizedIterable.SizeType.UNAVAILABLE;

/**
 * Utility methods for {@link Iterable} instances.
 */
public abstract class Iterables {
	private static final SizedIterable EMPTY = new FixedSizedIterable() {
		@SuppressWarnings("unchecked")
		@Override
		public Iterator iterator() {
			return Iterators.empty();
		}

		@Override
		public int size() {
			return 0;
		}
	};

	Iterables() {
	}

	/**
	 * @return an unmodifiable empty {@link Iterable}.
	 */
	@SuppressWarnings("unchecked")
	public static  SizedIterable empty() {
		return EMPTY;
	}

	/**
	 * @return an unmodifiable singleton {@link Iterable} containing the given object.
	 */
	public static  SizedIterable of(T object) {
		return new FixedSizedIterable() {
			@Override
			public Iterator iterator() {
				return new SingletonIterator<>(object);
			}

			@Override
			public int size() {
				return 1;
			}
		};
	}

	/**
	 * @return an unmodifiable {@link Iterable} containing the given objects.
	 */
	@SafeVarargs
	public static  SizedIterable of(T... objects) {
		return new FixedSizedIterable() {
			@Override
			public Iterator iterator() {
				return new ArrayIterator<>(objects);
			}

			@Override
			public int size() {
				return objects.length;
			}
		};
	}

	/**
	 * Create a one-pass-only {@code Iterable} from an {@link Iterator} of items. Note that {@code Iterables} created
	 * from {@link Iterator}s will be exhausted when the given iterator has been passed over. Further attempts will
	 * register the {@code Iterable} as empty. If the iterator is terminated partway through iteration, further
	 * calls to {@link Iterable#iterator()} will pick up where the previous iterator left off. If
	 * {@link Iterable#iterator()} calls are interleaved, calls to the given iterator will be interleaved.
	 */
	public static  Iterable once(Iterator iterator) {
		return () -> iterator;
	}

	/**
	 * Converts a container of some kind into a possibly once-only {@link Iterable}.
	 *
	 * @param container the non-null container to turn into an {@link Iterable}, can be one of {@link Iterable}, {@link
	 *                  Iterator}, {@link Stream}, {@code Array}, {@link Pair} or {@link Entry}.
	 *
	 * @return the container as an iterable.
	 *
	 * @throws ClassCastException if the container is not one of {@link Iterable}, {@link Iterator}, {@link Stream},
	 *                            {@code Array}, {@link Pair} or {@link Entry}
	 */
	@SuppressWarnings("unchecked")
	public static  Iterable from(Object container) {
		if (container instanceof Iterable)
			return (Iterable) container;
		else if (container instanceof Iterator)
			return once((Iterator) container);
		else if (container instanceof Stream)
			return once(((Stream) container).iterator());
		else if (container instanceof Object[])
			return of((T[]) container);
		else if (container instanceof Pair)
			return fromPair((Pair) container);
		else if (container instanceof Entry)
			return fromEntry((Entry) container);
		else
			throw new ClassCastException("Required an Iterable, Iterator, Array, Stream, Pair or Entry but got: " +
			                             container.getClass());
	}

	public static  SizedIterable fromPair(final Pair pair) {
		return new FixedSizedIterable() {
			@Override
			public Iterator iterator() {
				return pair.iterator();
			}

			@Override
			public int size() {
				return 2;
			}
		};
	}

	public static  SizedIterable fromEntry(final Entry entry) {
		return new FixedSizedIterable() {
			@Override
			public Iterator iterator() {
				return Maps.iterator(entry);
			}

			@Override
			public int size() {
				return 2;
			}
		};
	}

	/**
	 * @return true if all elements in this {@code Sequence} satisfy the given predicate, false otherwise.
	 */
	public static  boolean all(Iterable iterable, Predicate predicate) {
		for (T each : iterable)
			if (!predicate.test(each))
				return false;

		return true;
	}

	/**
	 * @return true if no elements in this {@code Sequence} satisfy the given predicate, false otherwise.
	 */
	public static  boolean none(Iterable iterable, Predicate predicate) {
		return !any(iterable, predicate);
	}

	/**
	 * @return true if any element in this {@code Sequence} satisfies the given predicate, false otherwise.
	 */
	public static  boolean any(Iterable iterable, Predicate predicate) {
		for (T each : iterable)
			if (predicate.test(each))
				return true;

		return false;
	}

	/**
	 * Remove all elements in the given {@link Iterable} using {@link Iterator#remove()}.
	 */
	public static void clear(Iterable iterable) {
		for (Iterator iterator = iterable.iterator(); iterator.hasNext(); ) {
			iterator.next();
			iterator.remove();
		}
	}

	/**
	 * Remove all elements in the given {@link Iterable} found among the given items, using {@link Iterator#remove()}.
	 */
	public static boolean removeAll(Iterable iterable, Object... items) {
		boolean modified = false;
		for (Iterator iterator = iterable.iterator(); iterator.hasNext(); )
			if (Arrayz.contains(items, iterator.next())) {
				iterator.remove();
				modified = true;
			}
		return modified;
	}

	/**
	 * Remove all elements in the given {@link Iterable} found in the second {@link Iterable},
	 * using {@link Iterator#remove()}.
	 */
	public static boolean removeAll(Iterable iterable, Iterable items) {
		boolean modified = false;
		for (Iterator iterator = iterable.iterator(); iterator.hasNext(); )
			if (contains(items, iterator.next())) {
				iterator.remove();
				modified = true;
			}
		return modified;
	}

	/**
	 * Remove all elements in the given {@link Iterable} found among the given items, using {@link Iterator#remove()}.
	 */
	public static boolean retainAll(Iterable iterable, Object... items) {
		boolean modified = false;
		for (Iterator iterator = iterable.iterator(); iterator.hasNext(); )
			if (!Arrayz.contains(items, iterator.next())) {
				iterator.remove();
				modified = true;
			}
		return modified;
	}

	/**
	 * Remove all elements in the given {@link Iterable} found in the second {@link Iterable},
	 * using {@link Iterator#remove()}.
	 */
	public static boolean retainAll(Iterable iterable, Iterable items) {
		boolean modified = false;
		for (Iterator iterator = iterable.iterator(); iterator.hasNext(); ) {
			if (!contains(items, iterator.next())) {
				iterator.remove();
				modified = true;
			}
		}
		return modified;
	}

	/**
	 * @return the given {@link Iterable} collected into a {@link List}.
	 */
	public static  List toList(Iterable iterable) {
		if (iterable instanceof Collection)
			return new ArrayList<>((Collection) iterable);

		List list = new ArrayList<>();
		for (T t : iterable)
			list.add(t);
		return list;
	}

	/**
	 * Create a {@link List} view of the given {@link Iterable}, where changes in the underlying {@link Iterable} are
	 * reflected in the returned {@link List}. If a {@link List} is given it is returned unchanged. The list does not
	 * implement {@link RandomAccess} unless the given {@link Iterable} does, and is best accessed in sequence. The
	 * list does not support modification except the various removal operations, through {@link Iterator#remove()} only
	 * if implemented in the {@link Iterable}'s {@link Iterable#iterator()}.
	 *
	 * @since 1.2
	 */
	public static  List asList(Iterable iterable) {
		return IterableList.from(iterable);
	}

	/**
	 * @return true if any object in the given {@link Iterable} is equal to the given object, false otherwise.
	 *
	 * @since 1.2
	 */
	@SuppressWarnings("unchecked")
	public static  boolean contains(Iterable iterable, T object) {
		if (iterable instanceof Collection)
			return ((Collection) iterable).contains(object);

		for (T each : iterable)
			if (Objects.equals(each, object))
				return true;

		return false;
	}

	/**
	 * @return true if the given {@link Collection} contains all of the given items, false otherwise.
	 *
	 * @since 1.2
	 */
	@SafeVarargs
	public static  boolean containsAll(Collection collection, T... items) {
		for (T item : items)
			if (!collection.contains(item))
				return false;

		return true;
	}

	/**
	 * @return true if the given {@link Collection} contains any of the given items, false otherwise.
	 *
	 * @since 1.2
	 */
	@SafeVarargs
	public static  boolean containsAny(Collection collection, T... items) {
		for (Object item : items)
			if (collection.contains(item))
				return true;

		return false;
	}

	/**
	 * @return true if the given {@link Collection} contains any of the given items, false otherwise.
	 *
	 * @since 1.2
	 */
	public static boolean containsAny(Collection collection, Iterable items) {
		for (Object item : items)
			if (collection.contains(item))
				return true;

		return false;
	}

	public static SizeType sizeType(Iterable iterable) {
		if (iterable instanceof SizedIterable)
			return ((SizedIterable) iterable).sizeType();

		if (iterable instanceof Collection)
			return Collectionz.sizeType((Collection) iterable);

		return UNAVAILABLE;
	}

	public static int size(Iterable iterable) {
		if (iterable instanceof SizedIterable)
			return ((SizedIterable) iterable).size();

		if (iterable instanceof Collection)
			return ((Collection) iterable).size();

		return Iterators.size(iterable.iterator());
	}

	public static boolean isEmpty(Iterable iterable) {
		if (iterable instanceof SizedIterable)
			return ((SizedIterable) iterable).isEmpty();

		if (iterable instanceof Collection)
			return ((Collection) iterable).isEmpty();

		return !iterable.iterator().hasNext();
	}

	private static class SingletonIterator implements Iterator {
		private final T object;
		private boolean used;

		public SingletonIterator(T object) {
			this.object = object;
		}

		@Override
		public boolean hasNext() {
			return !used;
		}

		@Override
		public T next() {
			if (used)
				throw new NoSuchElementException();

			used = true;
			return object;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy