001    /* ============================================================
002     * JRobin : Pure java implementation of RRDTool's functionality
003     * ============================================================
004     *
005     * Project Info:  http://www.jrobin.org
006     * Project Lead:  Sasa Markovic (saxon@jrobin.org);
007     *
008     * (C) Copyright 2003-2005, by Sasa Markovic.
009     *
010     * Developers:    Sasa Markovic (saxon@jrobin.org)
011     *
012     *
013     * This library is free software; you can redistribute it and/or modify it under the terms
014     * of the GNU Lesser General Public License as published by the Free Software Foundation;
015     * either version 2.1 of the License, or (at your option) any later version.
016     *
017     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019     * See the GNU Lesser General Public License for more details.
020     *
021     * You should have received a copy of the GNU Lesser General Public License along with this
022     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023     * Boston, MA 02111-1307, USA.
024     */
025    
026    package org.jrobin.data;
027    
028    import org.jrobin.core.ConsolFuns;
029    import org.jrobin.core.RrdException;
030    import org.jrobin.core.Util;
031    
032    /**
033     * Simple class which holds aggregated values (MIN, MAX, FIRST, LAST, AVERAGE and TOTAL). You
034     * don't need to create objects of this class directly. Objects of this class are returned from
035     * <code>getAggregates()</code> method in
036     * {@link org.jrobin.core.FetchData#getAggregates(String) FetchData} and
037     * {@link DataProcessor#getAggregates(String)} DataProcessor} classes.
038     */
039    public class Aggregates implements ConsolFuns {
040            double min = Double.NaN, max = Double.NaN;
041            double first = Double.NaN, last = Double.NaN;
042            double average = Double.NaN, total = Double.NaN;
043    
044            Aggregates() {
045                    // NOP;
046            }
047    
048            /**
049             * Returns the minimal value
050             *
051             * @return Minimal value
052             */
053            public double getMin() {
054                    return min;
055            }
056    
057            /**
058             * Returns the maximum value
059             *
060             * @return Maximum value
061             */
062            public double getMax() {
063                    return max;
064            }
065    
066            /**
067             * Returns the first falue
068             *
069             * @return First value
070             */
071            public double getFirst() {
072                    return first;
073            }
074    
075            /**
076             * Returns the last value
077             *
078             * @return Last value
079             */
080            public double getLast() {
081                    return last;
082            }
083    
084            /**
085             * Returns average
086             *
087             * @return Average value
088             */
089            public double getAverage() {
090                    return average;
091            }
092    
093            /**
094             * Returns total value
095             *
096             * @return Total value
097             */
098            public double getTotal() {
099                    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    }