1 /**
2 * collectd - bindings/java/org/collectd/api/DataSet.java
3 * Copyright (C) 2009 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 package org.collectd.api;
29 import java.util.List;
30 import java.util.ArrayList;
32 /**
33 * Java representation of collectd/src/plugin.h:data_set_t structure.
34 *
35 * @author Florian Forster <octo at collectd.org>
36 */
37 public class DataSet
38 {
39 private String _type;
40 private List<DataSource> _ds;
42 private DataSet ()
43 {
44 this._type = null;
45 this._ds = new ArrayList<DataSource> ();
46 }
48 public DataSet (String type)
49 {
50 this._type = type;
51 this._ds = new ArrayList<DataSource> ();
52 }
54 public DataSet (String type, DataSource dsrc)
55 {
56 this._type = type;
57 this._ds = new ArrayList<DataSource> ();
58 this._ds.add (dsrc);
59 }
61 public DataSet (String type, List<DataSource> ds)
62 {
63 this._type = type;
64 this._ds = ds;
65 }
67 public void setType (String type)
68 {
69 this._type = type;
70 }
72 public String getType ()
73 {
74 return (this._type);
75 }
77 public void addDataSource (DataSource dsrc)
78 {
79 this._ds.add (dsrc);
80 }
82 public List<DataSource> getDataSources ()
83 {
84 return (this._ds);
85 }
87 public String toString ()
88 {
89 StringBuffer sb = new StringBuffer ();
90 int i;
92 sb.append (this._type);
93 for (i = 0; i < this._ds.size (); i++)
94 {
95 if (i == 0)
96 sb.append ("\t");
97 else
98 sb.append (", ");
99 sb.append (this._ds.get (i).toString ());
100 }
102 return (sb.toString ());
103 }
105 static public DataSet parseDataSet (String str)
106 {
107 DataSet ds = new DataSet ();
108 String[] fields;
109 int i;
111 str = str.trim();
112 if (str.length() == 0) {
113 return (null);
114 }
115 if (str.charAt(0) == '#') {
116 return (null);
117 }
119 fields = str.split ("\\s+");
120 if (fields.length < 2)
121 return (null);
123 ds._type = fields[0];
125 for (i = 1; i < fields.length; i++) {
126 DataSource dsrc;
128 dsrc = DataSource.parseDataSource (fields[i]);
129 if (dsrc == null)
130 break;
132 ds._ds.add (dsrc);
133 }
135 if (i < fields.length)
136 return (null);
138 return (ds);
139 } /* DataSet parseDataSet */
140 } /* class DataSet */
142 /* vim: set sw=4 sts=4 et : */