com.speedment.common.mapstream.util.CollectorUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime-deploy Show documentation
Show all versions of runtime-deploy Show documentation
A Speedment bundle that shades all dependencies into one jar. This is
useful when deploying an application on a server.
/**
*
* Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
*
* 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 com.speedment.common.mapstream.util;
import com.speedment.common.mapstream.MapStream;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import static com.speedment.common.mapstream.util.StaticClassUtil.instanceNotAllowed;
/**
* Utility methods for collecting Speedment streams in various ways.
*
* @author Per Minborg
* @author Emil Forslund
* @since 2.1.0
*/
public final class CollectorUtil {
/**
* Returns a new {@link MapStream} where the elements have been grouped together using
* the specified function.
*
* @param the stream element type
* @param the type of the key to group by
* @param grouper the function to use for grouping
* @return a {@link MapStream} grouped by key
*/
public static Collector>> groupBy(Function grouper) {
return new CollectorImpl<>(
() -> new GroupHolder<>(grouper),
GroupHolder::add,
GroupHolder::merge,
GroupHolder::finisher,
Collections.emptySet()
);
}
private static class GroupHolder {
private final Function grouper;
private final Map> elements;
private final Function> createList = c -> new ArrayList<>();
public GroupHolder(Function grouper) {
this.grouper = grouper;
this.elements = new HashMap<>();
}
public void add(T element) {
final C key = grouper.apply(element);
elements.computeIfAbsent(key, createList)
.add(element);
}
public GroupHolder merge(GroupHolder holder) {
holder.elements.entrySet().forEach(e
-> elements.computeIfAbsent(e.getKey(), createList)
.addAll(e.getValue())
);
return this;
}
public MapStream> finisher() {
return MapStream.of(elements);
}
}
/**
* Simple implementation class for {@code Collector}.
*
* @param the type of elements to be collected
* @param the type of the intermediate holder
* @param the type of the result
*/
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;
}
@SuppressWarnings("unchecked")
CollectorImpl(Supplier supplier,
BiConsumer accumulator,
BinaryOperator combiner,
Set characteristics) {
this(supplier, accumulator, combiner, i -> (R) i, characteristics);
}
@Override
public BiConsumer accumulator() {
return accumulator;
}
@Override
public Supplier supplier() {
return supplier;
}
@Override
public BinaryOperator combiner() {
return combiner;
}
@Override
public Function finisher() {
return finisher;
}
@Override
public Set characteristics() {
return characteristics;
}
}
/**
* Utility classes should not be instantiated.
*/
private CollectorUtil() {
instanceNotAllowed(getClass());
}
}