Commit 5118f3aa authored by Sören Henning's avatar Sören Henning

get aggregator by using reflection

parent a14e8213
package teead.aggregation;
import java.util.HashMap;
public class AggregatorConfiguration extends HashMap<String, String> {
private static final int INITIAL_MAP_CAPACITY = 1; // Most times the configuration is empty
public AggregatorConfiguration() {
super(INITIAL_MAP_CAPACITY);
}
private static final long serialVersionUID = 1L;
}
package teead.aggregation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import com.google.common.base.Throwables;
/**
* @author Sören Henning
*
*/
public final class Aggregators {
private static final String AGGREGATORS_PATH = "teead.aggregation";
private Aggregators() {}
public static Aggregator getByClassName(final String name, final AggregatorConfiguration configuration) {
if (name.indexOf('.') < 0) {
// It seems that just the class name were passed
try {
return getByFullClassName(AGGREGATORS_PATH + '.' + name, configuration);
} catch (IllegalArgumentException exception) {
return getByFullClassName(name, configuration);
}
} else {
// It seems that a fully qualified name were passed
try {
return getByFullClassName(name, configuration);
} catch (IllegalArgumentException exception) {
return getByFullClassName(AGGREGATORS_PATH + '.' + name, configuration);
}
}
}
public static Aggregator getByFullClassName(final String name, final AggregatorConfiguration configuration) {
try {
// get aggregator class by using reflection
Class<?> aggregatorClass = Class.forName(name);
Class<?>[] constructorParameterClasses = new Class[] { AggregatorConfiguration.class };
Object[] constructorParameterObjects = new Object[] { configuration };
Constructor<?> constructor = aggregatorClass.getConstructor(constructorParameterClasses);
return (Aggregator) constructor.newInstance(constructorParameterObjects);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("The Aggregator \""
+ name
+ "\" could not be found.", e);
} catch (InstantiationException e) {
throw new IllegalArgumentException("The Aggregator \""
+ name
+ "\" is declared as abstract and cannot be instantiated", e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("The constructor of \""
+ name
+ "\" could not be accessed.", e);
} catch (IllegalArgumentException e) {
// should not happen at all
throw new IllegalArgumentException("The constructor of \""
+ name
+ "\" has not been called with the correct amount of arguments.", e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException("The constructor of \""
+ name
+ "\" has thrown an exception:\n"
+ Throwables.getStackTraceAsString(e), e);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("The Aggregator \""
+ name
+ "\" does not have any constructor with exactly one AggregatorConfiguration as its parameter.", e);
} catch (SecurityException e) {
throw new IllegalArgumentException("A Security Manager is present and \""
+ name
+ "\"does not have the correct class loader.", e);
}
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class GeometricMeanAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.geometricMean(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class GeometricMeanAggregator implements Aggregator {
public GeometricMeanAggregator() {}
public GeometricMeanAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.geometricMean(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class MaxAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.max(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class MaxAggregator implements Aggregator {
public MaxAggregator() {}
public MaxAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.max(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class MeanAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.mean(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class MeanAggregator implements Aggregator {
public MeanAggregator() {}
public MeanAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.mean(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class MinAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.min(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class MinAggregator implements Aggregator {
public MinAggregator() {}
public MinAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.min(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class PercentileAggregator implements Aggregator {
private double percentile;
public PercentileAggregator(final double percentile) {
this.setPercentile(percentile);
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.percentile(timeSeries.toValuesArray(), this.getPercentile());
}
public double getPercentile() {
return percentile;
}
public void setPercentile(final double percentile) {
this.percentile = percentile;
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class PercentileAggregator implements Aggregator {
public final static String PERCENTILE_KEY = "percentile";
private double percentile;
public PercentileAggregator(final double percentile) {
this.setPercentile(percentile);
}
public PercentileAggregator(final AggregatorConfiguration configuration) {
this(Double.valueOf(configuration.get(PERCENTILE_KEY)));
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.percentile(timeSeries.toValuesArray(), this.getPercentile());
}
public double getPercentile() {
return percentile;
}
public void setPercentile(final double percentile) {
this.percentile = percentile;
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class ProductAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.product(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class ProductAggregator implements Aggregator {
public ProductAggregator() {}
public ProductAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.product(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class SumAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.sum(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class SumAggregator implements Aggregator {
public SumAggregator() {}
public SumAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.sum(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class SumLogAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.sumLog(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class SumLogAggregator implements Aggregator {
public SumLogAggregator() {}
public SumLogAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.sumLog(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class SumSqAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.sumSq(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class SumSqAggregator implements Aggregator {
public SumSqAggregator() {}
public SumSqAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.sumSq(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class VarianceAggregator implements Aggregator {
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.variance(timeSeries.toValuesArray());
}
}
package teead.aggregation;
import org.apache.commons.math3.stat.StatUtils;
import teead.timeseries.TimeSeries;
public class VarianceAggregator implements Aggregator {
public VarianceAggregator() {}
public VarianceAggregator(final AggregatorConfiguration configuration) {
this();
}
@Override
public double aggregate(final TimeSeries timeSeries) {
return StatUtils.variance(timeSeries.toValuesArray());
}
}
package teead;
import static org.junit.Assert.assertTrue;
import java.time.Duration;
import java.time.Instant;
import org.junit.Test;
import teead.measurement.Measurement;
import teead.timeseries.BoundedTimeSeries;
import teead.timeseries.TimeSeries;
import teead.timeseries.TimeSeriesPoint;
public class TimeSeriesExtractorTest {
@Test
public void testExtractFromEmptyTimeSeries() {
BoundedTimeSeries timeSeries = new BoundedTimeSeries(Duration.ofMinutes(1));
TimeSeriesExtractor timeSeriesExtractor = new TimeSeriesExtractor(timeSeries);
Measurement measurement = new Measurement(Instant.parse("2016-08-26T10:00:00.00Z"), 42.24);
TimeSeries extractedTimeSeries = timeSeriesExtractor.extract(measurement);
assertTrue(extractedTimeSeries.size() == 0);
}
@Test
public void testExtractFromThreeElementsTimeSeries() {
BoundedTimeSeries timeSeries = new BoundedTimeSeries(Duration.ofMinutes(1));
timeSeries.appendEnd(new TimeSeriesPoint(Instant.parse("2016-08-26T10:00:00.00Z"), 5));
timeSeries.appendEnd(new TimeSeriesPoint(Instant.parse("2016-08-26T10:00:05.00Z"), 10));
timeSeries.appendEnd(new TimeSeriesPoint(Instant.parse("2016-08-26T10:00:07.00Z"), 15));
TimeSeriesExtractor timeSeriesExtractor = new TimeSeriesExtractor(timeSeries);
Measurement measurement = new Measurement(Instant.parse("2016-08-26T10:00:10.00Z"), 20);
TimeSeries extractedTimeSeries = timeSeriesExtractor.extract(measurement);
assertTrue(extractedTimeSeries.size() == 3);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment