Browse Source
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.pull/117/head
5 changed files with 316 additions and 21 deletions
@ -0,0 +1,96 @@
@@ -0,0 +1,96 @@
|
||||
/* |
||||
* Copyright 2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.domain; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Simple value object to work with ranges. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @since 1.10 |
||||
*/ |
||||
public class Range<T extends Comparable<T>> { |
||||
|
||||
private final T lowerBound; |
||||
private final T upperBound; |
||||
private final boolean lowerInclusive; |
||||
private final boolean upperInclusive; |
||||
|
||||
/** |
||||
* Creates a new {@link Range} with the given lower and upper bound. Treats the given values as inclusive bounds. Use |
||||
* {@link #Range(Comparable, Comparable, boolean, boolean)} to configure different bound behavior. |
||||
* |
||||
* @see #Range(Comparable, Comparable, boolean, boolean) |
||||
* @param lowerBound can be {@literal null} in case upperBound is not {@literal null}. |
||||
* @param upperBound can be {@literal null} in case lowerBound is not {@literal null}. |
||||
*/ |
||||
public Range(T lowerBound, T upperBound) { |
||||
this(lowerBound, upperBound, true, true); |
||||
} |
||||
|
||||
/** |
||||
* Createsa new {@link Range} with the given lower and upper bound as well as the given inclusive/exclusive semantics. |
||||
* |
||||
* @param lowerBound can be {@literal null}. |
||||
* @param upperBound can be {@literal null}. |
||||
* @param lowerInclusive |
||||
* @param upperInclusive |
||||
*/ |
||||
public Range(T lowerBound, T upperBound, boolean lowerInclusive, boolean upperInclusive) { |
||||
|
||||
this.lowerBound = lowerBound; |
||||
this.upperBound = upperBound; |
||||
this.lowerInclusive = lowerInclusive; |
||||
this.upperInclusive = upperInclusive; |
||||
} |
||||
|
||||
/** |
||||
* Returns the lower bound of the range. |
||||
* |
||||
* @return can be {@literal null}. |
||||
*/ |
||||
public T getLowerBound() { |
||||
return lowerBound; |
||||
} |
||||
|
||||
/** |
||||
* Returns the upper bound of the range. |
||||
* |
||||
* @return can be {@literal null}. |
||||
*/ |
||||
public T getUpperBound() { |
||||
return upperBound; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether the {@link Range} contains the given value. |
||||
* |
||||
* @param value must not be {@literal null}. |
||||
* @return |
||||
*/ |
||||
public boolean contains(T value) { |
||||
|
||||
Assert.notNull(value, "Reference value must not be null!"); |
||||
|
||||
boolean greaterThanLowerBound = lowerBound == null ? true : lowerInclusive ? lowerBound.compareTo(value) <= 0 |
||||
: lowerBound.compareTo(value) < 0; |
||||
boolean lessThanUpperBound = upperBound == null ? true : upperInclusive ? upperBound.compareTo(value) >= 0 |
||||
: upperBound.compareTo(value) > 0; |
||||
|
||||
return greaterThanLowerBound && lessThanUpperBound; |
||||
} |
||||
} |
||||
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
/* |
||||
* Copyright 2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.domain; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* Unit tests for {@link Range}. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @since 1.10 |
||||
*/ |
||||
public class RangeUnitTests { |
||||
|
||||
/** |
||||
* @see DATACMNS-651 |
||||
*/ |
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void rejectsNullReferenceValuesForContains() { |
||||
new Range<Long>(10L, 20L).contains(null); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-651 |
||||
*/ |
||||
@Test |
||||
public void usesBoundsInclusivelyByDefault() { |
||||
|
||||
Range<Long> range = new Range<Long>(10L, 20L); |
||||
|
||||
assertThat(range.contains(10L), is(true)); |
||||
assertThat(range.contains(20L), is(true)); |
||||
assertThat(range.contains(15L), is(true)); |
||||
assertThat(range.contains(5L), is(false)); |
||||
assertThat(range.contains(25L), is(false)); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-651 |
||||
*/ |
||||
@Test |
||||
public void excludesLowerBoundIfConfigured() { |
||||
|
||||
Range<Long> range = new Range<Long>(10L, 20L, false, true); |
||||
|
||||
assertThat(range.contains(10L), is(false)); |
||||
assertThat(range.contains(20L), is(true)); |
||||
assertThat(range.contains(15L), is(true)); |
||||
assertThat(range.contains(5L), is(false)); |
||||
assertThat(range.contains(25L), is(false)); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-651 |
||||
*/ |
||||
@Test |
||||
public void excludesUpperBoundIfConfigured() { |
||||
|
||||
Range<Long> range = new Range<Long>(10L, 20L, true, false); |
||||
|
||||
assertThat(range.contains(10L), is(true)); |
||||
assertThat(range.contains(20L), is(false)); |
||||
assertThat(range.contains(15L), is(true)); |
||||
assertThat(range.contains(5L), is(false)); |
||||
assertThat(range.contains(25L), is(false)); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-651 |
||||
*/ |
||||
@Test |
||||
public void handlesOpenUpperBoundCorrectly() { |
||||
|
||||
Range<Long> range = new Range<Long>(10L, null); |
||||
|
||||
assertThat(range.contains(10L), is(true)); |
||||
assertThat(range.contains(20L), is(true)); |
||||
assertThat(range.contains(15L), is(true)); |
||||
assertThat(range.contains(5L), is(false)); |
||||
assertThat(range.contains(25L), is(true)); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-651 |
||||
*/ |
||||
@Test |
||||
public void handlesOpenLowerBoundCorrectly() { |
||||
|
||||
Range<Long> range = new Range<Long>(null, 20L); |
||||
|
||||
assertThat(range.contains(10L), is(true)); |
||||
assertThat(range.contains(20L), is(true)); |
||||
assertThat(range.contains(15L), is(true)); |
||||
assertThat(range.contains(5L), is(true)); |
||||
assertThat(range.contains(25L), is(false)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue