1 /* ============================================================
2 * JRobin : Pure java implementation of RRDTool's functionality
3 * ============================================================
4 *
5 * Project Info: http://www.jrobin.org
6 * Project Lead: Sasa Markovic (saxon@jrobin.org);
7 *
8 * (C) Copyright 2003-2005, by Sasa Markovic.
9 *
10 * Developers: Sasa Markovic (saxon@jrobin.org)
11 *
12 *
13 * This library is free software; you can redistribute it and/or modify it under the terms
14 * of the GNU Lesser General Public License as published by the Free Software Foundation;
15 * either version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License along with this
22 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23 * Boston, MA 02111-1307, USA.
24 */
25
26 package org.jrobin.data;
27
28 import org.jrobin.core.ConsolFuns;
29 import org.jrobin.core.RrdException;
30 import org.jrobin.core.Util;
31
32 /**
33 * Simple class which holds aggregated values (MIN, MAX, FIRST, LAST, AVERAGE and TOTAL). You
34 * don't need to create objects of this class directly. Objects of this class are returned from
35 * <code>getAggregates()</code> method in
36 * {@link org.jrobin.core.FetchData#getAggregates(String) FetchData} and
37 * {@link DataProcessor#getAggregates(String)} DataProcessor} classes.
38 */
39 public class Aggregates implements ConsolFuns {
40 double min = Double.NaN, max = Double.NaN;
41 double first = Double.NaN, last = Double.NaN;
42 double average = Double.NaN, total = Double.NaN;
43
44 Aggregates() {
45 // NOP;
46 }
47
48 /**
49 * Returns the minimal value
50 *
51 * @return Minimal value
52 */
53 public double getMin() {
54 return min;
55 }
56
57 /**
58 * Returns the maximum value
59 *
60 * @return Maximum value
61 */
62 public double getMax() {
63 return max;
64 }
65
66 /**
67 * Returns the first falue
68 *
69 * @return First value
70 */
71 public double getFirst() {
72 return first;
73 }
74
75 /**
76 * Returns the last value
77 *
78 * @return Last value
79 */
80 public double getLast() {
81 return last;
82 }
83
84 /**
85 * Returns average
86 *
87 * @return Average value
88 */
89 public double getAverage() {
90 return average;
91 }
92
93 /**
94 * Returns total value
95 *
96 * @return Total value
97 */
98 public double getTotal() {
99 return total;
100 }
101
102 /**
103 * Returns single aggregated value for the give consolidation function
104 *
105 * @param consolFun Consolidation function: MIN, MAX, FIRST, LAST, AVERAGE, TOTAL. These constants
106 * are conveniently defined in the {@link org.jrobin.core.ConsolFuns ConsolFuns} interface.
107 * @return Aggregated value
108 * @throws RrdException Thrown if unsupported consolidation function is supplied
109 */
110 public double getAggregate(String consolFun) throws RrdException {
111 if (consolFun.equals(CF_AVERAGE)) {
112 return average;
113 }
114 else if (consolFun.equals(CF_FIRST)) {
115 return first;
116 }
117 else if (consolFun.equals(CF_LAST)) {
118 return last;
119 }
120 else if (consolFun.equals(CF_MAX)) {
121 return max;
122 }
123 else if (consolFun.equals(CF_MIN)) {
124 return min;
125 }
126 else if (consolFun.equals(CF_TOTAL)) {
127 return total;
128 }
129 else {
130 throw new RrdException("Unknown consolidation function: " + consolFun);
131 }
132 }
133
134 /**
135 * Returns String representing all aggregated values. Just for debugging purposes.
136 *
137 * @return String containing all aggregated values
138 */
139 public String dump() {
140 return "MIN=" + Util.formatDouble(min) + ", MAX=" + Util.formatDouble(max) + "\n" +
141 "FIRST=" + Util.formatDouble(first) + ", LAST=" + Util.formatDouble(last) + "\n" +
142 "AVERAGE=" + Util.formatDouble(average) + ", TOTAL=" + Util.formatDouble(total);
143 }
144 }
145