From fc07bc315eacf26ad0d8dca0ae1cdf18b9bf5040 Mon Sep 17 00:00:00 2001 From: Deep Ganguli Date: Wed, 23 Jan 2013 18:59:51 -0800 Subject: [PATCH] Added umbrellaInterval method, which takes an Iterable of intervals and returns a single interval spanning the entire range of input intervals. --- .../com/metamx/druid/utils/JodaUtils.java | 30 ++++++++++++++++ .../com/metamx/druid/utils/JodaUtilsTest.java | 34 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/common/src/main/java/com/metamx/druid/utils/JodaUtils.java b/common/src/main/java/com/metamx/druid/utils/JodaUtils.java index 53b14618001..3932ef08153 100644 --- a/common/src/main/java/com/metamx/druid/utils/JodaUtils.java +++ b/common/src/main/java/com/metamx/druid/utils/JodaUtils.java @@ -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 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) { diff --git a/common/src/test/java/com/metamx/druid/utils/JodaUtilsTest.java b/common/src/test/java/com/metamx/druid/utils/JodaUtilsTest.java index 6abaf485b68..f0248295dfd 100644 --- a/common/src/test/java/com/metamx/druid/utils/JodaUtilsTest.java +++ b/common/src/test/java/com/metamx/druid/utils/JodaUtilsTest.java @@ -31,6 +31,40 @@ import java.util.List; */ public class JodaUtilsTest { + @Test + public void testUmbrellaIntervalsSimple() throws Exception + { + List 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 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 {