xref: /illumos-gate/usr/src/cmd/pools/poold/com/sun/solaris/service/pools/Value.java (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  *ident	"%Z%%M%	%I%	%E% SMI"
27  *
28  */
29 
30 package com.sun.solaris.service.pools;
31 
32 /**
33  * The <code>Value</code> class represents a pools value.
34  */
35 public class Value {
36 
37 	private long _this;
38 
39 	/**
40 	 * Constructor. Only for use from native code.
41 	 * @param pointer A pointer to a C value.
42 	 */
43 	private Value(long pointer)
44 	{
45 		_this = pointer;
46 	}
47 
48 	/**
49 	 * Constructor
50 	 * @param name The name of the value.
51 	 * @throws PoolsException If there is an error whilst
52 	 * allocating the value.
53 	 */
54 	public Value(String name) throws PoolsException
55 	{
56 		if ((_this = PoolInternal.pool_value_alloc()) == 0)
57 			throw new PoolsException();
58 		setName(name);
59 	}
60 
61 	/**
62 	 * Constructor
63 	 * @param name The name of the value.
64 	 * @param value The value of the value.
65 	 * @throws PoolsException If there is an error whilst
66 	 * allocating the value.
67 	 */
68 	public Value(String name, long value) throws PoolsException
69 	{
70 		this(name);
71 		setValue(value);
72 	}
73 
74 	/**
75 	 * Constructor
76 	 * @param name The name of the value.
77 	 * @param value The value of the value.
78 	 * @param s Indicates if the value is signed or not.
79 	 * @throws PoolsException If there is an error whilst
80 	 * allocating the value.
81 	 */
82 	public Value(String name, long value, boolean s) throws PoolsException
83 	{
84 		this(name);
85 		setValue(value, s);
86 	}
87 
88 	/**
89 	 * Constructor
90 	 * @param name The name of the value.
91 	 * @param value The value of the value.
92 	 * @throws PoolsException If there is an error whilst
93 	 * allocating the value.
94 	 */
95 	public Value(String name, String value) throws PoolsException
96 	{
97 		this(name);
98 		setValue(value);
99 	}
100 
101 	/**
102 	 * Constructor
103 	 * @param name The name of the value.
104 	 * @param value The value of the value.
105 	 * @throws PoolsException If there is an error whilst
106 	 * allocating the value.
107 	 */
108 	public Value(String name, boolean value) throws PoolsException
109 	{
110 		this(name);
111 		setValue(value);
112 	}
113 
114 	/**
115 	 * Constructor
116 	 * @param name The name of the value.
117 	 * @param value The value of the value.
118 	 * @throws PoolsException If there is an error whilst
119 	 * allocating the value.
120 	 */
121 	public Value(String name, double value) throws PoolsException
122 	{
123 		this(name);
124 		setValue(value);
125 	}
126 
127         /**
128          * Explicitly reclaim the memory allocated for this value by
129          * the C proxy.
130          */
131 	public void close()
132 	{
133 		if (_this != 0) {
134 			PoolInternal.pool_value_free(_this);
135 			_this = 0;
136 		}
137 	}
138 
139         /**
140          * Reclaim the memory allocated for this value by the C
141 	 * proxy.
142          *
143          * @throws Throwable If freeing this configuration fails.
144          */
145 	protected void finalize() throws Throwable
146 	{
147 		try
148 		{
149 			close();
150 		}
151 		finally
152 		{
153 			super.finalize();
154 		}
155 	}
156 
157 	/**
158 	 * Name this value.
159 	 *
160 	 * @param name The name to set for this value.
161 	 */
162 	public void setName(String name)
163 	{
164 		PoolInternal.pool_value_set_name(_this, name);
165 	}
166 
167 	/**
168 	 * Set this value to take the supplied signed long value.
169 	 *
170 	 * @param value The value to which this value should be set.
171 	 */
172 	public void setValue(long value)
173 	{
174 		PoolInternal.pool_value_set_int64(_this, value);
175 	}
176 
177 	/**
178 	 * Set this value to take the supplied long value.
179 	 *
180 	 * @param value The value to which this value should be set.
181 	 * @param s Is the value signed or unsigned.
182 	 */
183 	public void setValue(long value, boolean s)
184 	{
185 		if (s)
186 			setValue(value);
187 		PoolInternal.pool_value_set_uint64(_this, value);
188 	}
189 
190 	/**
191 	 * Set this value to take the supplied string value.
192 	 *
193 	 * @param value The value to which this value should be set.
194 	 * @throws PoolsExecption If the setting of the value fails.
195 	 */
196 	public void setValue(String value) throws PoolsException
197 	{
198 		if (PoolInternal.pool_value_set_string(_this, value) !=
199 		    PoolInternal.PO_SUCCESS)
200 			throw new PoolsException();
201 	}
202 
203 	/**
204 	 * Set this value to take the supplied boolean value.
205 	 *
206 	 * @param value The value to which this value should be set.
207 	 */
208 	public void setValue(boolean value)
209 	{
210 		if (value == true)
211 			PoolInternal.pool_value_set_bool(_this, (short)1);
212 		else
213 			PoolInternal.pool_value_set_bool(_this, (short)0);
214 	}
215 
216 	/**
217 	 * Set this value to take the supplied double value.
218 	 *
219 	 * @param value The value to which this value should be set.
220 	 */
221 	public void setValue(double value)
222 	{
223 		PoolInternal.pool_value_set_double(_this, value);
224 	}
225 
226 	/**
227 	 * Returns the name of the value.
228 	 *
229 	 * @return the name of the value.
230 	 */
231 	public String getName()
232 	{
233 		return (PoolInternal.pool_value_get_name(_this));
234 	}
235 
236 	/**
237 	 * Returns the pointer to the native value represented by this
238 	 * object.
239 	 *
240 	 * @return the pointer to the native value represented by this
241 	 * object.
242 	 */
243 	public long getValue()
244 	{
245 		return (_this);
246 	}
247 
248 	/**
249 	 * Returns the type of this object.
250 	 *
251 	 * @return the type of this object.
252 	 */
253 	public int getType()
254 	{
255 		return (PoolInternal.pool_value_get_type(_this));
256 	}
257 
258 	/**
259          * Returns a string representation of this value.
260          *
261          * @return a string representation of this value.
262          */
263 	public String toString()
264 	{
265 		int type = PoolInternal.pool_value_get_type(_this);
266 
267 		try {
268 			if (type == PoolInternal.POC_INT ||
269 			    type == PoolInternal.POC_UINT)
270 				return (String.valueOf(getLong()));
271 			if (type == PoolInternal.POC_STRING)
272 				return getString();
273 			if (type == PoolInternal.POC_BOOL)
274 				return (String.valueOf(getBool()));
275 			if (type == PoolInternal.POC_DOUBLE)
276 				return (String.valueOf(getDouble()));
277 		}
278 		catch (PoolsException pe) {
279 			return pe.toString();
280 		}
281 		return "";	/* Stop the compiler complaining */
282 	}
283 
284         /**
285          * Returns the value as a UnsignedInt64.
286          *
287          * @return the value as a UnsignedInt64.
288          * @throws PoolsException if the value is not an
289          * UnsignedInt64.
290          */
291 	public final UnsignedInt64 getUnsignedInt64() throws PoolsException
292 	{
293 		return (getUnsignedInt64Value(_this));
294 	}
295 
296         /**
297          * Returns the value as a long.
298          *
299          * @return the value as a long.
300          * @throws PoolsException if the value is not a long.
301          */
302 	public final long getLong() throws PoolsException
303 	{
304 		return (getLongValue(_this));
305 	}
306 
307         /**
308          * Returns the value as a String.
309          *
310          * @return the value as a String.
311          * @throws PoolsException if the value is not a String.
312          */
313 	public final String getString() throws PoolsException
314 	{
315 		return (getStringValue(_this));
316 	}
317 
318         /**
319          * Returns the value as a boolean.
320          *
321          * @return the value as a boolean.
322          * @throws PoolsException if the value is not a boolean.
323          */
324 	public final boolean getBool() throws PoolsException
325 	{
326 		return (getBoolValue(_this));
327 	}
328 
329         /**
330          * Returns the value as a double.
331          *
332          * @return the value as a double.
333          * @throws PoolsException if the value is not a double.
334          */
335 	public final double getDouble() throws PoolsException
336 	{
337 		return (getDoubleValue(_this));
338 	}
339 
340         /**
341          * Returns the value as a UnsignedInt64.
342          *
343          * @param pointer the native value to be accessed.
344          * @return the value as a UnsignedInt64.
345          */
346 	private final static native UnsignedInt64 getUnsignedInt64Value(
347 	    long pointer);
348 
349         /**
350          * Returns the value as a long.
351          *
352          * @param pointer the native value to be accessed.
353          * @return the value as a long.
354          */
355 	private final static native long getLongValue(long pointer);
356 
357         /**
358          * Returns the value as a String.
359          *
360          * @param pointer the native value to be accessed.
361          * @return the value as a String.
362          */
363 	private final static native String getStringValue(long pointer);
364 
365         /**
366          * Returns the value as a boolean.
367          *
368          * @param pointer the native value to be accessed.
369          * @return the value as a boolean.
370          */
371 	private final static native boolean getBoolValue(long pointer);
372 
373         /**
374          * Returns the value as a double.
375          *
376          * @param pointer the native value to be accessed.
377          * @return the value as a double.
378          */
379 	private final static native double getDoubleValue(long pointer);
380 }
381