flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject [4/5] flink git commit: [FLINK-2626] [core] Minor cleanups in AverageAccumulator and AverageAccumulatorTest
Date Mon, 07 Sep 2015 15:35:19 GMT
[FLINK-2626] [core] Minor cleanups in AverageAccumulator and AverageAccumulatorTest


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/9292e5c4
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/9292e5c4
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/9292e5c4

Branch: refs/heads/master
Commit: 9292e5c4d5f10063853c687dfaac4ed84ecc0631
Parents: 2f8bcf5
Author: Stephan Ewen <sewen@apache.org>
Authored: Mon Sep 7 14:53:07 2015 +0200
Committer: Stephan Ewen <sewen@apache.org>
Committed: Mon Sep 7 14:53:07 2015 +0200

----------------------------------------------------------------------
 .../common/accumulators/AverageAccumulator.java | 10 +++---
 .../accumulators/AverageAccumulatorTest.java    | 33 +++++++++++---------
 2 files changed, 25 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/9292e5c4/flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
b/flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
index 8982928..5ed3c26 100644
--- a/flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
+++ b/flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
@@ -22,10 +22,12 @@ package org.apache.flink.api.common.accumulators;
  * An accumulator that get the average values.
  * Input can be {@code long}, {@code integer}, {@code double} as the result is {@code double}.
  */
-public class AverageAccumulator implements SimpleAccumulator <Double> {
+public class AverageAccumulator implements SimpleAccumulator<Double> {
 
-	private double localValue = 0;
-	private long count = 0;
+	private static final long serialVersionUID = 3672555084179165255L;
+	
+	private double localValue;
+	private long count;
 
 	@Override
 	public void add(Double value) {
@@ -69,7 +71,7 @@ public class AverageAccumulator implements SimpleAccumulator <Double>
{
 			this.count += temp.count;
 			this.localValue += other.getLocalValue();
 		} else {
-			throw new RuntimeException("The merged accumulator must be AverageAccumulator.");
+			throw new IllegalArgumentException("The merged accumulator must be AverageAccumulator.");
 		}
 	}
 

http://git-wip-us.apache.org/repos/asf/flink/blob/9292e5c4/flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
b/flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
index 5cd2a64..9ebd27c 100644
--- a/flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
+++ b/flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
@@ -21,6 +21,7 @@ package org.apache.flink.api.common.accumulators;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -29,7 +30,7 @@ public class AverageAccumulatorTest {
 	@Test
 	public void testGet() {
 		AverageAccumulator average = new AverageAccumulator();
-		assertEquals(Double.valueOf(0), average.getLocalValue());
+		assertEquals(0.0, average.getLocalValue(), 0.0);
 	}
 
 	@Test
@@ -39,45 +40,45 @@ public class AverageAccumulatorTest {
 		for (i1 = 0; i1 < 10; i1++) {
 			average.add(i1);
 		}
-		assertEquals(Double.valueOf(4.5), average.getLocalValue());
+		assertEquals(4.5, average.getLocalValue(), 0.0);
 		average.resetLocal();
 
 		Integer i2;
 		for (i2 = 0; i2 < 10; i2++) {
 			average.add(i2);
 		}
-		assertEquals(Double.valueOf(4.5), average.getLocalValue());
+		assertEquals(4.5, average.getLocalValue(), 0.0);
 		average.resetLocal();
 
 		long i3;
 		for (i3 = 0; i3 < 10; i3++) {
 			average.add(i3);
 		}
-		assertEquals(Double.valueOf(4.5), average.getLocalValue());
+		assertEquals(4.5, average.getLocalValue(), 0.0);
 		average.resetLocal();
 
 		Long i4;
 		for (i4 = 0L; i4 < 10; i4++) {
 			average.add(i4);
 		}
-		assertEquals(Double.valueOf(4.5), average.getLocalValue());
+		assertEquals(4.5, average.getLocalValue(), 0.0);
 		average.resetLocal();
 
 		double i5;
 		for (i5 = 0; i5 < 10; i5++) {
 			average.add(i5);
 		}
-		assertEquals(Double.valueOf(4.5), average.getLocalValue());
+		assertEquals(4.5, average.getLocalValue(), 0.0);
 		average.resetLocal();
 
 		Double i6;
 		for (i6 = 0.0; i6 < 10; i6++) {
 			average.add(i6);
 		}
-		assertEquals(Double.valueOf(4.5), average.getLocalValue());
+		assertEquals(4.5, average.getLocalValue(), 0.0);
 		average.resetLocal();
 
-		assertEquals(Double.valueOf(0), average.getLocalValue());
+		assertEquals(0.0, average.getLocalValue(), 0.0);
 	}
 
 	@Test
@@ -87,7 +88,7 @@ public class AverageAccumulatorTest {
 		average.add(1);
 		averageNew.add(2);
 		average.merge(averageNew);
-		assertEquals(Double.valueOf(1.5), average.getLocalValue());
+		assertEquals(1.5, average.getLocalValue(), 0.0);
 	}
 
 	@Test
@@ -97,11 +98,15 @@ public class AverageAccumulatorTest {
 		average.add(1);
 		try {
 			average.merge(averageNew);
-		} catch (Exception e) {
-			assertTrue(e.toString().indexOf("The merged accumulator must be AverageAccumulator.")
!= -1);
-			return;
+			fail("should fail with an exception");
+		}
+		catch (IllegalArgumentException e) {
+			assertNotNull(e.getMessage());
+			assertTrue(e.getMessage().contains("The merged accumulator must be AverageAccumulator."));
+		}
+		catch (Throwable t) {
+			fail("wrong exception; expected IllegalArgumentException but found " + t.getClass().getName());
 		}
-		fail();
 	}
 
 	@Test
@@ -109,6 +114,6 @@ public class AverageAccumulatorTest {
 		AverageAccumulator average = new AverageAccumulator();
 		average.add(1);
 		AverageAccumulator averageNew = average.clone();
-		assertEquals(Double.valueOf(1), averageNew.getLocalValue());
+		assertEquals(1, averageNew.getLocalValue(), 0.0);
 	}
 }


Mime
View raw message