1 /***
2 * ***************************************************************
3 * JADE - Java Agent DEvelopment Framework is a framework to develop
4 * multi-agent systems in compliance with the FIPA specifications.
5 * Copyright (C) 2000 CSELT S.p.A.
6 *
7 * GNU Lesser General Public License
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation,
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 * **************************************************************
24 */
25 package JADE_SL.abs;
26
27 import JADE_SL.onto.*;
28 import JADE_SL.schema.*;
29 import java.io.PrintStream;
30
31 import java.util.Date;
32
33 /***
34 * @author Paola Turci, Federico Bergenti - Universita` di Parma
35 */
36 public class AbsPrimitive implements AbsTerm {
37 private Object value = null;
38 private String typeName = null;
39
40 /***
41 * Construct an Abstract descriptor to hold a primitive of
42 * the proper type (e.g. String, int, boolean...) and set its value.
43 */
44 private AbsPrimitive(String typeName, Object value) {
45 this.typeName = typeName;
46 this.value = value;
47 }
48
49 /***
50 * Construct an Abstract descriptor to hold a primitive of
51 * the proper type (e.g. String, int, boolean...).
52 * @param typeName The name of the type of the primitive held by
53 * this abstract descriptor.
54 */
55 public AbsPrimitive(String typeName) {
56 this(typeName, null);
57 }
58
59 /***
60 * Create an AbsPrimitive of type <code>BasicOntology.STRING</code>
61 * containing a given <code>String</code> value.
62 */
63 public static AbsPrimitive wrap(String value) {
64 AbsPrimitive ret = null;
65 if (value != null) {
66 ret = new AbsPrimitive(BasicOntology.STRING, value);
67 }
68 return ret;
69 }
70
71 /***
72 * Create an AbsPrimitive of type <code>BasicOntology.BOOLEAN</code>
73 * containing a given <code>boolean</code> value.
74 */
75 public static AbsPrimitive wrap(boolean value) {
76 AbsPrimitive ret = new AbsPrimitive(BasicOntology.BOOLEAN, new Boolean(value));
77 return ret;
78 }
79
80 /***
81 * Create an AbsPrimitive of type <code>BasicOntology.INTEGER</code>
82 * containing a given <code>int</code> value.
83 */
84 public static AbsPrimitive wrap(int value) {
85 AbsPrimitive ret = new AbsPrimitive(BasicOntology.INTEGER, new Long((long) value));
86 return ret;
87 }
88
89 /***
90 * Create an AbsPrimitive of type <code>BasicOntology.INTEGER</code>
91 * containing a given <code>long</code> value.
92 */
93 public static AbsPrimitive wrap(long value) {
94 AbsPrimitive ret = new AbsPrimitive(BasicOntology.INTEGER, new Long(value));
95 return ret;
96 }
97
98
99 /***
100 * Create an AbsPrimitive of type <code>BasicOntology.FLOAT</code>
101 * containing a given <code>float</code> value.
102 */
103 public static AbsPrimitive wrap(float value) {
104 AbsPrimitive ret = new AbsPrimitive(BasicOntology.FLOAT, new Double((double) value));
105 return ret;
106 }
107
108 /***
109 * Create an AbsPrimitive of type <code>BasicOntology.FLOAT</code>
110 * containing a given <code>double</code> value.
111 */
112 public static AbsPrimitive wrap(double value) {
113 AbsPrimitive ret = new AbsPrimitive(BasicOntology.FLOAT, new Double(value));
114 return ret;
115 }
116
117
118 /***
119 * Create an AbsPrimitive of type <code>BasicOntology.DATE</code>
120 * containing a given <code>Date</code> value.
121 */
122 public static AbsPrimitive wrap(Date value) {
123 AbsPrimitive ret = null;
124 if (value != null) {
125 ret = new AbsPrimitive(BasicOntology.DATE, value);
126 }
127 return ret;
128 }
129
130 /***
131 * Create an AbsPrimitive of type <code>BasicOntology.BYTE_SEQUENCE</code>
132 * containing a given <code>byte[]</code> value.
133 */
134 public static AbsPrimitive wrap(byte[] value) {
135 AbsPrimitive ret = null;
136 if (value != null) {
137 ret = new AbsPrimitive(BasicOntology.BYTE_SEQUENCE, value);
138 }
139 return ret;
140 }
141
142
143 /***
144 * Set the value of this AbsPrimitive to the given String.
145 * @param value The new value
146 * @throws IllegalArgumentException If the type of this AbsPrimitive
147 * is not <code>BasicOntology.STRING</code>
148 */
149 public void set(String value) {
150 if (!getTypeName().equals(BasicOntology.STRING))
151 throw new IllegalArgumentException("Wrong type");
152 this.value = value;
153 }
154
155 /***
156 * Set the value of this AbsPrimitive to the given boolean value.
157 * @param value The new value
158 * @throws IllegalArgumentException If the type of this AbsPrimitive
159 * is not <code>BasicOntology.BOOLEAN</code>
160 */
161 public void set(boolean value) {
162 if (!getTypeName().equals(BasicOntology.BOOLEAN))
163 throw new IllegalArgumentException("Wrong type");
164 this.value = new Boolean(value);
165 }
166
167 /***
168 * Set the value of this AbsPrimitive to the given int value.
169 * @param value The new value
170 * @throws IllegalArgumentException If the type of this AbsPrimitive
171 * is not <code>BasicOntology.INTEGER</code>
172 */
173 public void set(int value) {
174 if (!getTypeName().equals(BasicOntology.INTEGER))
175 throw new IllegalArgumentException("Wrong type");
176 this.value = new Long((long) value);
177 }
178
179 /***
180 * Set the value of this AbsPrimitive to the given long value.
181 * @param value The new value
182 * @throws IllegalArgumentException If the type of this AbsPrimitive
183 * is not <code>BasicOntology.INTEGER</code>
184 */
185 public void set(long value) {
186 if (!getTypeName().equals(BasicOntology.INTEGER))
187 throw new IllegalArgumentException("Wrong type");
188 this.value = new Long(value);
189 }
190
191
192 /***
193 * Set the value of this AbsPrimitive to the given float value.
194 * @param value The new value
195 * @throws IllegalArgumentException If the type of this AbsPrimitive
196 * is not <code>BasicOntology.FLOAT</code>
197 */
198 public void set(float value) {
199 if (!getTypeName().equals(BasicOntology.FLOAT))
200 throw new IllegalArgumentException("Wrong type");
201 this.value = new Double((double) value);
202 }
203
204 /***
205 * Set the value of this AbsPrimitive to the given double value.
206 * @param value The new value
207 * @throws IllegalArgumentException If the type of this AbsPrimitive
208 * is not <code>BasicOntology.FLOAT</code>
209 */
210 public void set(double value) {
211 if (!getTypeName().equals(BasicOntology.FLOAT))
212 throw new IllegalArgumentException("Wrong type");
213 this.value = new Double(value);
214 }
215
216
217 /***
218 * Set the value of this AbsPrimitive to the given Date value.
219 * @param value The new value
220 * @throws IllegalArgumentException If the type of this AbsPrimitive
221 * is not <code>BasicOntology.DATE</code>
222 */
223 public void set(Date value) {
224 if (!getTypeName().equals(BasicOntology.DATE))
225 throw new IllegalArgumentException("Wrong type");
226 this.value = value;
227 }
228
229 /***
230 * Set the value of this AbsPrimitive to the given byte[] value.
231 * @param value The new value
232 * @throws IllegalArgumentException If the type of this AbsPrimitive
233 * is not <code>BasicOntology.BYTE_SEQUENCE</code>
234 */
235 public void set(byte[] value) {
236 if (!getTypeName().equals(BasicOntology.BYTE_SEQUENCE))
237 throw new IllegalArgumentException("Wrong type");
238 this.value = value;
239 }
240
241 /***
242 * @return the value of this AbsPrimitive as a String.
243 * @throws ClassCastException If the type of this AbsPrimitive
244 * is not <code>BasicOntology.STRING</code>
245 */
246 public String getString() {
247 return (String) value;
248 }
249
250 /***
251 * @return the value of this AbsPrimitive as a boolean.
252 * @throws ClassCastException If the type of this AbsPrimitive
253 * is not <code>BasicOntology.BOOLEAN</code>
254 */
255 public boolean getBoolean() {
256 return ((Boolean) value).booleanValue();
257 }
258
259 /***
260 * @return the value of this AbsPrimitive as an int.
261 * @throws ClassCastException If the type of this AbsPrimitive
262 * is not <code>BasicOntology.INTEGER</code>
263 */
264 public int getInteger() {
265 return (int) ((Long) value).longValue();
266 }
267
268 /***
269 * @return the value of this AbsPrimitive as a long.
270 * @throws ClassCastException If the type of this AbsPrimitive
271 * is not <code>BasicOntology.INTEGER</code>
272 */
273 public long getLong() {
274 return ((Long) value).longValue();
275 }
276
277
278 /***
279 * @return the value of this AbsPrimitive as a float.
280 * @throws ClassCastException If the type of this AbsPrimitive
281 * is not <code>BasicOntology.FLOAT</code>
282 */
283 public float getFloat() {
284 return (float) ((Double) value).doubleValue();
285 }
286
287 /***
288 * @return the value of this AbsPrimitive as a double.
289 * @throws ClassCastException If the type of this AbsPrimitive
290 * is not <code>BasicOntology.FLOAT</code>
291 */
292 public double getDouble() {
293 return ((Double) value).doubleValue();
294 }
295
296
297 /***
298 * @return the value of this AbsPrimitive as a Date.
299 * @throws ClassCastException If the type of this AbsPrimitive
300 * is not <code>BasicOntology.DATE</code>
301 */
302 public Date getDate() {
303 return (Date) value;
304 }
305
306 /***
307 * @return the value of this AbsPrimitive as a byte[].
308 * @throws ClassCastException If the type of this AbsPrimitive
309 * is not <code>BasicOntology.BYTE_SEQUENCE</code>
310 */
311 public byte[] getByteSequence() {
312 return (byte[]) value;
313 }
314
315 /***
316 * @return the value of this AbsPrimitive as an Object.
317 * If the type of this AbsPrimitive is <code>BasicOntology.BOOLEAN
318 * BasicOntology.INTEGER or BasicOntology.FLOAT</code> a
319 * <code>Boolean, Integer or Float</code> object is returned.
320 */
321 public Object getObject() {
322 return value;
323 }
324
325 protected void dump(int indent, PrintStream ps) {
326 for (int i = 0; i < indent; i++) {
327 ps.print(" ");
328 }
329 ps.println(getClass());
330 ps.println(getObject());
331 }
332
333 /***
334 * @return The name of the type of the object held by this
335 * abstract descriptor.
336 * @see AbsObject#getTypeName()
337 */
338 public String getTypeName() {
339 return typeName;
340 }
341
342 /***
343 Makes no sense in the case of an AbsPrimitive that has no attribute
344 --> Just return null
345 */
346 public AbsObject getAbsObject(String name) {
347 return null;
348 }
349
350 /***
351 Makes no sense in the case of an AbsPrimitive that has no attribute
352 --> Just return null
353 */
354 public String[] getNames() {
355 return null;
356 }
357
358 /***
359 * Tests if this AbsPrimitive is grounded. It always returns true
360 */
361 public boolean isGrounded() {
362 return true;
363 }
364
365 /***
366 Makes no sense in the case of an AbsAggregate that has no attribute
367 --> Just return 0
368 */
369 public int getCount() {
370 return 0;
371 }
372
373 public void dump() {
374 dump(0, System.out);
375 }
376
377 public String toString() {
378 return value.toString();
379 }
380 }
381