functionalj.stream.longstream.AsLongStreamPlusWithStatistic 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.longstream;
import static functionalj.stream.longstream.LongStreamPlusHelper.dummy;
import static functionalj.tuple.LongLongTuple.longTuple;
import java.util.Comparator;
import java.util.LongSummaryStatistics;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.LongFunction;
import java.util.function.LongPredicate;
import java.util.function.LongUnaryOperator;
import functionalj.function.LongComparator;
import functionalj.function.aggregator.LongAggregation;
import functionalj.function.aggregator.LongAggregationToBoolean;
import functionalj.function.aggregator.LongAggregationToLong;
import functionalj.stream.markers.Eager;
import functionalj.stream.markers.Terminal;
import functionalj.tuple.LongLongTuple;
import lombok.val;
public interface AsLongStreamPlusWithStatistic {
public LongStreamPlus longStreamPlus();
/** @return the size of the stream */
@Eager
@Terminal
public default int size() {
val streamPlus = longStreamPlus();
return (int)streamPlus
.count();
}
@Eager
@Terminal
public default long count() {
val streamPlus = longStreamPlus();
return streamPlus
.count();
}
@Eager
@Terminal
public default long sum() {
val streamPlus = longStreamPlus();
return streamPlus
.sum();
}
/** @return the product of all the number */
@Eager
@Terminal
public default OptionalLong product() {
val streamPlus = longStreamPlus();
return streamPlus
.reduce((a, b) -> a*b);
}
@Eager
@Terminal
public default OptionalDouble average() {
val streamPlus = longStreamPlus();
return streamPlus
.average();
}
public default LongSummaryStatistics summaryStatistics() {
val streamPlus = longStreamPlus();
return streamPlus
.summaryStatistics();
}
@Eager
@Terminal
/** Using the comparator, find the minimal value */
public default OptionalLong min() {
val streamPlus = longStreamPlus();
return streamPlus
.min();
}
@Eager
@Terminal
/** Using the comparator, find the maximum value */
public default OptionalLong max() {
val streamPlus = longStreamPlus();
return streamPlus
.max();
}
/** Fund the min value using the comparator */
public default OptionalLong min(LongComparator comparator) {
val streamPlus = longStreamPlus();
return streamPlus
.sorted(comparator)
.findFirst();
}
/** Fund the max value using the comparator */
public default OptionalLong max(LongComparator comparator) {
val streamPlus = longStreamPlus();
return streamPlus
.sorted(comparator.reverse())
.findFirst();
}
/** Return the value whose mapped value is the smallest. */
@Eager
@Terminal
public default > OptionalLong minBy(LongFunction mapper) {
val streamPlus = longStreamPlus();
return streamPlus
.sortedBy(mapper)
.findFirst();
}
/** Return the value whose mapped value is the smallest. */
@Eager
@Terminal
public default > OptionalLong minBy(LongAggregation aggregation) {
val mapper = aggregation.newAggregator();
return minBy(mapper);
}
/** Return the value whose mapped value is the biggest. */
@Eager
@Terminal
public default > OptionalLong maxBy(LongFunction mapper) {
val streamPlus = longStreamPlus();
return streamPlus
.sortedBy(mapper,(a, b) -> Objects.compare(a, b, Comparator.reverseOrder()))
.findFirst();
}
/** Return the value whose mapped value is the smallest. */
@Eager
@Terminal
public default > OptionalLong maxBy(LongAggregation aggregation) {
val mapper = aggregation.newAggregator();
return maxBy(mapper);
}
/** Return the value whose mapped value is the smallest using the comparator. */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Eager
@Terminal
public default OptionalLong minBy(
LongFunction mapper,
Comparator super D> comparator) {
val streamPlus = longStreamPlus();
return streamPlus
.sortedBy(mapper, (Comparator)comparator)
.findFirst();
}
/** Return the value whose mapped value is the smallest using the comparator. */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Eager
@Terminal
public default OptionalLong minBy(
LongAggregation aggregation,
Comparator super D> comparator) {
val mapper = aggregation.newAggregator();
val streamPlus = longStreamPlus();
return streamPlus
.sortedBy(mapper, (Comparator)comparator)
.findFirst();
}
/** Return the value whose mapped value is the biggest using the comparator. */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Eager
@Terminal
public default OptionalLong maxBy(
LongFunction mapper,
Comparator super D> comparator) {
val streamPlus = longStreamPlus();
return streamPlus
.sortedBy(mapper, (Comparator)comparator.reversed())
.findFirst();
}
/** Return the value whose mapped value is the biggest using the comparator. */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Eager
@Terminal
public default OptionalLong maxBy(
LongAggregation aggregation,
Comparator super D> comparator) {
val mapper = aggregation.newAggregator();
val streamPlus = longStreamPlus();
return streamPlus
.sortedBy(mapper, (Comparator)comparator.reversed())
.findFirst();
}
/** Return the value whose mapped value is the smallest mapped int value. */
public default OptionalLong minOf(LongUnaryOperator mapper) {
val streamPlus = longStreamPlus();
val result
= streamPlus
.mapToObj(i -> LongLongTuple.of(i, mapper.applyAsLong(i)))
.min ((a, b) -> Long.compare(a._2, b._2))
.map (t -> t._1);
return result.isPresent()
? OptionalLong.of((long)result.get())
: OptionalLong.empty();
}
/** Return the value whose mapped value is the smallest mapped double value. */
public default OptionalLong minOf(LongAggregationToLong aggregation) {
val mapper = aggregation.newAggregator();
return minOf(mapper);
}
/** Return the value whose mapped value is the largest mapped int value. */
public default OptionalLong maxOf(LongUnaryOperator mapper) {
val streamPlus = longStreamPlus();
Optional