3 * Copyright (C) 2009 Hyperic, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package org.collectd.api;
22 * Java representation of collectd/src/plugin.h:data_source_t structure.
24 public class DataSource {
25 public static final int TYPE_COUNTER = 0;
26 public static final int TYPE_GAUGE = 1;
27 public static final int TYPE_DERIVE = 2;
28 public static final int TYPE_ABSOLUTE = 3;
30 static final String COUNTER = "COUNTER";
31 static final String GAUGE = "GAUGE";
32 static final String DERIVE = "DERIVE";
33 static final String ABSOLUTE = "ABSOLUTE";
35 static final String NAN = "U";
36 private static final String[] TYPES = { COUNTER, GAUGE, DERIVE, ABSOLUTE };
43 public DataSource (String name, int type, double min, double max) {
45 this._type = TYPE_GAUGE;
46 if (type == TYPE_COUNTER)
47 this._type = TYPE_COUNTER;
48 else if (type == TYPE_DERIVE)
49 this._type = TYPE_DERIVE;
50 else if (type == TYPE_ABSOLUTE)
51 this._type = TYPE_ABSOLUTE;
56 /* Needed in parseDataSource below. Other code should use the above
57 * constructor or `parseDataSource'. */
58 private DataSource () {
59 this._type = TYPE_GAUGE;
62 public String getName() {
66 public void setName(String name) {
70 public int getType() {
74 public void setType(int type) {
78 public double getMin() {
82 public void setMin(double min) {
86 public double getMax() {
90 public void setMax(double max) {
94 static double toDouble(String val) {
95 if (val.equals(NAN)) {
99 return Double.parseDouble(val);
103 private String asString(double val) {
104 if (Double.isNaN(val)) {
108 return String.valueOf(val);
112 public String toString() {
113 StringBuffer sb = new StringBuffer();
114 final char DLM = ':';
115 sb.append(_name).append(DLM);
116 sb.append(TYPES[_type]).append(DLM);
117 sb.append(asString(_min)).append(DLM);
118 sb.append(asString(_max));
119 return sb.toString();
122 static public DataSource parseDataSource (String str)
125 int str_len = str.length ();
126 DataSource dsrc = new DataSource ();
128 /* Ignore trailing commas. This makes it easier for parsing code. */
129 if (str.charAt (str_len - 1) == ',') {
130 str = str.substring (0, str_len - 1);
133 fields = str.split(":");
134 if (fields.length != 4)
137 dsrc._name = fields[0];
139 if (fields[1].equals (DataSource.GAUGE)) {
140 dsrc._type = TYPE_GAUGE;
143 dsrc._type = TYPE_COUNTER;
146 dsrc._min = toDouble (fields[2]);
147 dsrc._max = toDouble (fields[3]);
150 } /* DataSource parseDataSource */
153 /* vim: set sw=4 sts=4 et : */