public final class WeightsPolling<T> {
private final List<WeightsInfo<T>> weightsInfos;
private Integer totalWeights = 0;
private Integer maxWeights;
private WeightsInfo<T> maxWeightsInfo;
public WeightsPolling(List<WeightsInfo<T>> weightsInfos) {
this.weightsInfos = weightsInfos;
init();
}
private void init() {
weightsInfos.forEach(info -> totalWeights += info.getOriginalWeights());
}
public WeightsInfo<T> polling() {
weightsInfos.forEach(info -> {
maxWeightInfo(info);
info.setCurrentWeights(info.getCurrentWeights() + info.getOriginalWeights());
});
if (maxWeightsInfo != null) {
maxWeightsInfo.setCurrentWeights(maxWeightsInfo.getCurrentWeights() - totalWeights);
}
maxWeights = null;
return maxWeightsInfo;
}
private void maxWeightInfo(WeightsInfo<T> weightsInfo) {
if (maxWeights == null || weightsInfo.getCurrentWeights() > maxWeights) {
maxWeights = weightsInfo.getCurrentWeights();
maxWeightsInfo = weightsInfo;
}
}
}
@Data
public class WeightsInfo<T> {
/**
* 原始权重
*/
private int originalWeights;
/**
* 当前权重
*/
private int currentWeights;
private T data;
public WeightsInfo(T data, int originalWeights) {
this.originalWeights = originalWeights;
this.currentWeights = originalWeights;
this.data = data;
}
}