All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.paganini2008.devtools.beans.streaming.Functions Maven / Gradle / Ivy
package com.github.paganini2008.devtools.beans.streaming;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import com.github.paganini2008.devtools.Comparables;
import com.github.paganini2008.devtools.beans.BeanUtils;
/**
*
* Functions
*
* @author Fred Feng
*
* @version 1.0
*/
public abstract class Functions {
public static > Calculation min(String attributeName, Class requiredType) {
return new Min(attributeName, requiredType);
}
public static > Calculation max(String attributeName, Class requiredType) {
return new Max(attributeName, requiredType);
}
public static Calculation sum(String attributeName) {
return new Sum(attributeName);
}
public static Calculation avg(String attributeName, int scale, RoundingMode roundingMode) {
return new Avg(attributeName, scale, roundingMode);
}
public static class Min> implements Calculation {
private final String attributeName;
private final Class requiredType;
Min(String attributeName, Class requiredType) {
this.attributeName = attributeName;
this.requiredType = requiredType;
}
public T getResult(List elements) {
T identity = BeanUtils.getProperty(elements.get(0), attributeName, requiredType);
return elements.stream().map(e -> {
return BeanUtils.getProperty(e, attributeName, requiredType);
}).reduce(identity, (left, right) -> {
return Comparables.min(left, right);
});
}
}
public static class Max> implements Calculation {
private final String attributeName;
private final Class requiredType;
Max(String attributeName, Class requiredType) {
this.attributeName = attributeName;
this.requiredType = requiredType;
}
public T getResult(List elements) {
return elements.stream().map(e -> {
return BeanUtils.getProperty(e, attributeName, requiredType);
}).reduce(null, (left, right) -> {
return Comparables.max(left, right);
});
}
}
public static class Sum implements Calculation {
private final String attributeName;
Sum(String attributeName) {
this.attributeName = attributeName;
}
public BigDecimal getResult(List elements) {
return elements.stream().map(e -> {
return BeanUtils.getProperty(e, attributeName, BigDecimal.class);
}).reduce(BigDecimal.ZERO, (left, right) -> {
return left.add(right);
});
}
}
public static class Avg implements Calculation {
private final String attributeName;
private final int scale;
private final RoundingMode roundingMode;
Avg(String attributeName, int scale, RoundingMode roundingMode) {
this.attributeName = attributeName;
this.scale = scale;
this.roundingMode = roundingMode;
}
public BigDecimal getResult(List elements) {
return elements.stream().map(e -> {
return BeanUtils.getProperty(e, attributeName, BigDecimal.class);
}).reduce(BigDecimal.ZERO, (left, right) -> {
return left.add(right);
}).divide(BigDecimal.valueOf(elements.size()), scale, roundingMode);
}
}
}