functionalj.stream.AsStreamPlusWithStatistic Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 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.function.Func.alwaysTrue;
import static functionalj.stream.StreamPlusHelper.dummy;
import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Predicate;
import functionalj.function.aggregator.Aggregation;
import functionalj.function.aggregator.AggregationToBoolean;
import functionalj.stream.markers.Eager;
import functionalj.stream.markers.Terminal;
import functionalj.tuple.Tuple;
import functionalj.tuple.Tuple2;
import lombok.val;
public interface AsStreamPlusWithStatistic {
public StreamPlus streamPlus();
/** @return the size of the stream */
@Eager
@Terminal
public default int size() {
val streamPlus = streamPlus();
return (int)streamPlus
.count();
}
@Eager
@Terminal
public default long count() {
val streamPlus = streamPlus();
return streamPlus
.count();
}
public default Optional min(Comparator super DATA> comparator) {
val streamPlus = streamPlus();
return streamPlus
.min(comparator);
}
public default Optional max(Comparator super DATA> comparator) {
val streamPlus = streamPlus();
return streamPlus
.max(comparator);
}
/** Return the value whose mapped value is the smallest. */
@Eager
@Terminal
public default > Optional minBy(Function mapper) {
val streamPlus = streamPlus();
return streamPlus
.min((a,b) -> {
val mappedA = mapper.apply(a);
val mappedB = mapper.apply(b);
return mappedA.compareTo(mappedB);
});
}
/** Return the value whose mapped value is the smallest. */
@Eager
@Terminal
public default > Optional minBy(Aggregation aggregation) {
val mapper = aggregation.newAggregator();
return minBy(mapper);
}
/** Return the value whose mapped value is the biggest. */
@Eager
@Terminal
public default > Optional maxBy(Function mapper) {
val streamPlus = streamPlus();
return streamPlus
.max((a,b) -> {
val mappedA = mapper.apply(a);
val mappedB = mapper.apply(b);
return mappedA.compareTo(mappedB);
});
}
/** Return the value whose mapped value is the biggest. */
@Eager
@Terminal
public default > Optional maxBy(Aggregation aggregation) {
val mapper = aggregation.newAggregator();
return maxBy(mapper);
}
/** Return the value whose mapped value is the smallest using the comparator. */
@Eager
@Terminal
public default Optional minBy(
Function mapper,
Comparator super D> comparator) {
val streamPlus = streamPlus();
return streamPlus
.min((a,b) -> {
val mappedA = mapper.apply(a);
val mappedB = mapper.apply(b);
return comparator.compare(mappedA, mappedB);
});
}
/** Return the value whose mapped value is the smallest using the comparator. */
@Eager
@Terminal
public default Optional minBy(
Aggregation aggregation,
Comparator super D> comparator) {
val mapper = aggregation.newAggregator();
return minBy(mapper, comparator);
}
/** Return the value whose mapped value is the biggest using the comparator. */
@Eager
@Terminal
public default Optional maxBy(
Function mapper,
Comparator super D> comparator) {
val streamPlus = streamPlus();
return streamPlus
.max((a,b) -> {
val mappedA = mapper.apply(a);
val mappedB = mapper.apply(b);
return comparator.compare(mappedA, mappedB);
});
}
/** Return the value whose mapped value is the biggest using the comparator. */
@Eager
@Terminal
public default Optional maxBy(
Aggregation aggregation,
Comparator super D> comparator) {
val mapper = aggregation.newAggregator();
return maxBy(mapper, comparator);
}
/** Return the value is the smallest and the biggest using the comparator. */
@Eager
@Terminal
@SuppressWarnings("unchecked")
public default Optional> minMax(Comparator super DATA> comparator) {
val streamPlus = streamPlus();
val minRef = new AtomicReference