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

com.github.underscore.math.$ Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
/*
 * The MIT License (MIT)
 *
 * Copyright 2015 Valentyn Kolesnikov
 *
 * 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 com.github.underscore.math;

import com.github.underscore.Block;
import com.github.underscore.Function1;
import com.github.underscore.FunctionAccum;
import com.github.underscore.Predicate;
import com.github.underscore.Tuple;
import com.github.underscore.Optional;
import java.util.*;

public class $ extends com.github.underscore.$ {
    public $(final Iterable iterable) {
        super(iterable);
    }

    public $(final String string) {
        super(string);
    }

    public static class Chain extends com.github.underscore.$.Chain {
        public Chain(final T item) {
            super(item);
        }

        public Chain(final List list) {
            super(list);
        }

        public Chain first() {
            return new Chain($.first(value()));
        }

        public Chain first(int n) {
            return new Chain($.first(value(), n));
        }

        public Chain initial() {
            return new Chain($.initial(value()));
        }

        public Chain initial(int n) {
            return new Chain($.initial(value(), n));
        }

        public Chain last() {
            return new Chain($.last(value()));
        }

        public Chain last(int n) {
            return new Chain($.last(value(), n));
        }

        public Chain rest() {
            return new Chain($.rest(value()));
        }

        public Chain rest(int n) {
            return new Chain($.rest(value(), n));
        }

        public Chain compact() {
            return new Chain($.compact(value()));
        }

        public Chain compact(final T falsyValue) {
            return new Chain($.compact(value(), falsyValue));
        }

        @SuppressWarnings("unchecked")
        public Chain flatten() {
            return new Chain((List) $.flatten(value()));
        }

        public  Chain map(final Function1 func) {
            return new Chain($.map(value(), func));
        }

        public Chain filter(final Predicate pred) {
            return new Chain($.filter(value(), pred));
        }

        public Chain reject(final Predicate pred) {
            return new Chain($.reject(value(), pred));
        }

        public  Chain reduce(final FunctionAccum func, final F zeroElem) {
            return new Chain($.reduce(value(), func, zeroElem));
        }

        public  Chain reduceRight(final FunctionAccum func, final F zeroElem) {
            return new Chain($.reduceRight(value(), func, zeroElem));
        }

        public Chain> find(final Predicate pred) {
            return new Chain>($.find(value(), pred));
        }

        public Chain> findLast(final Predicate pred) {
            return new Chain>($.findLast(value(), pred));
        }

        @SuppressWarnings("unchecked")
        public Chain max() {
            return new Chain($.max((Collection) value()));
        }

        public > Chain max(final Function1 func) {
            return new Chain($.max(value(), func));
        }

        @SuppressWarnings("unchecked")
        public Chain min() {
            return new Chain($.min((Collection) value()));
        }

        public > Chain min(final Function1 func) {
            return new Chain($.min(value(), func));
        }

        @SuppressWarnings("unchecked")
        public Chain sort() {
            return new Chain($.sort((List) value()));
        }

        public > Chain sortBy(final Function1 func) {
            return new Chain($.sortBy(value(), func));
        }

        @SuppressWarnings("unchecked")
        public  Chain> sortBy(final K key) {
            return new Chain>($.sortBy((List>) value(), key));
        }

        public  Chain>> groupBy(final Function1 func) {
            return new Chain>>($.groupBy(value(), func));
        }

        public Chain>> indexBy(final String property) {
            return new Chain>>($.indexBy(value(), property));
        }

        public  Chain> countBy(final Function1 func) {
            return new Chain>($.countBy(value(), func));
        }

        public Chain shuffle() {
            return new Chain($.shuffle(value()));
        }

        public Chain sample() {
            return new Chain($.sample(value()));
        }

        public Chain sample(final int howMany) {
            return new Chain($.newArrayList($.sample(value(), howMany)));
        }

        public Chain tap(final Block func) {
            $.tap(value(), func);
            return new Chain(value());
        }

        public Chain every(final Predicate pred) {
            return new Chain($.every(value(), pred));
        }

        public Chain some(final Predicate pred) {
            return new Chain($.some(value(), pred));
        }

        public Chain contains(final T elem) {
            return new Chain($.contains(value(), elem));
        }

        public Chain invoke(final String methodName, final List args) {
            return new Chain($.invoke(value(), methodName, args));
        }

        public Chain invoke(final String methodName) {
            return new Chain($.invoke(value(), methodName));
        }

        public Chain pluck(final String propertyName) {
            return new Chain($.pluck(value(), propertyName));
        }

        public  Chain where(final List> properties) {
            return new Chain($.where(value(), properties));
        }

        public  Chain> findWhere(final List> properties) {
            return new Chain>($.findWhere(value(), properties));
        }

        public Chain uniq() {
            return new Chain($.uniq(value()));
        }

        @SuppressWarnings("unchecked")
        public  Chain uniq(final Function1 func) {
            return new Chain($.newArrayList((Iterable) $.uniq(value(), func)));
        }

        public Chain union(final List ... lists) {
            return new Chain($.union(value(), lists));
        }

        public Chain intersection(final List ... lists) {
            return new Chain($.intersection(value(), lists));
        }

        public Chain difference(final List ... lists) {
            return new Chain($.difference(value(), lists));
        }

        public Chain range(final int stop) {
            return new Chain(newIntegerList($.range(stop)));
        }

        public Chain range(final int start, final int stop) {
            return new Chain(newIntegerList($.range(start, stop)));
        }

        public Chain range(final int start, final int stop, final int step) {
            return new Chain(newIntegerList($.range(start, stop, step)));
        }

        public Chain> chunk(final int size) {
            return new Chain>($.chunk(value(), size));
        }

        public Chain concat(final List ... lists) {
            return new Chain($.concat(value(), lists));
        }

        public Chain slice(final int start) {
            return new Chain($.slice(value(), start));
        }

        public Chain slice(final int start, final int end) {
            return new Chain($.slice(value(), start, end));
        }

        public Chain reverse() {
            return new Chain($.reverse(value()));
        }

        public Chain join() {
            return new Chain($.join(value()));
        }

        public Chain join(final String separator) {
            return new Chain($.join(value(), separator));
        }

        public Chain skip(final int numberToSkip) {
            return new Chain(value().subList(numberToSkip, value().size()));
        }

        public Chain limit(final int size) {
            return new Chain(value().subList(0, size));
        }

        @SuppressWarnings("unchecked")
        public  Chain> toMap() {
            return new Chain>($.toMap((Iterable>) value()));
        }

        @SuppressWarnings("unchecked")
        public  Chain sum() {
            return new Chain($.sum((List) value()));
        }

        public  Chain sum(final Function1 func) {
            return new Chain($.sum(value(), func));
        }

        @SuppressWarnings("unchecked")
        public Chain mean() {
            return new Chain($.mean((List) value()));
        }

        @SuppressWarnings("unchecked")
        public Chain median() {
            return new Chain($.median((List) value()));
        }
    }

    public static  Chain chain(final List list) {
        return new $.Chain(list);
    }

    public static  Chain chain(final Iterable iterable) {
        return new $.Chain(newArrayList(iterable));
    }

    public static  Chain chain(final T ... array) {
        return new $.Chain(Arrays.asList(array));
    }

    @SuppressWarnings("unchecked")
    public Chain chain() {
        return new $.Chain(newArrayList(value()));
    }

    public static  T sum(final Iterable iterable) {
        T result = null;
        for (final T item : iterable) {
            result = sum(result, item);
        }
        return result;
    }

    public static  F sum(final Iterable iterable, final Function1 func) {
        F result = null;
        for (final E item : iterable) {
            result = sum(result, func.apply(item));
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    public  T sum() {
        return (T) sum((List) getIterable());
    }

    @SuppressWarnings("unchecked")
    public  F sum(final Function1 func) {
        return (F) sum((List) getIterable(), func);
    }

    @SuppressWarnings("unchecked")
    private static  T sum(final T first, final T second) {
        if (first == null) {
            return second;
        } else if (second == null) {
            return first;
        } else if (first instanceof java.math.BigDecimal) {
            return (T) sum((java.math.BigDecimal) first, (java.math.BigDecimal) second);
        } else if (second instanceof java.math.BigInteger) {
            return (T) sum((java.math.BigInteger) first, (java.math.BigInteger) second);
        } else if (first instanceof Byte) {
            return (T) sum((Byte) first, (Byte) second);
        } else if (first instanceof Double) {
            return (T) sum((Double) first, (Double) second);
        } else if (first instanceof Float) {
            return (T) sum((Float) first, (Float) second);
        } else if (first instanceof Integer) {
            return (T) sum((Integer) first, (Integer) second);
        } else if (first instanceof Long) {
            return (T) sum((Long) first, (Long) second);
        } else if (first instanceof Short) {
            return (T) sum((Short) first, (Short) second);
        } else {
            throw new UnsupportedOperationException("Sum only supports official subclasses of Number");
        }
    }

    private static java.math.BigDecimal sum(java.math.BigDecimal first, java.math.BigDecimal second) {
        return first.add(second);
    }

    private static java.math.BigInteger sum(java.math.BigInteger first, java.math.BigInteger second) {
        return first.add(second);
    }

    private static Byte sum(Byte first, Byte second) {
        return (byte) (first + second);
    }

    private static Double sum(Double first, Double second) {
        return first + second;
    }

    private static Float sum(Float first, Float second) {
        return first + second;
    }

    private static Integer sum(Integer first, Integer second) {
        return first + second;
    }

    private static Long sum(Long first, Long second) {
        return first + second;
    }

    private static Short sum(Short first, Short second) {
        return (short) (first + second);
    }

    public static  double mean(final Iterable iterable) {
        T result = null;
        int count = 0;
        for (final T item : iterable) {
            result = sum(result, item);
            count += 1;
        }
        return result.doubleValue() / count;
    }

    @SuppressWarnings("unchecked")
    public double mean() {
        return mean((Iterable) getIterable());
    }

    @SuppressWarnings("unchecked")
    public static  double median(final Iterable iterable) {
        final List result = newArrayList((Collection) iterable);
        final int size = size(iterable);
        if (size % 2 != 0) {
            return result.get(size / 2).doubleValue();
        }
        return (result.get(size / 2 - 1).doubleValue() + result.get(size / 2).doubleValue()) / 2;
    }

    @SuppressWarnings("unchecked")
    public double median() {
        return median((Iterable) getIterable());
    }

    public static void main(String ... args) {
        final String message = "Underscore-java-math is a math plugin for underscore-java.\n\n"
            + "For docs, license, tests, and downloads, see: http://javadev.github.io/underscore-java";
        System.out.println(message);
    }
}