Added umbrellaInterval method, which takes an Iterable of intervals and returns

a single interval spanning the entire range of input intervals.
This commit is contained in:
Deep Ganguli 2013-01-23 18:59:51 -08:00
parent 9b6244ec15
commit fc07bc315e
2 changed files with 64 additions and 0 deletions

View File

@ -19,6 +19,7 @@
package com.metamx.druid.utils;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.metamx.common.guava.Comparators;
@ -63,6 +64,35 @@ public class JodaUtils
return retVal;
}
public static Interval umbrellaInterval(Iterable<Interval> intervals)
{
DateTime minStart = null;
DateTime maxEnd = null;
for (Interval interval : intervals) {
DateTime curStart = interval.getStart();
DateTime curEnd = interval.getEnd();
if (minStart == null || maxEnd == null) {
minStart = curStart;
maxEnd = curEnd;
}
if (curStart.isBefore(minStart)) {
minStart = curStart;
}
if (curEnd.isAfter(maxEnd)) {
maxEnd = curEnd;
}
}
if (minStart == null || maxEnd == null) {
throw new IllegalArgumentException("Empty list of intervals");
}
return new Interval(minStart, maxEnd);
}
public static DateTime minDateTime(DateTime... times)
{
if (times == null) {

View File

@ -31,6 +31,40 @@ import java.util.List;
*/
public class JodaUtilsTest
{
@Test
public void testUmbrellaIntervalsSimple() throws Exception
{
List<Interval> intervals = Arrays.asList(
new Interval("2011-03-03/2011-03-04"),
new Interval("2011-01-01/2011-01-02"),
new Interval("2011-02-01/2011-02-05"),
new Interval("2011-02-03/2011-02-08"),
new Interval("2011-01-01/2011-01-03"),
new Interval("2011-03-01/2011-03-02"),
new Interval("2011-03-05/2011-03-06"),
new Interval("2011-02-01/2011-02-02")
);
Assert.assertEquals(
new Interval("2011-01-01/2011-03-06"),
JodaUtils.umbrellaInterval(intervals)
);
}
@Test
public void testUmbrellaIntervalsNull() throws Exception
{
List<Interval> intervals = Arrays.asList();
Throwable thrown = null;
try {
Interval res = JodaUtils.umbrellaInterval(intervals);
}
catch (IllegalArgumentException e) {
thrown = e;
}
Assert.assertNotNull("Empty list of intervals", thrown);
}
@Test
public void testCondenseIntervalsSimple() throws Exception
{