1 25 26 package org.jrobin.core; 27 28 import java.io.IOException ; 29 import java.util.StringTokenizer ; 30 31 53 public class Sample { 54 private RrdDb parentDb; 55 private long time; 56 private String [] dsNames; 57 private double[] values; 58 59 Sample(RrdDb parentDb, long time) throws IOException { 60 this.parentDb = parentDb; 61 this.time = time; 62 this.dsNames = parentDb.getDsNames(); 63 values = new double[dsNames.length]; 64 clearCurrentValues(); 65 } 66 67 private void clearCurrentValues() { 68 for(int i = 0; i < values.length; i++) { 69 values[i] = Double.NaN; 70 } 71 } 72 73 79 public void setValue(String dsName, double value) throws RrdException { 80 for(int i = 0; i < values.length; i++) { 81 if(dsNames[i].equals(dsName)) { 82 values[i] = value; 83 return; 84 } 85 } 86 throw new RrdException("Datasource " + dsName + " not found"); 87 } 88 89 96 public void setValue(int i, double value) throws RrdException { 97 if(i < values.length) { 98 values[i] = value; 99 return; 100 } 101 throw new RrdException("Sample datasource index " + i + " out of bounds"); 102 } 103 104 112 public void setValues(double[] values) throws RrdException { 113 if(values.length <= this.values.length) { 114 for(int i = 0; i < values.length; i++) { 115 this.values[i] = values[i]; 116 } 117 } 118 else { 119 throw new RrdException("Invalid number of values specified (found " + 120 values.length + ", only " + dsNames.length + " allowed)"); 121 } 122 } 123 124 128 public double[] getValues() { 129 return values; 130 } 131 132 136 public long getTime() { 137 return time; 138 } 139 140 144 public void setTime(long time) { 145 this.time = time; 146 } 147 148 153 public String [] getDsNames() { 154 return dsNames; 155 } 156 157 176 public void set(String timeAndValues) throws RrdException { 177 StringTokenizer tokenizer = new StringTokenizer (timeAndValues, ":", false); 178 int n = tokenizer.countTokens(); 179 if(n > values.length + 1) { 180 throw new RrdException("Invalid number of values specified (found " + 181 values.length + ", " + dsNames.length + " allowed)"); 182 } 183 String timeToken = tokenizer.nextToken(); 184 try { 185 time = Long.parseLong(timeToken); 186 } 187 catch(NumberFormatException nfe) { 188 if(timeToken.equalsIgnoreCase("N") || timeToken.equalsIgnoreCase("NOW")) { 189 time = Util.getTime(); 190 } 191 else { 192 throw new RrdException("Invalid sample timestamp: " + timeToken); 193 } 194 } 195 for(int i = 0; tokenizer.hasMoreTokens(); i++) { 196 try { 197 values[i] = Double.parseDouble(tokenizer.nextToken()); 198 } 199 catch (NumberFormatException nfe) { 200 } 202 } 203 } 204 205 212 public void update() throws IOException , RrdException { 213 parentDb.store(this); 214 clearCurrentValues(); 215 } 216 217 233 public void setAndUpdate(String timeAndValues) throws IOException , RrdException { 234 set(timeAndValues); 235 update(); 236 } 237 238 242 public String dump() { 243 StringBuffer buffer = new StringBuffer ("update \""); 244 buffer.append(parentDb.getRrdBackend().getPath() + "\" " + time); 245 for(int i = 0; i < values.length; i++) { 246 buffer.append(":"); 247 buffer.append(Util.formatDouble(values[i], "U", false)); 248 } 249 return buffer.toString(); 250 } 251 252 String getRrdToolCommand() { 253 return dump(); 254 } 255 } 256 | Popular Tags |