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

functionalj.stream.intstream.collect.DerivedIntCollectorToIntPlus Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
package functionalj.stream.intstream.collect;

import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.DoubleToIntFunction;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;
import java.util.function.LongToIntFunction;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;

import functionalj.stream.collect.CollectorToIntPlus;
import functionalj.stream.doublestream.collect.DoubleCollectorToIntPlus;
import functionalj.stream.longstream.collect.LongCollectorToIntPlus;
import lombok.val;

abstract public class DerivedIntCollectorToIntPlus {
    
    final IntCollectorToIntPlus collector;
    
    protected DerivedIntCollectorToIntPlus(IntCollectorToIntPlus collector) {
        this.collector = collector;
    }
    
    public Supplier supplier() {
        return collector.supplier();
    }
    
    public BinaryOperator combiner() {
        return collector.combiner();
    }
    
    public ToIntFunction finisherToInt() {
        return collector.finisherToInt();
    }
    
    public Function finisher() {
        return a -> finisherToInt().applyAsInt(a);
    }
    
    public Set characteristics() {
        return collector.characteristics();
    }
    
    //== Implementations ==
    
    public static class FromObj 
                        extends DerivedIntCollectorToIntPlus
                        implements CollectorToIntPlus {
        
        private final ToIntFunction mapper;
        
        public FromObj(IntCollectorToIntPlus collector,
                       ToIntFunction               mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings("unchecked")
        @Override
        public BiConsumer accumulator(){
            @SuppressWarnings("rawtypes")
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsInt(s);
                accumulator.accept(a, d);
            };
        }
        
        @Override
        public Collector collector() {
            return this;
        }
    }
    
    public static class FromInt 
                        extends DerivedIntCollectorToIntPlus
                        implements IntCollectorToIntPlus {
        
        private final IntUnaryOperator mapper;
        
        public  FromInt(IntCollectorToIntPlus collector, 
                                IntUnaryOperator                   mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjIntConsumer intAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsInt(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromLong 
                        extends DerivedIntCollectorToIntPlus
                        implements LongCollectorToIntPlus {
        
        private final LongToIntFunction mapper;
        
        public FromLong(IntCollectorToIntPlus collector, 
                        LongToIntFunction                  mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjLongConsumer longAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsInt(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromDouble 
                        extends DerivedIntCollectorToIntPlus
                        implements DoubleCollectorToIntPlus {
        
        private final DoubleToIntFunction mapper;
        
        public FromDouble(IntCollectorToIntPlus collector, 
                          DoubleToIntFunction                mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjDoubleConsumer doubleAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsInt(s);
                accumulator.accept(a, d);
            };
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy