Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
SumMetricAggregatorProvider.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 org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 
31 
33  @Override
35  return new SumMetricAggregator();
36  }
37 
38  private static class SumMetricAggregator implements MetricAggregator<Number> {
39 
40  Logger log = LoggerFactory.getLogger(SumMetricAggregator.class);
41 
42  Double sum = null;
43 
44  @Override
45  public void append(Number calculated) {
46  log.debug("append({})", calculated);
47  if (sum == null)
48  sum = new Double(0);
49 
50  sum += calculated.doubleValue();
51  }
52 
53  @Override
54  public Number getAggregated() {
55  if (sum == null)
56  return null;
57 
58  return sum.doubleValue();
59  }
60 
61  @Override
62  public void reset() {
63  log.debug("Reset aggregator");
64  sum = null;
65  }
66 
67  @Override
68  public String getName() {
69  return "sum";
70  }
71 
72  @Override
73  public String toString() {
74  return "SumMetricAggregator{" +
75  "log=" + log +
76  ", sum=" + sum +
77  '}';
78  }
79  }
80 }