![JAR search and dependency download from the Maven repository](/logo.png)
com.jillesvangurp.iterables.Reducers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iterables-support Show documentation
Show all versions of iterables-support Show documentation
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 extends Number> 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