Repository: incubator-apex-malhar Updated Branches: refs/heads/devel-3 873667bce -> 3d0166bfe
- MLHR-1863 #resolve #comment Make time buckets comparable. Project: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/commit/28094b7c Tree: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/tree/28094b7c Diff: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/diff/28094b7c Branch: refs/heads/devel-3 Commit: 28094b7c49abee63e7aacaa58af3f59512fcd2dd Parents: 66a1d58 Author: Timothy Farkas <[email protected]> Authored: Tue Sep 29 12:35:16 2015 -0700 Committer: Timothy Farkas <[email protected]> Committed: Tue Sep 29 15:13:06 2015 -0700 ---------------------------------------------------------------------- .../lib/appdata/schemas/CustomTimeBucket.java | 13 +++++++- .../lib/appdata/schemas/TimeBucket.java | 16 ++++++++++ .../appdata/schemas/CustomTimeBucketTest.java | 18 +++++++++++ .../lib/appdata/schemas/TimeBucketTest.java | 33 ++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/blob/28094b7c/library/src/main/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucket.java ---------------------------------------------------------------------- diff --git a/library/src/main/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucket.java b/library/src/main/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucket.java index 21ae425..5987324 100644 --- a/library/src/main/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucket.java +++ b/library/src/main/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucket.java @@ -26,7 +26,7 @@ import com.google.common.base.Preconditions; /** * This represents a {@link TimeBucket} which can be a multiple of a time unit. */ -public class CustomTimeBucket implements Serializable +public class CustomTimeBucket implements Serializable, Comparable<CustomTimeBucket> { private static final long serialVersionUID = 201509221545L; @@ -189,4 +189,15 @@ public class CustomTimeBucket implements Serializable return true; } + @Override + public int compareTo(CustomTimeBucket other) + { + if (this.numMillis < other.numMillis) { + return -1; + } else if (this.numMillis == other.numMillis) { + return 0; + } + + return 1; + } } http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/blob/28094b7c/library/src/main/java/com/datatorrent/lib/appdata/schemas/TimeBucket.java ---------------------------------------------------------------------- diff --git a/library/src/main/java/com/datatorrent/lib/appdata/schemas/TimeBucket.java b/library/src/main/java/com/datatorrent/lib/appdata/schemas/TimeBucket.java index 01ff4a0..c4a6d0b 100644 --- a/library/src/main/java/com/datatorrent/lib/appdata/schemas/TimeBucket.java +++ b/library/src/main/java/com/datatorrent/lib/appdata/schemas/TimeBucket.java @@ -16,6 +16,7 @@ package com.datatorrent.lib.appdata.schemas; import java.util.Collections; +import java.util.Comparator; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -225,4 +226,19 @@ public enum TimeBucket Preconditions.checkArgument(SUFFIXES.contains(suffix)); return SUFFIX_TO_TIME_BUCKET.get(suffix); } + + public static class TimeBucketComparator implements Comparator<TimeBucket> + { + public static final TimeBucketComparator INSTANCE = new TimeBucketComparator(); + + private TimeBucketComparator() + { + } + + @Override + public int compare(TimeBucket timeBucketA, TimeBucket timeBucketB) + { + return timeBucketA.getTimeUnit().compareTo(timeBucketB.getTimeUnit()); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/blob/28094b7c/library/src/test/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucketTest.java ---------------------------------------------------------------------- diff --git a/library/src/test/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucketTest.java b/library/src/test/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucketTest.java index 1fac867..b6ebef8 100644 --- a/library/src/test/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucketTest.java +++ b/library/src/test/java/com/datatorrent/lib/appdata/schemas/CustomTimeBucketTest.java @@ -59,4 +59,22 @@ public class CustomTimeBucketTest Assert.assertEquals(expected, customTimeBucket.roundDown(val)); } + + @Test + public void compareTest() + { + CustomTimeBucket bigger = new CustomTimeBucket("180m"); + CustomTimeBucket smaller = new CustomTimeBucket("2h"); + + Assert.assertTrue(bigger.compareTo(bigger) == 0); + Assert.assertTrue(smaller.compareTo(smaller) == 0); + + Assert.assertTrue(bigger.compareTo(smaller) > 0); + Assert.assertTrue(smaller.compareTo(bigger) < 0); + + smaller = new CustomTimeBucket("91m"); + + Assert.assertTrue(bigger.compareTo(smaller) > 0); + Assert.assertTrue(smaller.compareTo(bigger) < 0); + } } http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/blob/28094b7c/library/src/test/java/com/datatorrent/lib/appdata/schemas/TimeBucketTest.java ---------------------------------------------------------------------- diff --git a/library/src/test/java/com/datatorrent/lib/appdata/schemas/TimeBucketTest.java b/library/src/test/java/com/datatorrent/lib/appdata/schemas/TimeBucketTest.java new file mode 100644 index 0000000..b43ee46 --- /dev/null +++ b/library/src/test/java/com/datatorrent/lib/appdata/schemas/TimeBucketTest.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015 DataTorrent + * + * 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 com.datatorrent.lib.appdata.schemas; + +import org.junit.Assert; +import org.junit.Test; + +import com.datatorrent.lib.appdata.schemas.TimeBucket.TimeBucketComparator; + +public class TimeBucketTest +{ + @Test + public void compareTimeBuckets() + { + Assert.assertTrue(TimeBucketComparator.INSTANCE.compare(TimeBucket.HOUR, TimeBucket.MINUTE) > 0); + Assert.assertTrue(TimeBucketComparator.INSTANCE.compare(TimeBucket.MINUTE, TimeBucket.HOUR) < 0); + Assert.assertTrue(TimeBucketComparator.INSTANCE.compare(TimeBucket.MINUTE, TimeBucket.MINUTE) == 0); + } +}
