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

io.activej.common.CollectorsEx Maven / Gradle / Ivy

There is a newer version: 4.3-r8
Show newest version
/*
 * Copyright (C) 2020 ActiveJ LLC.
 *
 * 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 io.activej.common;

import io.activej.common.collection.CollectionUtils;
import io.activej.common.ref.Ref;
import io.activej.common.ref.RefBoolean;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static io.activej.common.collection.CollectionUtils.set;

public class CollectorsEx {

	public static final Collector TO_VOID = Collector.of(() -> null, (a, v) -> {}, (a1, a2) -> null, a -> null);

	@SuppressWarnings("unchecked")
	public static  Collector toVoid() {
		return (Collector) TO_VOID;
	}

	public static  Collector, T> toFirst() {
		return Collector.of(
				Ref::new,
				(a, v) -> { if (a.value == null) a.value = v; },
				(a1, a2) -> a1,
				a -> a.value);
	}

	public static  Collector, T> toLast() {
		return Collector.of(
				Ref::new,
				(a, v) -> a.value = v,
				(a1, a2) -> a1,
				a -> a.value);
	}

	private static final Collector TO_ALL =
			Collector.of(
					() -> new RefBoolean(true),
					(a, t) -> a.value &= t,
					(a1, a2) -> {
						a1.value &= a2.value;
						return a1;
					},
					b -> b.value);

	private static final Collector TO_ANY =
			Collector.of(
					() -> new RefBoolean(false),
					(a, t) -> a.value |= t,
					(a1, a2) -> {
						a1.value |= a2.value;
						return a1;
					},
					b -> b.value);

	private static final Collector TO_NONE =
			Collector.of(
					() -> new RefBoolean(true),
					(a, t) -> a.value &= !t,
					(a1, a2) -> {
						a1.value &= a2.value;
						return a1;
					},
					b -> b.value);

	public static  Collector toAll(Predicate predicate) {
		return Collector.of(
				() -> new RefBoolean(true),
				(a, t) -> a.value &= predicate.test(t),
				(a1, a2) -> {
					a1.value &= a2.value;
					return a1;
				},
				b -> b.value);
	}

	public static Collector toAll() {
		return TO_ALL;
	}

	public static  Collector toAny(Predicate predicate) {
		return Collector.of(
				() -> new RefBoolean(false),
				(a, t) -> a.value |= predicate.test(t),
				(a1, a2) -> {
					a1.value |= a2.value;
					return a1;
				},
				b -> b.value);
	}

	public static Collector toAny() {
		return TO_ANY;
	}

	public static  Collector toNone(Predicate predicate) {
		return Collector.of(
				() -> new RefBoolean(true),
				(a, t) -> a.value &= !predicate.test(t),
				(a1, a2) -> {
					a1.value &= a2.value;
					return a1;
				},
				b -> b.value);
	}

	public static Collector toNone() {
		return TO_NONE;
	}

	public static  BinaryOperator throwingMerger() {
		return (u, v) -> { throw new IllegalStateException("Duplicate key " + u); };
	}

	public static  Collector, ?, Map> toMap() {
		return Collectors.toMap(Entry::getKey, Entry::getValue);
	}

	public static  Collector>> toMultimap(Function keyMapper,
			Function valueMapper) {
		return Collectors.toMap(keyMapper, t -> set(valueMapper.apply(t)), CollectionUtils::union);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy