chukwa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ey...@apache.org
Subject svn commit: r760641 - in /hadoop/chukwa/trunk/src: java/org/apache/hadoop/chukwa/hicc/AreaCalculator.java test/org/apache/hadoop/chukwa/hicc/AreaCalculatorTest.java
Date Tue, 31 Mar 2009 21:10:33 GMT
Author: eyang
Date: Tue Mar 31 21:10:33 2009
New Revision: 760641

URL: http://svn.apache.org/viewvc?rev=760641&view=rev
Log:
CHUKWA-69.  Calculate trapezoid area for a given series of data.  (Contribute by Cheng Zhang
via Eric Yang)

Added:
    hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/hicc/AreaCalculator.java
    hadoop/chukwa/trunk/src/test/org/apache/hadoop/chukwa/hicc/AreaCalculatorTest.java

Added: hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/hicc/AreaCalculator.java
URL: http://svn.apache.org/viewvc/hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/hicc/AreaCalculator.java?rev=760641&view=auto
==============================================================================
--- hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/hicc/AreaCalculator.java (added)
+++ hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/hicc/AreaCalculator.java Tue Mar
31 21:10:33 2009
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.chukwa.hicc;
+
+import java.util.TreeMap;
+import java.util.Map.Entry;
+
+public class AreaCalculator {
+  public static TreeMap<String, Double> getAreas(
+      TreeMap<String, TreeMap<String, Double>> dataSet) 
+  {
+    TreeMap<String, Double> areas = new TreeMap<String, Double>();
+    for (Entry<String, TreeMap<String, Double>> entry : dataSet.entrySet()) {
+      String key = entry.getKey();
+      Double area = getArea(entry.getValue());
+      areas.put(key, area);
+    }
+    return areas;
+  }
+
+  public static Double getArea(TreeMap<String, Double> data) {
+    double area = 0;
+    boolean first = true;
+    double x0, x1, y0, y1;
+    x0 = x1 = y0 = y1 = 0;
+    for (Entry<String, Double> entry : data.entrySet()) {
+      double x = Double.parseDouble(entry.getKey());
+      double y = entry.getValue();
+      if (first) {
+        x0 = x;
+        y0 = y;
+        first = false;
+      } else {
+        x1 = x;
+        y1 = y;
+        area += getArea(x0, y0, x1, y1);
+        x0 = x1;
+        y0 = y1;
+      }
+    }
+    return area;
+  }
+
+  public static Double getArea(double x0, double y0, double x1, double y1) {
+    return (x1 - x0) * (y0 + y1) / 2;
+  }
+}

Added: hadoop/chukwa/trunk/src/test/org/apache/hadoop/chukwa/hicc/AreaCalculatorTest.java
URL: http://svn.apache.org/viewvc/hadoop/chukwa/trunk/src/test/org/apache/hadoop/chukwa/hicc/AreaCalculatorTest.java?rev=760641&view=auto
==============================================================================
--- hadoop/chukwa/trunk/src/test/org/apache/hadoop/chukwa/hicc/AreaCalculatorTest.java (added)
+++ hadoop/chukwa/trunk/src/test/org/apache/hadoop/chukwa/hicc/AreaCalculatorTest.java Tue
Mar 31 21:10:33 2009
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.chukwa.hicc;
+
+import java.util.Date;
+import java.util.Random;
+import java.util.TreeMap;
+
+import junit.framework.TestCase;
+
+public class AreaCalculatorTest extends TestCase {
+
+  public void testGetAreaTreeMapOfStringTreeMapOfStringDouble() {
+    TreeMap<String, TreeMap<String, Double>> maps = new TreeMap<String, TreeMap<String,
Double>>();
+    maps.put("abc", getDots());
+    maps.put("def", getDots());
+
+    TreeMap<String, Double> areas = AreaCalculator.getAreas(maps);
+    System.out.println("Area of 'abc': " + areas.get("abc"));
+    System.out.println("Area of 'def': " + areas.get("def"));
+  }
+
+  public void testGetAreaTreeMapOfStringDouble() {
+    TreeMap<String, Double> map = getDots();
+    System.out.println("Area: " + AreaCalculator.getArea(map));
+  }
+
+  public void testGetAreaDoubleDoubleDoubleDouble() {
+    Double area = AreaCalculator.getArea(1, 4, 2, 4);
+    System.out.println(area);
+    assertEquals(true, area > 3.99999 && area < 4.00001);
+  }
+
+  private TreeMap<String, Double> getDots() {
+    TreeMap<String, Double> map = new TreeMap<String, Double>();
+    long now = new Date().getTime();
+    Random r = new Random(now);
+    for (long i = 0; i < 4; i++) {
+      double value = r.nextInt(10) + 2;
+      System.out.println(now + ": " + value);
+      map.put(now + "", value);
+      now += 1000;
+    }
+    return map;
+  }
+}



Mime
View raw message