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

functionalj.stream.AsStreamPlusWithCalculate 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.stream.collect.Collected.collectedOf;

import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import functionalj.function.aggregator.Aggregation;
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 AsStreamPlusWithCalculate {
    
    /** @return  the stream plus instance of this object. */
    public StreamPlus streamPlus();
    
    public void forEach(Consumer action);
    
    
    // TODO - Optimize this so the concurrent one can has benefit from the Java implementation
    //        Still not sure how to do that properly.
    
    /** Perform the calculation using the data of this stream */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public default  RESULT calculate(Aggregation aggregation) {
        val collector  = aggregation.collectorPlus();
        Supplier       supplier    = collector.supplier();
        BiConsumer     accumulator = collector.accumulator();
        BinaryOperator combiner    = collector.combiner();
        Function       finisher    = collector.finisher();
        
        val streamPlus  = streamPlus();
        val accumulated = streamPlus.stream().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 */
    public default 
                        Tuple2 
                        calculate(
                                Aggregation collector1,
                                Aggregation collector2) {
        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(
                                Aggregation collector1,
                                Aggregation collector2,
                                Aggregation collector3) {
        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(
                                Aggregation collector1,
                                Aggregation collector2,
                                Aggregation collector3,
                                Aggregation collector4) {
        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(
                                Aggregation collector1,
                                Aggregation collector2,
                                Aggregation collector3,
                                Aggregation collector4,
                                Aggregation collector5) {
        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(
                                Aggregation collector1,
                                Aggregation collector2,
                                Aggregation collector3,
                                Aggregation collector4,
                                Aggregation collector5,
                                Aggregation collector6) {
        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