com.jongsoft.lang.collection.support.Collections Maven / Gradle / Ivy
package com.jongsoft.lang.collection.support;
import java.util.ArrayList;
import java.util.Objects;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import com.jongsoft.lang.API;
import com.jongsoft.lang.collection.Collection;
import com.jongsoft.lang.collection.Map;
import com.jongsoft.lang.collection.Sequence;
public final class Collections {
private Collections() {
// hidden constructor utility class
}
public static Map> groupBy(Supplier> instanceSupplier, Sequence source,
Function super U, ? extends K> keyGenerator) {
Objects.requireNonNull(keyGenerator, "keyGenerator is null");
Map> result = API.Map();
for (U element : source) {
K key = keyGenerator.apply(element);
Sequence elementsForKey = API.Option(result.get(key))
.getOrSupply(instanceSupplier);
result = result.put(key, elementsForKey.append(element));
}
return result;
}
public static > Collector, K> collector(Function, K> finisher) {
final BinaryOperator> combiner = (left, right) -> {
left.addAll(right);
return left;
};
return Collector.of(ArrayList::new, ArrayList::add, combiner, finisher::apply);
}
public static String textValueOf(String type, Collection collection) {
StringBuilder text = new StringBuilder(type);
text.append("[");
if (collection != null) {
int index = 0;
for (T element : collection) {
text.append(element);
index++;
if (index < collection.size()) {
text.append(", ");
}
}
}
text.append("]");
return text.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy