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

functionalj.stream.Collected Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 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 java.util.Objects;
import java.util.function.BiConsumer;
import java.util.stream.Collector;

import lombok.val;


interface Collected {
    
    @SuppressWarnings("unchecked")
    static  Collected of(
            StreamProcessor processor, 
            Streamable         streamable) {
        Objects.requireNonNull(processor);
        if (processor instanceof Collector)
            return new ByCollector(((Collector)processor));
        
        Objects.requireNonNull(streamable);
        return new ByStreamProcessor<>(processor, streamable);
    }
    
    public void   accumulate(DATA each);
    public RESULT finish();
    
    
    static  CollectedInt ofInt(IntCollectorPlus collector) {
        return new CollectedInt(collector);
    }
    static  CollectedLong ofLong(LongCollectorPlus collector) {
        return new CollectedLong(collector);
    }
    static  CollectedDouble ofDouble(DoubleCollectorPlus collector) {
        return new CollectedDouble(collector);
    }
    
    
    static class ByCollector 
            implements
                StreamProcessor,
                Collected{
        
        private final Collector collector;
        private final BiConsumer        accumulator;
        private final ACCUMULATED                          accumulated;
        
        ByCollector(Collector collector) {
            this.collector   = collector;
            this.accumulated = collector.supplier().get();
            this.accumulator = collector.accumulator();
        }
        
        public void accumulate(DATA each) {
            accumulator.accept(accumulated, each);
        }
        
        public RESULT finish() {
            return collector.finisher().apply(accumulated);
        }
        
        @Override
        public RESULT process(StreamPlus stream) {
            return stream.calculate(collector);
        }
    }
    
    static class ByStreamProcessor
            implements
                Collected {
        
        private final StreamProcessor processor;
        private final Streamable              streamable;
        
        ByStreamProcessor(
                StreamProcessor processor, 
                Streamable              stream) {
            this.processor  = processor;
            this.streamable = stream;
        }
        
        public void accumulate(DATA each) {
        }
        
        public RESULT finish() {
            val stream = streamable.stream();
            return processor.process(stream);
        }
    }
    
    static class CollectedInt {
        
        private final IntCollectorPlus collector;
        private final IntAccumulator           accumulator;
        private final ACCUMULATED                           accumulated;
        
        CollectedInt(IntCollectorPlus collector) {
            this.collector   = collector;
            this.accumulated = collector.supplier().get();
            this.accumulator = collector.intAccumulator();
        }
        
        public void accumulate(int each) {
            accumulator.accept(accumulated, each);
        }
        
        public RESULT finish() {
            val finisher = collector.finisher();
            return finisher.apply(accumulated);
        }
        
    }
    
    static class CollectedLong {
        
        private final LongCollectorPlus collector;
        private final LongAccumulator           accumulator;
        private final ACCUMULATED                            accumulated;
        
        CollectedLong(LongCollectorPlus collector) {
            this.collector   = collector;
            this.accumulated = collector.supplier().get();
            this.accumulator = collector.longAccumulator();
        }
        
        public void accumulate(long each) {
            accumulator.accept(accumulated, each);
        }
        
        public RESULT finish() {
            val finisher = collector.finisher();
            return finisher.apply(accumulated);
        }
        
    }
    
    static class CollectedDouble {
        
        private final DoubleCollectorPlus collector;
        private final DoubleAccumulator           accumulator;
        private final ACCUMULATED                              accumulated;
        
        CollectedDouble(DoubleCollectorPlus collector) {
            this.collector   = collector;
            this.accumulated = collector.supplier().get();
            this.accumulator = collector.doubleAccumulator();
        }
        
        public void accumulate(double each) {
            accumulator.accept(accumulated, each);
        }
        
        public RESULT finish() {
            val finisher = collector.finisher();
            return finisher.apply(accumulated);
        }
        
    }
    
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy