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

functionalj.functions.StatFuncs Maven / Gradle / Ivy

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

import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;

import functionalj.function.Func1;
import functionalj.stream.DoubleStreamElementProcessor;
import functionalj.stream.IntStreamElementProcessor;
import functionalj.stream.LongStreamElementProcessor;
import functionalj.stream.StreamElementProcessor;
import functionalj.tuple.Tuple2;

public class StatFuncs {
    
    public static  Func1, Tuple2> toIntPercentiles() {
        return tuple -> tuple.map2(Double::intValue);
    }
    
    public static  Func1, T> toPercentileElements() {
        return tuple -> tuple._1();
    }
    
    public static  Func1, Double> toPercentileValues() {
        return tuple -> tuple._2();
    }
    
    public static  Func1, Integer> toPercentileIntValues() {
        return tuple -> tuple._2().intValue();
    }
    
    // == Min ==
    
    public static  StreamElementProcessor minOf(ToIntFunction mapper) {
        return minAsInt().of(mapper);
    }
    
    public static  StreamElementProcessor minOf(ToLongFunction mapper) {
        return minAsLong().of(mapper);
    }
    
    public static  StreamElementProcessor minOf(ToDoubleFunction mapper) {
        return minAsDouble().of(mapper);
    }
    
    public static IntStreamElementProcessor minAsInt() {
        return new IntStreamElementProcessor() {
            private Integer minValue = null;
            @Override
            public void processElement(long index, int element) {
                if (minValue == null)
                    minValue = Integer.MAX_VALUE;
                minValue = (minValue < element) ? minValue : element;
            }
            @Override
            public Integer processComplete(long count) {
                return minValue;
            }
        };
    }
    
    public static LongStreamElementProcessor minAsLong() {
        return new LongStreamElementProcessor() {
            private Long minValue = Long.MAX_VALUE;
            @Override
            public void processElement(long index, long element) {
                if (minValue == null)
                    minValue = Long.MAX_VALUE;
                minValue = (minValue < element) ? minValue : element;
            }
            @Override
            public Long processComplete(long count) {
                return minValue;
            }
        };
    }
    
    public static DoubleStreamElementProcessor minAsDouble() {
        return new DoubleStreamElementProcessor() {
            private Double minValue = Double.MAX_VALUE;
            @Override
            public void processElement(long index, double element) {
                if (minValue == null)
                    minValue = Double.MAX_VALUE;
                minValue = (minValue < element) ? minValue : element;
            }
            @Override
            public Double processComplete(long count) {
                return minValue;
            }
        };
    }
    
    // == Max ==
    
    public static  StreamElementProcessor maxOf(ToIntFunction mapper) {
        return maxAsInt().of(mapper);
    }
    
    public static  StreamElementProcessor maxOf(ToLongFunction mapper) {
        return maxAsLong().of(mapper);
    }
    
    public static  StreamElementProcessor maxOf(ToDoubleFunction mapper) {
        return maxAsDouble().of(mapper);
    }
    
    public static IntStreamElementProcessor maxAsInt() {
        return new IntStreamElementProcessor() {
            private Integer maxValue = null;
            @Override
            public void processElement(long index, int element) {
                if (maxValue == null)
                    maxValue = Integer.MIN_VALUE;
                maxValue = (maxValue > element) ? maxValue : element;
            }
            @Override
            public Integer processComplete(long count) {
                return maxValue;
            }
        };
    }
    
    public static LongStreamElementProcessor maxAsLong() {
        return new LongStreamElementProcessor() {
            private Long maxValue = null;
            @Override
            public void processElement(long index, long element) {
                if (maxValue == null)
                    maxValue = Long.MIN_VALUE;
                maxValue = (maxValue > element) ? maxValue : element;
            }
            @Override
            public Long processComplete(long count) {
                return maxValue;
            }
        };
    }
    
    public static DoubleStreamElementProcessor maxAsDouble() {
        return new DoubleStreamElementProcessor() {
            private Double maxValue = null;
            @Override
            public void processElement(long index, double element) {
                if (maxValue == null)
                    maxValue = -Double.MAX_VALUE;
                maxValue = (maxValue > element) ? maxValue : element;
            }
            @Override
            public Double processComplete(long count) {
                return maxValue;
            }
        };
    }
    
    // TODO - Range, Sum, Mean, Medium, Mode, Variance, StandardDeviation, Quantiles
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy