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

functionalj.stream.intstream.AsIntStreamPlusWithCollect 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.intstream;

import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;

import functionalj.function.Func;
import functionalj.function.aggregator.IntAggregation;
import functionalj.function.aggregator.IntAggregationToDouble;
import functionalj.function.aggregator.IntAggregationToInt;
import functionalj.function.aggregator.IntAggregationToLong;
import functionalj.stream.intstream.collect.IntCollectorPlus;
import functionalj.stream.markers.Eager;
import functionalj.stream.markers.Terminal;
import lombok.val;

public interface AsIntStreamPlusWithCollect {
    
    public IntStreamPlus intStreamPlus();
    
    
    /**
     * Performs a mutable reduction operation on the elements of this stream. A mutable reduction is one in which the reduced value is
     * a mutable result container, such as an {@code ArrayList}, and elements are incorporated by updating the state of the result rather
     * than by replacing the result.
     **/
    @Eager
    @Terminal
    public default  RESULT collect(
            Supplier           supplier,
            ObjIntConsumer     accumulator,
            BiConsumer combiner) {
        val streamPlus = intStreamPlus();
        return streamPlus
                .collect(supplier, accumulator, combiner);
    }
    
    @Eager
    @Terminal
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public default  RESULT collect(IntCollectorPlus collector) {
        val supplier = (Supplier)collector.supplier();
        val combiner = Func.f((ACCUMULATOR r1, ACCUMULATOR r2) -> {
            BinaryOperator simpleCombiner = collector.combiner();
            simpleCombiner.apply(r1, r2);
        });
        ObjIntConsumer accumulator = (ACCUMULATOR r, int v) -> {
            // This is ridiculous but work. Sorry.
            Object      objectR           = (Object)r;
            ACCUMULATOR resultR           = (ACCUMULATOR)objectR;
            BiConsumer  simpleAccumulator = collector.accumulator();
            simpleAccumulator.accept(resultR, v);
        };
        val finisher    = collector.finisher();
        val streamPlus  = intStreamPlus();
        val accumulated = streamPlus.collect(supplier, accumulator, combiner);
        val result      = finisher.apply((ACCUMULATOR)accumulated);
        return result;
    }
    
    @Eager
    @Terminal
    public default  RESULT aggregate(IntAggregation aggregation) {
        val collector = aggregation.intCollectorPlus();
        return collect(collector);
    }
    
    @Eager
    @Terminal
    public default int aggregateToInt(IntAggregationToInt aggregation) {
        val collector = aggregation.intCollectorPlus();
        return collect(collector);
    }
    
    @Eager
    @Terminal
    public default long aggregateToLong(IntAggregationToLong aggregation) {
        val collector = aggregation.intCollectorPlus();
        return collect(collector);
    }
    
    @Eager
    @Terminal
    public default double aggregateToDouble(IntAggregationToDouble aggregation) {
        val collector = aggregation.intCollectorPlus();
        return collect(collector);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy