All Downloads are FREE. Search and download functionalities are using the official Maven repository.

functionalj.stream.AsStreamPlusWithStatistic Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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 comparator) {
        val streamPlus = streamPlus();
        return streamPlus
                .min(comparator);
    }
    
    public default Optional max(Comparator 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 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 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 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 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 comparator) {
        val streamPlus = streamPlus();
        val minRef = new AtomicReference(dummy);
        val maxRef = new AtomicReference(dummy);
        streamPlus
            .sorted(comparator)
            .forEach(each -> {
                minRef.compareAndSet(StreamPlusHelper.dummy, each);
                maxRef.set(each);
            });
        val min = minRef.get();
        val max = maxRef.get();
        return (dummy.equals(min) || dummy.equals(max))
                ? Optional.empty()
                : Optional.of(Tuple2.of((DATA)min, (DATA)max));
    }
    
    /** Return the value whose mapped value is the smallest and the biggest. */
    @Eager
    @Terminal
    @SuppressWarnings("unchecked")
    public default > Optional> minMaxBy(Function mapper) {
        val streamPlus = streamPlus();
        val minRef = new AtomicReference(dummy);
        val maxRef = new AtomicReference(dummy);
        streamPlus
        .sortedBy(mapper)
        .forEach(each -> {
            minRef.compareAndSet(dummy, each);
            maxRef.set(each);
        });
        
        val min = minRef.get();
        val max = maxRef.get();
        return (dummy.equals(min) || dummy.equals(max))
                ? Optional.empty()
                : Optional.of(Tuple2.of((DATA)min, (DATA)max));
    }
    
    /** Return the value whose mapped value is the smallest and the biggest. */
    @Eager
    @Terminal
    public default > Optional> minMaxBy(Aggregation aggregation) {
        val mapper = aggregation.newAggregator();
        return minMaxBy(mapper);
    }
    
    /** Return the value whose mapped value is the smallest and the biggest using the comparator. */
    @Eager
    @Terminal
    @SuppressWarnings("unchecked")
    public default  Optional> minMaxBy(
            Function     mapper, 
            Comparator comparator) {
        val streamPlus = streamPlus();
        val minRef = new AtomicReference(StreamPlusHelper.dummy);
        val maxRef = new AtomicReference(StreamPlusHelper.dummy);
        streamPlus
        .sortedBy(mapper, (i1, i2)->comparator.compare(i1, i2))
        .forEach(each -> {
            minRef.compareAndSet(StreamPlusHelper.dummy, each);
            maxRef.set(each);
        });
        
        val min = minRef.get();
        val max = maxRef.get();
        return (dummy.equals(min) || dummy.equals(max))
                ? Optional.empty()
                : Optional.of(Tuple2.of((DATA)min, (DATA)max));
    }
    
    /** Return the value whose mapped value is the smallest and the biggest using the comparator. */
    @Eager
    @Terminal
    public default  Optional> minMaxBy(
            Aggregation  aggregation, 
            Comparator comparator) {
        val mapper = aggregation.newAggregator();
        return minMaxBy(mapper, comparator);
    }
    
    /** Map each value using the mapper to a comparable value and use it to find a minimal value then return the index */
    public default > Optional minIndexBy(Function mapper) {
        return minIndexBy(alwaysTrue(), mapper);
    }
    
    /** Map each value using the mapper to a comparable value and use it to find a minimal value then return the index */
    public default > Optional minIndexBy(Aggregation aggregation) {
        val mapper = aggregation.newAggregator();
        return minIndexBy(alwaysTrue(), mapper);
    }
    
    /** Map each value using the mapper to a comparable value and use it to find a maximum value then return the index */
    public default > Optional maxIndexBy(Function mapper) {
        return maxIndexBy(alwaysTrue(), mapper);
    }
    
    /** Map each value using the mapper to a comparable value and use it to find a maximum value then return the index */
    public default > Optional maxIndexBy(Aggregation aggregation) {
        val mapper = aggregation.newAggregator();
        return maxIndexBy(alwaysTrue(), mapper);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional minIndexBy(
            Predicate   filter,
            Function mapper) {
        val streamPlus = streamPlus();
        return streamPlus
                .mapWithIndex(Tuple::of)
                .filter(t -> filter.test(t._2))
                .minBy (t -> mapper.apply(t._2))
                .map   (t -> t._1);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional minIndexBy(
            AggregationToBoolean aggregationFilter,
            Function          mapper) {
        val filter = aggregationFilter.newAggregator();
        return minIndexBy(filter, mapper);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional minIndexBy(
            Predicate   filter,
            Aggregation aggregationMapper) {
        val mapper = aggregationMapper.newAggregator();
        return minIndexBy(filter, mapper);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional minIndexBy(
            AggregationToBoolean aggregationFilter,
            Aggregation       aggregationMapper) {
        val filter = aggregationFilter.newAggregator();
        val mapper = aggregationMapper.newAggregator();
        return minIndexBy(filter, mapper);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a maximum value then return the index */
    public default > Optional maxIndexBy(
            Predicate   filter,
            Function mapper) {
        val streamPlus = streamPlus();
        return streamPlus
                .mapWithIndex(Tuple::of)
                .filter(t -> filter.test(t._2))
                .maxBy (t -> mapper.apply(t._2))
                .map   (t -> t._1);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional maxIndexBy(
            AggregationToBoolean aggregationFilter,
            Function          mapper) {
        val filter = aggregationFilter.newAggregator();
        return maxIndexBy(filter, mapper);
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional maxIndexBy(
            Predicate   filter,
            Aggregation aggregationMapper) {
        val mapper = aggregationMapper.newAggregator();
        return maxIndexBy(filter, mapper);
        
    }
    
    /** Using the mapper to map each value that passes the filter to a comparable and use it to find a minimal value then return the index */
    public default > Optional maxIndexBy(
            AggregationToBoolean aggregationFilter,
            Aggregation       aggregationMapper) {
        val filter = aggregationFilter.newAggregator();
        val mapper = aggregationMapper.newAggregator();
        return maxIndexBy(filter, mapper);
    }
    
}