Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.stream;
import static functionalj.tuple.Tuple.tuple2;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import functionalj.function.Func1;
import functionalj.function.FuncUnit1;
import functionalj.list.FuncList;
import functionalj.list.ImmutableList;
import functionalj.map.FuncMap;
import functionalj.map.ImmutableMap;
import functionalj.tuple.Tuple2;
import lombok.val;
public interface StreamPlusAdditionalTerminalOperators {
public Stream stream();
public StreamPlus deriveWith(
Function, Stream> action);
public StreamPlus filter(Predicate super DATA> predicate);
public StreamPlus filter(Function super DATA, T> mapper, Predicate super T> theCondition);
public TARGET terminate(Func1, TARGET> action);
public void terminate(FuncUnit1> action);
//-- Functionalities --
public default void forEachWithIndex(
BiConsumer super Integer, ? super DATA> action) {
terminate(stream -> {
if (action == null)
return;
val index = new AtomicInteger();
stream
.forEach(each -> action.accept(index.getAndIncrement(), each));
});
}
//-- groupingBy --
// Eager
public default FuncMap> groupingBy(
Function super DATA, ? extends KEY> classifier) {
return terminate(stream -> {
val theMap = new HashMap>();
stream
.collect(Collectors.groupingBy(classifier))
.forEach((key,list)->theMap.put(key, ImmutableList.from(list)));
return ImmutableMap.from(theMap);
});
}
// Eager
public default FuncMap groupingBy(
Function super DATA, ? extends KEY> classifier,
Function super FuncList, VALUE> aggregate) {
return terminate(stream -> {
val theMap = new HashMap();
stream
.collect(Collectors.groupingBy(classifier))
.forEach((key,list) -> {
val valueList = ImmutableList.from(list);
val aggregateValue = aggregate.apply(valueList);
theMap.put(key, aggregateValue);
});
return ImmutableMap.from(theMap);
});
}
// Eager
@SuppressWarnings({ "unchecked", "rawtypes" })
public default FuncMap groupingBy(
Function super DATA, ? extends KEY> classifier,
StreamProcessor super DATA, VALUE> processor) {
Function super FuncList, VALUE> aggregate = (FuncList list) -> {
return (VALUE)processor.process((StreamPlus)list.stream());
};
return groupingBy(classifier, aggregate);
}
// Eager
public default FuncMap groupingBy(
Function super DATA, ? extends KEY> classifier,
Supplier> collectorSupplier) {
Objects.requireNonNull(collectorSupplier);
val theMap = new ConcurrentHashMap>();
stream()
.forEach(each -> {
val key = classifier.apply(each);
val collected = theMap.computeIfAbsent(key, __ -> {
val collector = collectorSupplier.get();
return new Collected.ByCollector<>(collector);
});
collected.accumulate(each);
});
val mapBuilder = FuncMap.newBuilder();
theMap
.forEach((key, collected) -> {
val target = collected.finish();
mapBuilder.with(key, target);
});
val map = mapBuilder.build();
return map;
}
//-- min-max --
public default > Optional minBy(
Func1 mapper) {
return terminate(stream -> {
return stream
.min((a,b)->mapper.apply(a).compareTo(mapper.apply(b)));
});
}
public default > Optional maxBy(
Func1 mapper) {
return terminate(stream -> {
return stream
.max((a,b)->mapper.apply(a).compareTo(mapper.apply(b)));
});
}
public default Optional minBy(
Func1 mapper,
Comparator super D> comparator) {
return terminate(stream -> {
return stream
.min((a,b)->comparator.compare(mapper.apply(a), mapper.apply(b)));
});
}
public default Optional maxBy(
Func1 mapper,
Comparator super D> comparator) {
return terminate(stream -> {
return stream
.max((a,b)->comparator.compare(mapper.apply(a), mapper.apply(b)));
});
}
@SuppressWarnings("unchecked")
public default Tuple2, Optional> minMax(
Comparator super DATA> comparator) {
return terminate(stream -> {
val minRef = new AtomicReference