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.core;
027    
028    /**
029     * Class to represent single archive definition within the RRD.
030     * Archive definition consists of the following four elements:
031     * <p/>
032     * <ul>
033     * <li>consolidation function
034     * <li>X-files factor
035     * <li>number of steps
036     * <li>number of rows.
037     * </ul>
038     * <p>For the complete explanation of all archive definition parameters, see RRDTool's
039     * <a href="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a>
040     * </p>
041     *
042     * @author <a href="mailto:saxon@jrobin.org">Sasa Markovic</a>
043     */
044    
045    public class ArcDef implements ConsolFuns {
046            /**
047             * array of valid consolidation function names
048             */
049            public static final String CONSOL_FUNS[] = {CF_AVERAGE, CF_MAX, CF_MIN, CF_LAST};
050    
051            private String consolFun;
052            private double xff;
053            private int steps, rows;
054    
055            /**
056             * <p>Creates new archive definition object. This object should be passed as argument to
057             * {@link RrdDef#addArchive(ArcDef) addArchive()} method of
058             * {@link RrdDb RrdDb} object.</p>
059             * <p/>
060             * <p>For the complete explanation of all archive definition parameters, see RRDTool's
061             * <a href="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a></p>
062             *
063             * @param consolFun Consolidation function. Allowed values are "AVERAGE", "MIN",
064             *                  "MAX" and "LAST" (these string constants are conveniently defined in the
065             *                  {@link ConsolFuns} class).
066             * @param xff      X-files factor, between 0 and 1.
067             * @param steps  Number of archive steps.
068             * @param rows    Number of archive rows.
069             * @throws RrdException Thrown if any parameter has illegal value.
070             */
071            public ArcDef(String consolFun, double xff, int steps, int rows) throws RrdException {
072                    this.consolFun = consolFun;
073                    this.xff = xff;
074                    this.steps = steps;
075                    this.rows = rows;
076                    validate();
077            }
078    
079            /**
080             * Returns consolidation function.
081             *
082             * @return Consolidation function.
083             */
084            public String getConsolFun() {
085                    return consolFun;
086            }
087    
088            /**
089             * Returns the X-files factor.
090             *
091             * @return X-files factor value.
092             */
093            public double getXff() {
094                    return xff;
095            }
096    
097            /**
098             * Returns the number of primary RRD steps which complete a single archive step.
099             *
100             * @return Number of steps.
101             */
102            public int getSteps() {
103                    return steps;
104            }
105    
106            /**
107             * Returns the number of rows (aggregated values) stored in the archive.
108             *
109             * @return Number of rows.
110             */
111            public int getRows() {
112                    return rows;
113            }
114    
115            private void validate() throws RrdException {
116                    if (!isValidConsolFun(consolFun)) {
117                            throw new RrdException("Invalid consolidation function specified: " + consolFun);
118                    }
119                    if (Double.isNaN(xff) || xff < 0.0 || xff >= 1.0) {
120                            throw new RrdException("Invalid xff, must be >= 0 and < 1: " + xff);
121                    }
122                    if (steps < 1 || rows < 2) {
123                            throw new RrdException("Invalid steps/rows settings: " + steps + "/" + rows +
124                                            ". Minimal values allowed are steps=1, rows=2");
125                    }
126            }
127    
128            /**
129             * Returns string representing archive definition (RRDTool format).
130             *
131             * @return String containing all archive definition parameters.
132             */
133            public String dump() {
134                    return "RRA:" + consolFun + ":" + xff + ":" + steps + ":" + rows;
135            }
136    
137            /**
138             * Checks if two archive definitions are equal.
139             * Archive definitions are considered equal if they have the same number of steps
140             * and the same consolidation function. It is not possible to create RRD with two
141             * equal archive definitions.
142             *
143             * @param obj Archive definition to compare with.
144             * @return <code>true</code> if archive definitions are equal,
145             *         <code>false</code> otherwise.
146             */
147            public boolean equals(Object obj) {
148                    if (obj instanceof ArcDef) {
149                            ArcDef arcObj = (ArcDef) obj;
150                            return consolFun.equals(arcObj.consolFun) && steps == arcObj.steps;
151                    }
152                    return false;
153            }
154    
155            /**
156             * Checks if function argument represents valid consolidation function name.
157             *
158             * @param consolFun Consolidation function to be checked
159             * @return <code>true</code> if <code>consolFun</code> is valid consolidation function,
160             *         <code>false</code> otherwise.
161             */
162            public static boolean isValidConsolFun(String consolFun) {
163                    for (String cFun : CONSOL_FUNS) {
164                            if (cFun.equals(consolFun)) {
165                                    return true;
166                            }
167                    }
168                    return false;
169            }
170    
171            void setRows(int rows) {
172                    this.rows = rows;
173            }
174    
175            boolean exactlyEqual(ArcDef def) {
176                    return consolFun.equals(def.consolFun) && xff == def.xff &&
177                                    steps == def.steps && rows == def.rows;
178            }
179    }