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

java.util.stream.Collectors Maven / Gradle / Ivy

The newest version!
package java.util.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;

public final class Collectors
{
	private static  Function castingIdentity()
	{
		return i -> (R) i;
	}

	static class CollectorImpl implements Collector
	{
		private final Supplier supplier;
		private final BiConsumer accumulator;
		private final BinaryOperator combiner;
		private final Function finisher;
		private final Set characteristics;

		CollectorImpl(Supplier supplier, BiConsumer accumulator, BinaryOperator combiner, Function finisher, Set characteristics)
		{
			this.supplier= supplier;
			this.accumulator= accumulator;
			this.combiner= combiner;
			this.finisher= finisher;
			this.characteristics= characteristics;
		}

		CollectorImpl(Supplier supplier, BiConsumer accumulator, BinaryOperator combiner, Set characteristics)
		{
			this(supplier, accumulator, combiner, castingIdentity(), characteristics);
		}

		public BiConsumer accumulator()
		{
			return accumulator;
		}

		public Supplier supplier()
		{
			return supplier;
		}

		public BinaryOperator combiner()
		{
			return combiner;
		}

		public Function finisher()
		{
			return finisher;
		}

		public Set characteristics()
		{
			return characteristics;
		}
	}

	public static  Collector> toList()
	{
		return new Collector>()
		{
			List result= new ArrayList();

			public Supplier supplier()
			{
				return null;
			}

			public BiConsumer accumulator()
			{
				return (o, t) -> result.add(t);
			}

			public BinaryOperator combiner()
			{
				return null;
			}

			public Function> finisher()
			{
				return o -> (List) result;
			}

			public Set characteristics()
			{
				return null;
			}
		};
	}
}