1 /*
2 * jcollectd
3 * Copyright (C) 2009 Hyperic, Inc.
4 *
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.
8 *
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.
13 *
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
17 */
19 package org.collectd.api;
21 /**
22 * Java representation of collectd/src/plugin.h:data_source_t structure.
23 */
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 };
38 String _name;
39 int _type;
40 double _min;
41 double _max;
43 public DataSource (String name, int type, double min, double max) {
44 this._name = name;
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;
52 this._min = min;
53 this._max = max;
54 }
56 /* Needed in parseDataSource below. Other code should use the above
57 * constructor or `parseDataSource'. */
58 private DataSource () {
59 this._type = TYPE_GAUGE;
60 }
62 public String getName() {
63 return _name;
64 }
66 public void setName(String name) {
67 _name = name;
68 }
70 public int getType() {
71 return _type;
72 }
74 public void setType(int type) {
75 _type = type;
76 }
78 public double getMin() {
79 return _min;
80 }
82 public void setMin(double min) {
83 _min = min;
84 }
86 public double getMax() {
87 return _max;
88 }
90 public void setMax(double max) {
91 _max = max;
92 }
94 static double toDouble(String val) {
95 if (val.equals(NAN)) {
96 return Double.NaN;
97 }
98 else {
99 return Double.parseDouble(val);
100 }
101 }
103 private String asString(double val) {
104 if (Double.isNaN(val)) {
105 return NAN;
106 }
107 else {
108 return String.valueOf(val);
109 }
110 }
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();
120 }
122 static public DataSource parseDataSource (String str)
123 {
124 String[] fields;
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);
131 }
133 fields = str.split(":");
134 if (fields.length != 4)
135 return (null);
137 dsrc._name = fields[0];
139 if (fields[1].equals (DataSource.GAUGE)) {
140 dsrc._type = TYPE_GAUGE;
141 }
142 else {
143 dsrc._type = TYPE_COUNTER;
144 }
146 dsrc._min = toDouble (fields[2]);
147 dsrc._max = toDouble (fields[3]);
149 return (dsrc);
150 } /* DataSource parseDataSource */
151 }
153 /* vim: set sw=4 sts=4 et : */