Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Intervals by end then start comparator #38

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/main/java/com/metamx/common/guava/Comparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public int compare(T t, T t1)
};
}

private static final Comparator<Interval> INTERVAL_COMPARATOR = new Comparator<Interval>()
private static final Comparator<Interval> INTERVAL_BY_START_THEN_END = new Comparator<Interval>()
{
private final DateTimeComparator dateTimeComp = DateTimeComparator.getInstance();

Expand All @@ -79,6 +79,21 @@ public int compare(Interval lhs, Interval rhs)
}
};

private static final Comparator<Interval> INTERVAL_BY_END_THEN_START = new Comparator<Interval>()
{
private final DateTimeComparator dateTimeComp = DateTimeComparator.getInstance();

@Override
public int compare(Interval lhs, Interval rhs)
{
int retVal = dateTimeComp.compare(lhs.getEnd(), rhs.getEnd());
if (retVal == 0) {
retVal = dateTimeComp.compare(lhs.getStart(), rhs.getStart());
}
return retVal;
}
};

@Deprecated
public static Comparator<Interval> intervals()
{
Expand All @@ -87,6 +102,12 @@ public static Comparator<Interval> intervals()

public static Comparator<Interval> intervalsByStartThenEnd()
{
return INTERVAL_COMPARATOR;
return INTERVAL_BY_START_THEN_END;
}

public static Comparator<Interval> intervalsByEndThenStart()
{
return INTERVAL_BY_END_THEN_START;
}

}
40 changes: 38 additions & 2 deletions src/test/java/com/metamx/common/guava/ComparatorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public void testInverse() throws Exception
}

@Test
public void testIntervals() throws Exception
public void testIntervalsByStartThenEnd() throws Exception
{
Comparator<Interval> comp = Comparators.intervals();
Comparator<Interval> comp = Comparators.intervalsByStartThenEnd();

Assert.assertEquals(0, comp.compare(new Interval("P1d/2011-04-02"), new Interval("2011-04-01/2011-04-02")));
Assert.assertEquals(-1, comp.compare(new Interval("2011-03-31/2011-04-02"), new Interval("2011-04-01/2011-04-02")));
Expand Down Expand Up @@ -76,4 +76,40 @@ public void testIntervals() throws Exception
intervals
);
}

@Test
public void testIntervalsByEndThenStart() throws Exception
{
Comparator<Interval> comp = Comparators.intervalsByEndThenStart();

Assert.assertEquals(0, comp.compare(new Interval("P1d/2011-04-02"), new Interval("2011-04-01/2011-04-02")));
Assert.assertEquals(-1, comp.compare(new Interval("2011-04-01/2011-04-03"), new Interval("2011-04-01/2011-04-04")));
Assert.assertEquals(1, comp.compare(new Interval("2011-04-01/2011-04-02"), new Interval("2011-04-01/2011-04-01")));
Assert.assertEquals(-1, comp.compare(new Interval("2011-04-01/2011-04-03"), new Interval("2011-04-02/2011-04-03")));
Assert.assertEquals(1, comp.compare(new Interval("2011-04-01/2011-04-03"), new Interval("2011-03-31/2011-04-03")));

Interval[] intervals = new Interval[]{
new Interval("2011-04-01T18/2011-04-02T13"),
new Interval("2011-04-01/2011-04-03"),
new Interval("2011-04-01/2011-04-04"),
new Interval("2011-04-02/2011-04-04"),
new Interval("2011-04-01/2011-04-02"),
new Interval("2011-04-02/2011-04-03"),
new Interval("2011-04-02/2011-04-03T06")
};
Arrays.sort(intervals, comp);

Assert.assertArrayEquals(
new Interval[]{
new Interval("2011-04-01/2011-04-02"),
new Interval("2011-04-01T18/2011-04-02T13"),
new Interval("2011-04-01/2011-04-03"),
new Interval("2011-04-02/2011-04-03"),
new Interval("2011-04-02/2011-04-03T06"),
new Interval("2011-04-01/2011-04-04"),
new Interval("2011-04-02/2011-04-04")
},
intervals
);
}
}