DATACMNS-474 - Allow conversion of Distance into other Metrics.
Added support for distance conversion via Distance.in(Metric).
This commit is contained in:
committed by
Oliver Gierke
parent
176be042c9
commit
b882d33d7b
@@ -30,7 +30,7 @@ public class Distance {
|
||||
private final Metric metric;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Distance}.
|
||||
* Creates a new {@link Distance} with a neutral metric. This means the provided value needs to be in normalized form.
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
@@ -45,6 +45,7 @@ public class Distance {
|
||||
* @param metric can be {@literal null}.
|
||||
*/
|
||||
public Distance(double value, Metric metric) {
|
||||
|
||||
this.value = value;
|
||||
this.metric = metric == null ? Metrics.NEUTRAL : metric;
|
||||
}
|
||||
@@ -110,6 +111,19 @@ public class Distance {
|
||||
return new Distance(newLeft + newRight, metric);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Distance} in the given {@link Metric}. This means that the returned instance will have the
|
||||
* same normalized value as the original instance.
|
||||
*
|
||||
* @param metric must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public Distance in(Metric metric) {
|
||||
|
||||
Assert.notNull(metric, "Metric must not be null!");
|
||||
return this.metric.equals(metric) ? this : new Distance(getNormalizedValue() * metric.getMultiplier(), metric);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
|
||||
Reference in New Issue
Block a user