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

com.jillesvangurp.iterables.Reducers Maven / Gradle / Ivy

Go to download

Misc classes to make processing utf-8 content with iterators a bit less painful.

The newest version!
package com.jillesvangurp.iterables;

import java.math.BigDecimal;

public class Reducers {

    private static final class AddingReducer implements Reducer {
        @Override
        public T reduce(T input) {
            return input;
        }

        @SuppressWarnings("unchecked")
        @Override
        public T reduce(T cumulative, T input) {
            Class clazz = cumulative.getClass();
            if(clazz.isAssignableFrom(Integer.class)) {
                Integer result = (Integer)input + (Integer) cumulative;
                return (T)result;
            } else if(clazz.isAssignableFrom(Long.class)) {
                Long result = (Long)input + (Long) cumulative;
                return (T)result;
            } else if(clazz.isAssignableFrom(Double.class)){
                Double result = (Double)input + (Double) cumulative;
                return (T)result;
            } if(clazz.isAssignableFrom(Float.class)) {
                Float result = (Float)input + (Float) cumulative;
                return (T)result;
            } else if(clazz.isAssignableFrom(BigDecimal.class)) {
                BigDecimal result = ((BigDecimal)input).add((BigDecimal) cumulative);
                return (T)result;
            } else {
                throw new IllegalArgumentException("unsupported type " + clazz.getName());
            }
        }
    }

    public static  Reducer sum(Class clazz) {
        return new AddingReducer<>();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy