Author: mbenson
Date: Sun Jul 17 06:10:37 2011
New Revision: 1147537
URL: http://svn.apache.org/viewvc?rev=1147537&view=rev
Log:
[LANG-726] Add a method e.g. Range<T> Range<T>.intersectionWith(Range<T>)
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Range.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RangeTest.java
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Range.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Range.java?rev=1147537&r1=1147536&r2=1147537&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Range.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Range.java
Sun Jul 17 06:10:37 2011
@@ -371,6 +371,26 @@ public final class Range<T> implements S
return isBefore(otherRange.minimum);
}
+ /**
+ * Calculate the intersection of {@code this} and an overlapping Range.
+ * @param other overlapping Range
+ * @return range representing the intersection of {@code this} and {@code
other} ({@code this} if equal)
+ * @throws IllegalArgumentException if {@code other} does not overlap
{@code this}
+ * @since 3.0.1
+ */
+ public Range<T> intersectionWith(Range<T> other) {
+ if (!this.isOverlappedBy(other)) {
+ throw new IllegalArgumentException(String.format(
+ "Cannot calculate intersection with non-overlapping range %s",
other));
+ }
+ if (this.equals(other)) {
+ return this;
+ }
+ T min = getComparator().compare(minimum, other.minimum) < 0 ?
other.minimum : minimum;
+ T max = getComparator().compare(maximum, other.maximum) < 0 ? maximum
: other.maximum;
+ return between(min, max, getComparator());
+ }
+
// Basics
//--------------------------------------------------------------------
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RangeTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RangeTest.java?rev=1147537&r1=1147536&r2=1147537&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RangeTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/RangeTest.java
Sun Jul 17 06:10:37 2011
@@ -342,6 +342,27 @@ public class RangeTest {
assertFalse(intRange.isBeforeRange(Range.between(10, 20)));
}
+ @Test
+ public void testIntersectionWith() {
+ assertSame(intRange, intRange.intersectionWith(intRange));
+ assertSame(byteRange, byteRange.intersectionWith(byteRange));
+ assertSame(longRange, longRange.intersectionWith(longRange));
+ assertSame(floatRange, floatRange.intersectionWith(floatRange));
+ assertSame(doubleRange, doubleRange.intersectionWith(doubleRange));
+
+ assertEquals(Range.between(10, 15),
intRange.intersectionWith(Range.between(5, 15)));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIntersectionWithNull() {
+ intRange.intersectionWith(null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIntersectionWithNonOverlapping() {
+ intRange.intersectionWith(Range.between(0, 9));
+ }
+
//-----------------------------------------------------------------------
@Test
public void testSerializing() {