Repository: crunch Updated Branches: refs/heads/master 2befc0299 -> 54de73247
CRUNCH-554 Add MAX_COMPARABLES and MIN_COMPARABLES Project: http://git-wip-us.apache.org/repos/asf/crunch/repo Commit: http://git-wip-us.apache.org/repos/asf/crunch/commit/54de7324 Tree: http://git-wip-us.apache.org/repos/asf/crunch/tree/54de7324 Diff: http://git-wip-us.apache.org/repos/asf/crunch/diff/54de7324 Branch: refs/heads/master Commit: 54de732479e7e55e86fbe8048b35d93323bf3b13 Parents: 2befc02 Author: Gabriel Reid <[email protected]> Authored: Wed Jul 29 10:26:52 2015 +0200 Committer: Gabriel Reid <[email protected]> Committed: Thu Aug 6 13:27:23 2015 +0200 ---------------------------------------------------------------------- .../java/org/apache/crunch/fn/Aggregators.java | 218 +++---------------- .../org/apache/crunch/fn/AggregatorsTest.java | 2 + 2 files changed, 36 insertions(+), 184 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/crunch/blob/54de7324/crunch-core/src/main/java/org/apache/crunch/fn/Aggregators.java ---------------------------------------------------------------------- diff --git a/crunch-core/src/main/java/org/apache/crunch/fn/Aggregators.java b/crunch-core/src/main/java/org/apache/crunch/fn/Aggregators.java index 62ee089..c5b0c21 100644 --- a/crunch-core/src/main/java/org/apache/crunch/fn/Aggregators.java +++ b/crunch-core/src/main/java/org/apache/crunch/fn/Aggregators.java @@ -111,11 +111,19 @@ public final class Aggregators { } /** + * Return the maximum of all given {@link Comparable} values. + * @return The newly constructed instance + */ + public static <C extends Comparable<C>> Aggregator<C> MAX_COMPARABLES() { + return new MaxComparables<C>(); + } + + /** * Return the maximum of all given {@code long} values. * @return The newly constructed instance */ public static Aggregator<Long> MAX_LONGS() { - return new MaxLongs(); + return new MaxComparables<Long>(); } /** @@ -133,7 +141,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<Integer> MAX_INTS() { - return new MaxInts(); + return new MaxComparables<Integer>(); } /** @@ -151,7 +159,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<Float> MAX_FLOATS() { - return new MaxFloats(); + return new MaxComparables<Float>(); } /** @@ -169,7 +177,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<Double> MAX_DOUBLES() { - return new MaxDoubles(); + return new MaxComparables<Double>(); } /** @@ -187,7 +195,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<BigInteger> MAX_BIGINTS() { - return new MaxBigInts(); + return new MaxComparables<BigInteger>(); } /** @@ -223,11 +231,19 @@ public final class Aggregators { } /** + * Return the minimum of all given {@link Comparable} values. + * @return The newly constructed instance + */ + public static <C extends Comparable<C>> Aggregator<C> MIN_COMPARABLES() { + return new MinComparables<C>(); + } + + /** * Return the minimum of all given {@code long} values. * @return The newly constructed instance */ public static Aggregator<Long> MIN_LONGS() { - return new MinLongs(); + return new MinComparables<Long>(); } /** @@ -245,7 +261,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<Integer> MIN_INTS() { - return new MinInts(); + return new MinComparables<Integer>(); } /** @@ -263,7 +279,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<Float> MIN_FLOATS() { - return new MinFloats(); + return new MinComparables<Float>(); } /** @@ -281,7 +297,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<Double> MIN_DOUBLES() { - return new MinDoubles(); + return new MinComparables<Double>(); } /** @@ -299,7 +315,7 @@ public final class Aggregators { * @return The newly constructed instance */ public static Aggregator<BigInteger> MIN_BIGINTS() { - return new MinBigInts(); + return new MinComparables<BigInteger>(); } /** @@ -622,92 +638,9 @@ public final class Aggregators { } } - private static class MaxLongs extends SimpleAggregator<Long> { - private Long max = null; - - @Override - public void reset() { - max = null; - } - - @Override - public void update(Long next) { - if (max == null || max < next) { - max = next; - } - } - - @Override - public Iterable<Long> results() { - return ImmutableList.of(max); - } - } - - private static class MaxInts extends SimpleAggregator<Integer> { - private Integer max = null; - - @Override - public void reset() { - max = null; - } - - @Override - public void update(Integer next) { - if (max == null || max < next) { - max = next; - } - } - - @Override - public Iterable<Integer> results() { - return ImmutableList.of(max); - } - } - - private static class MaxFloats extends SimpleAggregator<Float> { - private Float max = null; - - @Override - public void reset() { - max = null; - } - - @Override - public void update(Float next) { - if (max == null || max < next) { - max = next; - } - } - - @Override - public Iterable<Float> results() { - return ImmutableList.of(max); - } - } - - private static class MaxDoubles extends SimpleAggregator<Double> { - private Double max = null; - - @Override - public void reset() { - max = null; - } - - @Override - public void update(Double next) { - if (max == null || max < next) { - max = next; - } - } - - @Override - public Iterable<Double> results() { - return ImmutableList.of(max); - } - } + private static class MaxComparables<C extends Comparable<C>> extends SimpleAggregator<C> { - private static class MaxBigInts extends SimpleAggregator<BigInteger> { - private BigInteger max = null; + private C max = null; @Override public void reset() { @@ -715,83 +648,21 @@ public final class Aggregators { } @Override - public void update(BigInteger next) { + public void update(C next) { if (max == null || max.compareTo(next) < 0) { max = next; } } @Override - public Iterable<BigInteger> results() { + public Iterable<C> results() { return ImmutableList.of(max); } } - private static class MinLongs extends SimpleAggregator<Long> { - private Long min = null; - - @Override - public void reset() { - min = null; - } - - @Override - public void update(Long next) { - if (min == null || min > next) { - min = next; - } - } - - @Override - public Iterable<Long> results() { - return ImmutableList.of(min); - } - } - - private static class MinInts extends SimpleAggregator<Integer> { - private Integer min = null; - - @Override - public void reset() { - min = null; - } - - @Override - public void update(Integer next) { - if (min == null || min > next) { - min = next; - } - } - - @Override - public Iterable<Integer> results() { - return ImmutableList.of(min); - } - } - - private static class MinFloats extends SimpleAggregator<Float> { - private Float min = null; - - @Override - public void reset() { - min = null; - } - - @Override - public void update(Float next) { - if (min == null || min > next) { - min = next; - } - } - - @Override - public Iterable<Float> results() { - return ImmutableList.of(min); - } - } + private static class MinComparables<C extends Comparable<C>> extends SimpleAggregator<C> { - private static class MinDoubles extends SimpleAggregator<Double> { - private Double min = null; + private C min = null; @Override public void reset() { @@ -799,35 +670,14 @@ public final class Aggregators { } @Override - public void update(Double next) { - if (min == null || min > next) { - min = next; - } - } - - @Override - public Iterable<Double> results() { - return ImmutableList.of(min); - } - } - - private static class MinBigInts extends SimpleAggregator<BigInteger> { - private BigInteger min = null; - - @Override - public void reset() { - min = null; - } - - @Override - public void update(BigInteger next) { + public void update(C next) { if (min == null || min.compareTo(next) > 0) { min = next; } } @Override - public Iterable<BigInteger> results() { + public Iterable<C> results() { return ImmutableList.of(min); } } http://git-wip-us.apache.org/repos/asf/crunch/blob/54de7324/crunch-core/src/test/java/org/apache/crunch/fn/AggregatorsTest.java ---------------------------------------------------------------------- diff --git a/crunch-core/src/test/java/org/apache/crunch/fn/AggregatorsTest.java b/crunch-core/src/test/java/org/apache/crunch/fn/AggregatorsTest.java index 9417b08..57dc8f0 100644 --- a/crunch-core/src/test/java/org/apache/crunch/fn/AggregatorsTest.java +++ b/crunch-core/src/test/java/org/apache/crunch/fn/AggregatorsTest.java @@ -71,6 +71,7 @@ public class AggregatorsTest { assertThat(sapply(MAX_DOUBLES(), 29.0, 17.0, 1729.0), is(1729.0)); assertThat(sapply(MAX_FLOATS(), 29f, 1745f, 17f, 1729f), is(1745.0f)); assertThat(sapply(MAX_BIGINTS(), bigInt("29"), bigInt("17"), bigInt("1729")), is(bigInt("1729"))); + assertThat(sapply(Aggregators.<String>MAX_COMPARABLES(), "b", "a", "d", "c"), is("d")); } @Test @@ -81,6 +82,7 @@ public class AggregatorsTest { assertThat(sapply(MIN_DOUBLES(), 29.0, 17.0, 1729.0), is(17.0)); assertThat(sapply(MIN_INTS(), 29, 170, 1729), is(29)); assertThat(sapply(MIN_BIGINTS(), bigInt("29"), bigInt("17"), bigInt("1729")), is(bigInt("17"))); + assertThat(sapply(Aggregators.<String>MIN_COMPARABLES(), "b", "a", "d", "c"), is("a")); } @Test
