Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
AvgMetricAggregatorProvider.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
3  * http://www.griddynamics.com
4  *
5  * This library is free software; you can redistribute it and/or modify it under the terms of
6  * the Apache License; either
7  * version 2.0 of the License, or any later version.
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
13  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
17  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  */
20 
21 package com.griddynamics.jagger.engine.e1.collector;
22 
23 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.AVERAGE_AGGREGATOR_ID;
24 
29 public class AvgMetricAggregatorProvider implements MetricAggregatorProvider {
30 
32  @Override
34  return new AvgMetricAggregator();
35  }
36 
37  private static class AvgMetricAggregator implements MetricAggregator<Number> {
38 
39  Double sum = null;
40  long count = 0;
41 
42  @Override
43  public void append(Number calculated) {
44  if (sum == null)
45  sum = new Double(0);
46 
47  sum += calculated.doubleValue();
48  ++count;
49  }
50 
51  @Override
52  public Number getAggregated() {
53  if (sum == null)
54  return null;
55 
56  if (count == 0) {
57  return 0;
58  }
59 
60  double result = sum / count;
61 
62  return Double.valueOf(result);
63  }
64 
65  @Override
66  public void reset() {
67  sum = null;
68  count = 0;
69  }
70 
71  @Override
72  public String getName() {
73  return AVERAGE_AGGREGATOR_ID;
74  }
75 
76  @Override
77  public String toString() {
78  return "AvgMetricAggregator{" +
79  "sum=" + sum +
80  ", count=" + count +
81  '}';
82  }
83  }
84 }
85