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

functionalj.stream.doublestream.AsDoubleStreamPlusWithCalculate 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.doublestream;

import static functionalj.stream.doublestream.collect.DoubleCollected.collectedOf;

import java.util.function.BinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.Function;
import java.util.function.ObjDoubleConsumer;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;

import functionalj.function.aggregator.DoubleAggregation;
import functionalj.function.aggregator.DoubleAggregationToDouble;
import functionalj.tuple.Tuple;
import functionalj.tuple.Tuple2;
import functionalj.tuple.Tuple3;
import functionalj.tuple.Tuple4;
import functionalj.tuple.Tuple5;
import functionalj.tuple.Tuple6;
import lombok.val;


public interface AsDoubleStreamPlusWithCalculate {
    
    /** @return  the stream plus instance of this object. */
    public DoubleStreamPlus doubleStreamPlus();
    
    public void forEach(DoubleConsumer action);
    
    
    // TODO - Optimize this so the concurrent one can has benefit from the Java implementation
    //        Still not sure how to do that.
    
    /** Perform the calculation using the data of this stream */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public default  RESULT calculate(
            DoubleAggregation aggregation) {
        val collector  = aggregation.doubleCollectorPlus();
        Supplier          supplier    = collector.supplier();
        ObjDoubleConsumer accumulator = collector.doubleAccumulator();
        BinaryOperator    combiner    = collector.combiner();
        Function          finisher    = collector.finisher();
        
        val streamPlus  = doubleStreamPlus();
        val accumulated = streamPlus.doubleStream().collect(supplier, accumulator, (a, b) -> combiner.apply(a, b));
        val value       = finisher.apply(accumulated);
        return (RESULT)value;
    }
    
    /** Perform the calculation using the data of this stream */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public default double calculate(
           DoubleAggregationToDouble aggregation) {
        val collector  = aggregation.doubleCollectorToDoublePlus();
        Supplier          supplier    = collector.supplier();
        ObjDoubleConsumer accumulator = collector.doubleAccumulator();
        BinaryOperator    combiner    = collector.combiner();
        ToDoubleFunction  finisher    = collector.finisherToDouble();
        
        val streamPlus  = doubleStreamPlus();
        val accumulated = streamPlus.doubleStream().collect(supplier, accumulator, (a, b) -> combiner.apply(a, b));
        val value       = finisher.applyAsDouble(accumulated);
        return value;
    }
    
    /** Perform the calculation using the data of this stream */
    public default 
                        Tuple2
                        calculate(
                                DoubleAggregation collector1,
                                DoubleAggregation collector2) {
        // TODO - Created combined collectors
        val collected1 = collectedOf(collector1);
        val collected2 = collectedOf(collector2);
        forEach(each -> {
            collected1.accumulate(each);
            collected2.accumulate(each);
        });
        return Tuple.of(
            collected1.finish(),
            collected2.finish()
        );
    }
    
    /** Perform the calculation using the data of this stream */
    public default 
                        Tuple3
                        calculate(
                                DoubleAggregation collector1,
                                DoubleAggregation collector2,
                                DoubleAggregation collector3) {
        // TODO - Created combined collectors
        val collected1 = collectedOf(collector1);
        val collected2 = collectedOf(collector2);
        val collected3 = collectedOf(collector3);
        forEach(each -> {
            collected1.accumulate(each);
            collected2.accumulate(each);
            collected3.accumulate(each);
        });
        return Tuple.of(
            collected1.finish(),
            collected2.finish(),
            collected3.finish()
        );
    }
    
    /** Perform the calculation using the data of this stream */
    public default 
                        Tuple4
                        calculate(
                                DoubleAggregation collector1,
                                DoubleAggregation collector2,
                                DoubleAggregation collector3,
                                DoubleAggregation collector4) {
        // TODO - Created combined collectors
        val collected1 = collectedOf(collector1);
        val collected2 = collectedOf(collector2);
        val collected3 = collectedOf(collector3);
        val collected4 = collectedOf(collector4);
        forEach(each -> {
            collected1.accumulate(each);
            collected2.accumulate(each);
            collected3.accumulate(each);
            collected4.accumulate(each);
        });
        return Tuple.of(
            collected1.finish(),
            collected2.finish(),
            collected3.finish(),
            collected4.finish()
        );
    }
    
    /** Perform the calculation using the data of this stream */
    public default 
                        Tuple5
                        calculate(
                                DoubleAggregation collector1,
                                DoubleAggregation collector2,
                                DoubleAggregation collector3,
                                DoubleAggregation collector4,
                                DoubleAggregation collector5) {
        // TODO - Created combined collectors
        val collected1 = collectedOf(collector1);
        val collected2 = collectedOf(collector2);
        val collected3 = collectedOf(collector3);
        val collected4 = collectedOf(collector4);
        val collected5 = collectedOf(collector5);
        forEach(each -> {
            collected1.accumulate(each);
            collected2.accumulate(each);
            collected3.accumulate(each);
            collected4.accumulate(each);
            collected5.accumulate(each);
        });
        return Tuple.of(
            collected1.finish(),
            collected2.finish(),
            collected3.finish(),
            collected4.finish(),
            collected5.finish()
        );
    }
    
    /** Perform the calculation using the data of this stream */
    public default 
                        Tuple6
                        calculate(
                                DoubleAggregation collector1,
                                DoubleAggregation collector2,
                                DoubleAggregation collector3,
                                DoubleAggregation collector4,
                                DoubleAggregation collector5,
                                DoubleAggregation collector6) {
        // TODO - Created combined collectors
        val collected1 = collectedOf(collector1);
        val collected2 = collectedOf(collector2);
        val collected3 = collectedOf(collector3);
        val collected4 = collectedOf(collector4);
        val collected5 = collectedOf(collector5);
        val collected6 = collectedOf(collector6);
        forEach(each -> {
            collected1.accumulate(each);
            collected2.accumulate(each);
            collected3.accumulate(each);
            collected4.accumulate(each);
            collected5.accumulate(each);
            collected6.accumulate(each);
        });
        return Tuple.of(
            collected1.finish(),
            collected2.finish(),
            collected3.finish(),
            collected4.finish(),
            collected5.finish(),
            collected6.finish()
        );
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy