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

functionalj.stream.longstream.collect.DerivedLongCollectorToDoublePlus Maven / Gradle / Ivy

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

import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.DoubleToLongFunction;
import java.util.function.Function;
import java.util.function.IntToLongFunction;
import java.util.function.LongUnaryOperator;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;

import functionalj.stream.collect.CollectorToDoublePlus;
import functionalj.stream.doublestream.collect.DoubleCollectorToDoublePlus;
import functionalj.stream.intstream.collect.IntCollectorToDoublePlus;
import lombok.val;

abstract public class DerivedLongCollectorToDoublePlus {
    
    final LongCollectorToDoublePlus collector;
    
    protected DerivedLongCollectorToDoublePlus(LongCollectorToDoublePlus collector) {
        this.collector = collector;
    }
    
    public Supplier supplier() {
        return collector.supplier();
    }
    
    public BinaryOperator combiner() {
        return collector.combiner();
    }
    
    public ToDoubleFunction finisherToDouble() {
        return collector.finisherToDouble();
    }
    
    public Function finisher() {
        val finisher = finisherToDouble();
        return accumulated -> {
            return finisher.applyAsDouble(accumulated);
        };
    }
    
    public Set characteristics() {
        return collector.characteristics();
    }
    
    //== Implementations ==
    
    public static class FromObj 
                        extends DerivedLongCollectorToDoublePlus
                        implements CollectorToDoublePlus {
        
        private final ToLongFunction mapper;
        
        public FromObj(LongCollectorToDoublePlus collector,
                       ToLongFunction                  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.applyAsLong(s);
                accumulator.accept(a, d);
            };
        }
        
        @Override
        public Collector collector() {
            return this;
        }
    }
    
    public static class FromInt 
                        extends DerivedLongCollectorToDoublePlus
                        implements IntCollectorToDoublePlus {
        
        private final IntToLongFunction mapper;
        
        public  FromInt(LongCollectorToDoublePlus collector, 
                                IntToLongFunction                      mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjIntConsumer intAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsLong(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromLong 
                        extends DerivedLongCollectorToDoublePlus
                        implements LongCollectorToDoublePlus {
        
        private final LongUnaryOperator mapper;
        
        public FromLong(LongCollectorToDoublePlus collector, 
                        LongUnaryOperator                      mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjLongConsumer longAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsLong(s);
                accumulator.accept(a, d);
            };
        }
    }
    
    public static class FromDouble 
                        extends DerivedLongCollectorToDoublePlus
                        implements DoubleCollectorToDoublePlus {
        
        private final DoubleToLongFunction mapper;
        
        public FromDouble(LongCollectorToDoublePlus collector, 
                          DoubleToLongFunction                   mapper) {
            super(collector);
            this.mapper = mapper;
        }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public  ObjDoubleConsumer doubleAccumulator() {
            val accumulator = (BiConsumer)collector.accumulator();
            return (a, s) -> {
                val d = mapper.applyAsLong(s);
                accumulator.accept(a, d);
            };
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy