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

functionalj.stream.collect.DerivedCollectorPlus Maven / Gradle / Ivy

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

import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.LongFunction;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;

import functionalj.stream.doublestream.collect.DoubleCollectorPlus;
import functionalj.stream.intstream.collect.IntCollectorPlus;
import functionalj.stream.longstream.collect.LongCollectorPlus;
import lombok.val;

abstract public class DerivedCollectorPlus {
    
    final Collector collector;
    
    protected DerivedCollectorPlus(Collector collector) {
        this.collector = collector;
    }
    
    public Supplier supplier() {
        return collector.supplier();
    }
    
    public BinaryOperator combiner() {
        return collector.combiner();
    }
    
    public Function finisher() {
        return collector.finisher();
    }
    
    public Set characteristics() {
        return collector.characteristics();
    }
    
    //== Implementations ==
    
    public static class FromObj 
                            extends DerivedCollectorPlus
                            implements CollectorPlus {
        
        private final Function mapper;
        
        public  FromObj(Collector collector, 
                                Function                mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @Override
        public Collector collector() {
            return this;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public BiConsumer accumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.apply(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromInt 
                        extends DerivedCollectorPlus
                        implements IntCollectorPlus {
        
        private final IntFunction mapper;
        
        public  FromInt(Collector collector, 
                                IntFunction                    mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjIntConsumer intAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.apply(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromLong 
                            extends DerivedCollectorPlus
                            implements LongCollectorPlus {
        
        private final LongFunction mapper;
        
        public  FromLong(Collector collector, 
                                 LongFunction                    mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjLongConsumer longAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.apply(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromDouble 
                    extends DerivedCollectorPlus
                    implements DoubleCollectorPlus {
        
        private final DoubleFunction mapper;
        
        public  FromDouble(Collector collector, 
                                   DoubleFunction                    mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjDoubleConsumer doubleAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.apply(s);
                accumulator.accept(a, d);
            };
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy