mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-04 11:23:50 +00:00
introduced factory pattern for ConfidenceBasedAverager
This commit is contained in:
1 parent
f72e32e935
commit
aee84f85c1
8 files changed
+111
-58
No files matched your search
+15
-54
@@ -9,58 +9,13 @@ import com.sap.sailing.domain.base.Bearing;
|
|||||||
import com.sap.sailing.domain.base.impl.DegreeBearingImpl;
|
import com.sap.sailing.domain.base.impl.DegreeBearingImpl;
|
||||||
import com.sap.sailing.domain.base.impl.RadianBearingImpl;
|
import com.sap.sailing.domain.base.impl.RadianBearingImpl;
|
||||||
import com.sap.sailing.domain.common.Util.Pair;
|
import com.sap.sailing.domain.common.Util.Pair;
|
||||||
|
import com.sap.sailing.domain.confidence.ConfidenceBasedAverager;
|
||||||
|
import com.sap.sailing.domain.confidence.ConfidenceBasedAveragerFactory;
|
||||||
import com.sap.sailing.domain.confidence.HasConfidence;
|
import com.sap.sailing.domain.confidence.HasConfidence;
|
||||||
import com.sap.sailing.domain.confidence.ScalableValue;
|
import com.sap.sailing.domain.confidence.ScalableValue;
|
||||||
import com.sap.sailing.domain.confidence.impl.ConfidenceBasedAveragerImpl;
|
import com.sap.sailing.domain.confidence.impl.ScalableDoubleWithConfidence;
|
||||||
|
|
||||||
public class ConfidenceTest {
|
public class ConfidenceTest {
|
||||||
private static class ScalableDouble implements ScalableValue<Double, Double> {
|
|
||||||
private final Double d;
|
|
||||||
|
|
||||||
public ScalableDouble(Double d) {
|
|
||||||
this.d = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ScalableValue<Double, Double> multiply(double factor) {
|
|
||||||
return new ScalableDouble(d * factor);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ScalableValue<Double, Double> add(ScalableValue<Double, Double> t) {
|
|
||||||
return new ScalableDouble(d+t.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Double divide(double divisor) {
|
|
||||||
return d / divisor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Double getValue() {
|
|
||||||
return d;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ScalableDoubleWithConfidence extends ScalableDouble implements HasConfidence<Double, Double> {
|
|
||||||
private final double confidence;
|
|
||||||
|
|
||||||
public ScalableDoubleWithConfidence(double d, double confidence) {
|
|
||||||
super(d);
|
|
||||||
this.confidence = confidence;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getConfidence() {
|
|
||||||
return confidence;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ScalableValue<Double, Double> getScalableValue() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ScalableBearing implements ScalableValue<Pair<Double, Double>, Bearing> {
|
private static class ScalableBearing implements ScalableValue<Pair<Double, Double>, Bearing> {
|
||||||
private final double sin;
|
private final double sin;
|
||||||
private final double cos;
|
private final double cos;
|
||||||
@@ -128,14 +83,16 @@ public class ConfidenceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAveragingWithEmptyListYieldsNull() {
|
public void testAveragingWithEmptyListYieldsNull() {
|
||||||
|
ConfidenceBasedAverager<Double, Double> averager = ConfidenceBasedAveragerFactory.INSTANCE.createAverager();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Double average = new ConfidenceBasedAveragerImpl<Double, Double>().getAverage();
|
Double average = averager.getAverage();
|
||||||
assertNull(average);
|
assertNull(average);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAveragingWithNullArrayYieldsNull() {
|
public void testAveragingWithNullArrayYieldsNull() {
|
||||||
Double average = new ConfidenceBasedAveragerImpl<Double, Double>().getAverage((HasConfidence<Double, Double>[]) null);
|
ConfidenceBasedAverager<Double, Double> averager = ConfidenceBasedAveragerFactory.INSTANCE.createAverager();
|
||||||
|
Double average = averager.getAverage((HasConfidence<Double, Double>[]) null);
|
||||||
assertNull(average);
|
assertNull(average);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,8 +100,9 @@ public class ConfidenceTest {
|
|||||||
public void testAveragingWithTwoDoubles() {
|
public void testAveragingWithTwoDoubles() {
|
||||||
ScalableDoubleWithConfidence d1 = new ScalableDoubleWithConfidence(1., 0.5);
|
ScalableDoubleWithConfidence d1 = new ScalableDoubleWithConfidence(1., 0.5);
|
||||||
ScalableDoubleWithConfidence d2 = new ScalableDoubleWithConfidence(2., 0.5);
|
ScalableDoubleWithConfidence d2 = new ScalableDoubleWithConfidence(2., 0.5);
|
||||||
|
ConfidenceBasedAverager<Double, Double> averager = ConfidenceBasedAveragerFactory.INSTANCE.createAverager();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Double average = new ConfidenceBasedAveragerImpl<Double, Double>().getAverage(d1, d2);
|
Double average = averager.getAverage(d1, d2);
|
||||||
assertEquals(1.5, average, 0.00000001);
|
assertEquals(1.5, average, 0.00000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,8 +111,9 @@ public class ConfidenceTest {
|
|||||||
ScalableDoubleWithConfidence d1 = new ScalableDoubleWithConfidence(1., 1.);
|
ScalableDoubleWithConfidence d1 = new ScalableDoubleWithConfidence(1., 1.);
|
||||||
ScalableDoubleWithConfidence d2 = new ScalableDoubleWithConfidence(2., 1.);
|
ScalableDoubleWithConfidence d2 = new ScalableDoubleWithConfidence(2., 1.);
|
||||||
ScalableDoubleWithConfidence d3 = new ScalableDoubleWithConfidence(3., 2.);
|
ScalableDoubleWithConfidence d3 = new ScalableDoubleWithConfidence(3., 2.);
|
||||||
|
ConfidenceBasedAverager<Double, Double> averager = ConfidenceBasedAveragerFactory.INSTANCE.createAverager();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Double average = new ConfidenceBasedAveragerImpl<Double, Double>().getAverage(d1, d2, d3);
|
Double average = averager.getAverage(d1, d2, d3);
|
||||||
assertEquals(2.25, average, 0.00000001);
|
assertEquals(2.25, average, 0.00000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,8 +121,9 @@ public class ConfidenceTest {
|
|||||||
public void testAveragingWithTwoBearings() {
|
public void testAveragingWithTwoBearings() {
|
||||||
ScalableBearingWithConfidence d1 = new ScalableBearingWithConfidence(new DegreeBearingImpl(350.), 1.);
|
ScalableBearingWithConfidence d1 = new ScalableBearingWithConfidence(new DegreeBearingImpl(350.), 1.);
|
||||||
ScalableBearingWithConfidence d2 = new ScalableBearingWithConfidence(new DegreeBearingImpl(10.), 1.);
|
ScalableBearingWithConfidence d2 = new ScalableBearingWithConfidence(new DegreeBearingImpl(10.), 1.);
|
||||||
|
ConfidenceBasedAverager<Pair<Double, Double>, Bearing> averager = ConfidenceBasedAveragerFactory.INSTANCE.createAverager();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Bearing average = new ConfidenceBasedAveragerImpl<Pair<Double, Double>, Bearing>().getAverage(d1, d2);
|
Bearing average = averager.getAverage(d1, d2);
|
||||||
assertEquals(0, average.getDegrees(), 0.00000001);
|
assertEquals(0, average.getDegrees(), 0.00000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,8 +132,9 @@ public class ConfidenceTest {
|
|||||||
ScalableBearingWithConfidence d1 = new ScalableBearingWithConfidence(new DegreeBearingImpl(350.), 1.);
|
ScalableBearingWithConfidence d1 = new ScalableBearingWithConfidence(new DegreeBearingImpl(350.), 1.);
|
||||||
ScalableBearingWithConfidence d2 = new ScalableBearingWithConfidence(new DegreeBearingImpl(10.), 1.);
|
ScalableBearingWithConfidence d2 = new ScalableBearingWithConfidence(new DegreeBearingImpl(10.), 1.);
|
||||||
ScalableBearingWithConfidence d3 = new ScalableBearingWithConfidence(new DegreeBearingImpl(20.), 2.);
|
ScalableBearingWithConfidence d3 = new ScalableBearingWithConfidence(new DegreeBearingImpl(20.), 2.);
|
||||||
|
ConfidenceBasedAverager<Pair<Double, Double>, Bearing> averager = ConfidenceBasedAveragerFactory.INSTANCE.createAverager();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Bearing average = new ConfidenceBasedAveragerImpl<Pair<Double, Double>, Bearing>().getAverage(d1, d2, d3);
|
Bearing average = averager.getAverage(d1, d2, d3);
|
||||||
assertEquals(10, average.getDegrees(), 0.1);
|
assertEquals(10, average.getDegrees(), 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package com.sap.sailing.domain.confidence;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link ScalableValue} where the <code>ValueType</code> and the <code>AveragesTo</code> type are both the same because
|
||||||
|
* the value type is itself scalable, offering addition, multiplication and division.
|
||||||
|
*
|
||||||
|
* @author Axel Uhl (d043530)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface AbstractScalarValue<T> extends ScalableValue<T, T> {
|
||||||
|
}
|
||||||
Executable
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package com.sap.sailing.domain.confidence;
|
||||||
|
|
||||||
|
public interface ConfidenceBasedAverager<ValueType, AveragesTo> {
|
||||||
|
AveragesTo getAverage(HasConfidence<ValueType, AveragesTo>... values);
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package com.sap.sailing.domain.confidence;
|
||||||
|
|
||||||
|
import com.sap.sailing.domain.confidence.impl.ConfidenceBasedAveragerFactoryImpl;
|
||||||
|
|
||||||
|
public interface ConfidenceBasedAveragerFactory {
|
||||||
|
ConfidenceBasedAveragerFactory INSTANCE = new ConfidenceBasedAveragerFactoryImpl();
|
||||||
|
|
||||||
|
<ValueType, AveragesTo> ConfidenceBasedAverager<ValueType, AveragesTo> createAverager();
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package com.sap.sailing.domain.confidence.impl;
|
||||||
|
|
||||||
|
import com.sap.sailing.domain.confidence.ConfidenceBasedAverager;
|
||||||
|
import com.sap.sailing.domain.confidence.ConfidenceBasedAveragerFactory;
|
||||||
|
|
||||||
|
public class ConfidenceBasedAveragerFactoryImpl implements ConfidenceBasedAveragerFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <ValueType, AveragesTo> ConfidenceBasedAverager<ValueType, AveragesTo> createAverager() {
|
||||||
|
return new ConfidenceBasedAveragerImpl<ValueType, AveragesTo>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+3
-4
@@ -1,5 +1,6 @@
|
|||||||
package com.sap.sailing.domain.confidence.impl;
|
package com.sap.sailing.domain.confidence.impl;
|
||||||
|
|
||||||
|
import com.sap.sailing.domain.confidence.ConfidenceBasedAverager;
|
||||||
import com.sap.sailing.domain.confidence.HasConfidence;
|
import com.sap.sailing.domain.confidence.HasConfidence;
|
||||||
import com.sap.sailing.domain.confidence.ScalableValue;
|
import com.sap.sailing.domain.confidence.ScalableValue;
|
||||||
|
|
||||||
@@ -25,11 +26,9 @@ import com.sap.sailing.domain.confidence.ScalableValue;
|
|||||||
* devices produce some statistical errors, no matter how small (cf. Heisenberg ;-) ).
|
* devices produce some statistical errors, no matter how small (cf. Heisenberg ;-) ).
|
||||||
*
|
*
|
||||||
* @author Axel Uhl (d043530)
|
* @author Axel Uhl (d043530)
|
||||||
*
|
|
||||||
* @param <T>
|
|
||||||
* the type of value to be scaled / averaged using their confidence
|
|
||||||
*/
|
*/
|
||||||
public class ConfidenceBasedAveragerImpl<ValueType, AveragesTo> {
|
public class ConfidenceBasedAveragerImpl<ValueType, AveragesTo> implements ConfidenceBasedAverager<ValueType, AveragesTo> {
|
||||||
|
@Override
|
||||||
public AveragesTo getAverage(HasConfidence<ValueType, AveragesTo>... values) {
|
public AveragesTo getAverage(HasConfidence<ValueType, AveragesTo>... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Executable
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package com.sap.sailing.domain.confidence.impl;
|
||||||
|
|
||||||
|
import com.sap.sailing.domain.confidence.AbstractScalarValue;
|
||||||
|
import com.sap.sailing.domain.confidence.ScalableValue;
|
||||||
|
|
||||||
|
public class ScalableDouble implements AbstractScalarValue<Double> {
|
||||||
|
private final double value;
|
||||||
|
|
||||||
|
public ScalableDouble(double value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ScalableValue<Double, Double> multiply(double factor) {
|
||||||
|
return new ScalableDouble(factor*getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ScalableDouble add(ScalableValue<Double, Double> t) {
|
||||||
|
return new ScalableDouble(getValue()+t.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double divide(double divisor) {
|
||||||
|
return getValue()/divisor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package com.sap.sailing.domain.confidence.impl;
|
||||||
|
|
||||||
|
import com.sap.sailing.domain.confidence.HasConfidence;
|
||||||
|
|
||||||
|
public class ScalableDoubleWithConfidence extends ScalableDouble implements HasConfidence<Double, Double> {
|
||||||
|
private final double confidence;
|
||||||
|
|
||||||
|
public ScalableDoubleWithConfidence(double d, double confidence) {
|
||||||
|
super(d);
|
||||||
|
this.confidence = confidence;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getConfidence() {
|
||||||
|
return confidence;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ScalableDouble getScalableValue() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user