KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > ArcState


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, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
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.core;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Class to represent internal RRD archive state for a single datasource. Objects of this
32  * class are never manipulated directly, it's up to JRobin framework to manage
33  * internal arcihve states.<p>
34  *
35  * @author <a HREF="mailto:saxon@jrobin.org">Sasa Markovic</a>
36  */

37 public class ArcState implements RrdUpdater {
38     private Archive parentArc;
39
40     private RrdDouble accumValue;
41     private RrdLong nanSteps;
42
43     ArcState(Archive parentArc, boolean shouldInitialize) throws IOException JavaDoc {
44         this.parentArc = parentArc;
45         accumValue = new RrdDouble(this);
46         nanSteps = new RrdLong(this);
47         if(shouldInitialize) {
48             Header header = parentArc.getParentDb().getHeader();
49             long step = header.getStep();
50             long lastUpdateTime = header.getLastUpdateTime();
51             long arcStep = parentArc.getArcStep();
52             long initNanSteps = (Util.normalize(lastUpdateTime, step) -
53                 Util.normalize(lastUpdateTime, arcStep)) / step;
54             accumValue.set(Double.NaN);
55             nanSteps.set(initNanSteps);
56         }
57     }
58
59     String JavaDoc dump() throws IOException JavaDoc {
60         return "accumValue:" + accumValue.get() + " nanSteps:" + nanSteps.get() + "\n";
61     }
62
63     void setNanSteps(long value) throws IOException JavaDoc {
64         nanSteps.set(value);
65     }
66
67     /**
68      * Returns the number of currently accumulated NaN steps.
69      *
70      * @return Number of currently accumulated NaN steps.
71      * @throws IOException Thrown in case of I/O error
72      */

73     public long getNanSteps() throws IOException JavaDoc {
74         return nanSteps.get();
75     }
76
77     void setAccumValue(double value) throws IOException JavaDoc {
78         accumValue.set(value);
79     }
80
81     /**
82      * Returns the value accumulated so far.
83      *
84      * @return Accumulated value
85      * @throws IOException Thrown in case of I/O error
86      */

87     public double getAccumValue() throws IOException JavaDoc {
88         return accumValue.get();
89     }
90
91     /**
92      * Returns the Archive object to which this ArcState object belongs.
93      *
94      * @return Parent Archive object.
95      */

96     public Archive getParent() {
97         return parentArc;
98     }
99
100     void appendXml(XmlWriter writer) throws IOException JavaDoc {
101         writer.startTag("ds");
102         writer.writeTag("value", accumValue.get());
103         writer.writeTag("unknown_datapoints", nanSteps.get());
104         writer.closeTag(); // ds
105
}
106
107     /**
108      * Copies object's internal state to another ArcState object.
109      * @param other New ArcState object to copy state to
110      * @throws IOException Thrown in case of I/O error
111      * @throws RrdException Thrown if supplied argument is not an ArcState object
112      */

113     public void copyStateTo(RrdUpdater other) throws IOException JavaDoc, RrdException {
114         if(!(other instanceof ArcState)) {
115             throw new RrdException(
116                 "Cannot copy ArcState object to " + other.getClass().getName());
117         }
118         ArcState arcState = (ArcState) other;
119         arcState.accumValue.set(accumValue.get());
120         arcState.nanSteps.set(nanSteps.get());
121     }
122
123     /**
124      * Returns the underlying storage (backend) object which actually performs all
125      * I/O operations.
126      * @return I/O backend object
127      */

128     public RrdBackend getRrdBackend() {
129         return parentArc.getRrdBackend();
130     }
131
132     /**
133      * Required to implement RrdUpdater interface. You should never call this method directly.
134      * @return Allocator object
135      */

136     public RrdAllocator getRrdAllocator() {
137         return parentArc.getRrdAllocator();
138     }
139 }
140
Popular Tags