DATACMNS-651 - Introduced a Range value type.

Primarily intended to be used with Distance instances, we introduce a Range value type. Distance now has factory methods to create Range instances between two distances. To support this, Distance now implements comparable based on the normalized value of it.

Tiny refactoring in TypeDiscoverer to avoid code duplication between the lookup of parameter type information for constructors and methods.
This commit is contained in:
Oliver Gierke
2015-03-04 17:34:32 +01:00
parent 186cb25605
commit 2d75cf2c43
5 changed files with 316 additions and 21 deletions

View File

@@ -15,12 +15,12 @@
*/
package org.springframework.data.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.number.IsCloseTo.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.geo.Metrics.*;
import org.junit.Test;
import org.springframework.data.domain.Range;
import org.springframework.util.SerializationUtils;
/**
@@ -145,4 +145,54 @@ public class DistanceUnitTests {
public void returnsMetricsAbbreviationAsUnit() {
assertThat(new Distance(10, KILOMETERS).getUnit(), is("km"));
}
/**
* @see DATACMNS-651
*/
@Test
public void createsARangeCorrectly() {
Distance twoKilometers = new Distance(2, KILOMETERS);
Distance tenKilometers = new Distance(10, KILOMETERS);
Range<Distance> range = Distance.between(twoKilometers, tenKilometers);
assertThat(range, is(notNullValue()));
assertThat(range.getLowerBound(), is(twoKilometers));
assertThat(range.getUpperBound(), is(tenKilometers));
}
/**
* @see DATACMNS-651
*/
@Test
public void createsARangeFromPiecesCorrectly() {
Distance twoKilometers = new Distance(2, KILOMETERS);
Distance tenKilometers = new Distance(10, KILOMETERS);
Range<Distance> range = Distance.between(2, KILOMETERS, 10, KILOMETERS);
assertThat(range, is(notNullValue()));
assertThat(range.getLowerBound(), is(twoKilometers));
assertThat(range.getUpperBound(), is(tenKilometers));
}
/**
* @see DATACMNS-651
*/
@Test
public void implementsComparableCorrectly() {
Distance twoKilometers = new Distance(2, KILOMETERS);
Distance tenKilometers = new Distance(10, KILOMETERS);
Distance tenKilometersInMiles = new Distance(6.21371256214785, MILES);
assertThat(tenKilometers.compareTo(tenKilometers), is(0));
assertThat(tenKilometers.compareTo(tenKilometersInMiles), is(0));
assertThat(tenKilometersInMiles.compareTo(tenKilometers), is(0));
assertThat(twoKilometers.compareTo(tenKilometers), is(lessThan(0)));
assertThat(tenKilometers.compareTo(twoKilometers), is(greaterThan(0)));
}
}