View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.hicc;
19  
20  import java.util.TreeMap;
21  import java.util.Map.Entry;
22  
23  public class AreaCalculator {
24    public static TreeMap<String, Double> getAreas(
25        TreeMap<String, TreeMap<String, Double>> dataSet) 
26    {
27      TreeMap<String, Double> areas = new TreeMap<String, Double>();
28      for (Entry<String, TreeMap<String, Double>> entry : dataSet.entrySet()) {
29        String key = entry.getKey();
30        Double area = getArea(entry.getValue());
31        areas.put(key, area);
32      }
33      return areas;
34    }
35  
36    public static Double getArea(TreeMap<String, Double> data) {
37      double area = 0;
38      boolean first = true;
39      double x0, x1, y0, y1;
40      x0 = x1 = y0 = y1 = 0;
41      for (Entry<String, Double> entry : data.entrySet()) {
42        double x = Double.parseDouble(entry.getKey());
43        double y = entry.getValue();
44        if (first) {
45          x0 = x;
46          y0 = y;
47          first = false;
48        } else {
49          x1 = x;
50          y1 = y;
51          area += getArea(x0, y0, x1, y1);
52          x0 = x1;
53          y0 = y1;
54        }
55      }
56      return area;
57    }
58  
59    public static Double getArea(double x0, double y0, double x1, double y1) {
60      return (x1 - x0) * (y0 + y1) / 2;
61    }
62  }