eu.agrosense.client.grid.util.AggregationStrategies Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2008-2012 AgroSense Foundation.
*
* AgroSense is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
* this software, see the FLOSS License Exception
* .
*
* AgroSense is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgroSense. If not, see .
*
* Contributors:
* johan - initial API and implementation and/or initial documentation
*/
package eu.agrosense.client.grid.util;
import eu.agrosense.client.grid.util.AggregationStrategy.Factory;
/**
* Basic aggregation strategy implementations.
*
* @author johan
*/
public class AggregationStrategies {
// absolute amounts:
protected static final class Sum implements AggregationStrategy {
private double sum;
private Sum(double initialValue) {
sum = initialValue;
}
@Override
public void add(double value) {
sum += value;
}
@Override
public double getAggregate() {
return sum;
}
}
public static Factory sum() {
return new Factory() {
@Override
public AggregationStrategy create(double value) { return new Sum(value); }
};
}
// e.g. setpoint rates:
protected static final class Average implements AggregationStrategy {
private double sum;
private int count;
private Average(double initialValue) {
sum = initialValue;
count = 1;
}
@Override
public void add(double value) {
sum += value;
count++;
}
@Override
public double getAggregate() {
return sum / count;
}
}
public static Factory average() {
return new Factory() {
@Override
public AggregationStrategy create(double value) { return new Average(value); }
};
}
protected static final class Max implements AggregationStrategy {
private double max;
private Max(double value) {
this.max = value;
}
@Override
public void add(double value) {
if (value > max) { max = value; }
}
@Override
public double getAggregate() {
return max;
}
}
public static Factory max() {
return new Factory() {
@Override
public AggregationStrategy create(double value) { return new Max(value); }
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy