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;
28 static final String COUNTER = "COUNTER";
29 static final String GAUGE = "GAUGE";
31 static final String NAN = "U";
32 private static final String[] TYPES = { COUNTER, GAUGE };
34 String _name;
35 int _type;
36 double _min;
37 double _max;
39 public DataSource (String name, int type, double min, double max) {
40 this._name = name;
41 this._type = TYPE_GAUGE;
42 if (type == TYPE_COUNTER)
43 this._type = TYPE_COUNTER;
44 this._min = min;
45 this._max = max;
46 }
48 /* Needed in parseDataSource below. Other code should use the above
49 * constructor or `parseDataSource'. */
50 private DataSource () {
51 this._type = TYPE_GAUGE;
52 }
54 public String getName() {
55 return _name;
56 }
58 public void setName(String name) {
59 _name = name;
60 }
62 public int getType() {
63 return _type;
64 }
66 public void setType(int type) {
67 _type = type;
68 }
70 public double getMin() {
71 return _min;
72 }
74 public void setMin(double min) {
75 _min = min;
76 }
78 public double getMax() {
79 return _max;
80 }
82 public void setMax(double max) {
83 _max = max;
84 }
86 static double toDouble(String val) {
87 if (val.equals(NAN)) {
88 return Double.NaN;
89 }
90 else {
91 return Double.parseDouble(val);
92 }
93 }
95 private String asString(double val) {
96 if (Double.isNaN(val)) {
97 return NAN;
98 }
99 else {
100 return String.valueOf(val);
101 }
102 }
104 public String toString() {
105 StringBuffer sb = new StringBuffer();
106 final char DLM = ':';
107 sb.append(_name).append(DLM);
108 sb.append(TYPES[_type]).append(DLM);
109 sb.append(asString(_min)).append(DLM);
110 sb.append(asString(_max));
111 return sb.toString();
112 }
114 static public DataSource parseDataSource (String str)
115 {
116 String[] fields;
117 int str_len = str.length ();
118 DataSource dsrc = new DataSource ();
120 /* Ignore trailing commas. This makes it easier for parsing code. */
121 if (str.charAt (str_len - 1) == ',') {
122 str = str.substring (0, str_len - 1);
123 }
125 fields = str.split(":");
126 if (fields.length != 4)
127 return (null);
129 dsrc._name = fields[0];
131 if (fields[1].equals (DataSource.GAUGE)) {
132 dsrc._type = TYPE_GAUGE;
133 }
134 else {
135 dsrc._type = TYPE_COUNTER;
136 }
138 dsrc._min = toDouble (fields[2]);
139 dsrc._max = toDouble (fields[3]);
141 return (dsrc);
142 } /* DataSource parseDataSource */
143 }
145 /* vim: set sw=4 sts=4 et : */