1 /*
2 * @(#)MapMessageImpl.java
3 *
4 * JFoxMQ the open source JMS MOM.
5 *
6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software
7 * is licensed under LGPL license.
8 *
9 * For more information, please visit: http://www.huihoo.org
10 */
11
12 package org.huihoo.jfox.ms.jms.message;
13
14 import java.io.Serializable;
15 import java.util.Collections;
16 import java.util.Enumeration;
17 import java.util.HashMap;
18
19 import javax.jms.JMSException;
20 import javax.jms.MapMessage;
21 import javax.jms.MessageFormatException;
22 import javax.jms.MessageNotWriteableException;
23
24 /***
25 * <p>
26 * Description: A <CODE>MapMessage</CODE> object is used to send a set of
27 * name-value pairs. The names are <CODE>String</CODE> objects, and the
28 * values are primitive data types in the Java programming language. The
29 * entries can be accessed sequentially or randomly by name. The order of the
30 * entries is undefined. <CODE>MapMessage</CODE> inherits from the <CODE>
31 * Message</CODE> interface and adds a message body that contains a Map.
32 * </p>
33 *
34 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
35 * @version Revision: 1.1 Date: 2002-12-08 12:50:10
36 */
37
38 public class MapMessageImpl
39 extends MessageImpl
40 implements MapMessage, Serializable {
41
42 private HashMap map = new HashMap();
43
44 /***
45 * Default constructor.
46 */
47 public MapMessageImpl() {
48 super();
49 }
50
51 /***
52 * Returns the <CODE>boolean</CODE> value with the specified name.
53 *
54 * @param name
55 * the name of the <CODE>boolean</CODE>
56 *
57 * @return the <CODE>boolean</CODE> value with the specified name
58 *
59 * @exception JMSException
60 * if the JMS provider fails to read the message due to some
61 * internal error.
62 * @exception MessageFormatException
63 * if this type conversion is invalid.
64 *
65 * @see javax.jms.MapMessage#getBoolean(String)
66 */
67 public boolean getBoolean(String name) throws JMSException {
68 return getBoolean(map.get(name));
69 }
70
71 /***
72 * Returns the <CODE>byte</CODE> value with the specified name.
73 *
74 * @param name
75 * the name of the <CODE>byte</CODE>
76 *
77 * @return the <CODE>byte</CODE> value with the specified name
78 *
79 * @exception JMSException
80 * if the JMS provider fails to read the message due to some
81 * internal error.
82 * @exception MessageFormatException
83 * if this type conversion is invalid.
84 *
85 * @see javax.jms.MapMessage#getByte(String)
86 */
87 public byte getByte(String name) throws JMSException {
88 return getByte(map.get(name));
89 }
90
91 /***
92 * Returns the <CODE>short</CODE> value with the specified name.
93 *
94 * @param name
95 * the name of the <CODE>short</CODE>
96 *
97 * @return the <CODE>short</CODE> value with the specified name
98 *
99 * @exception JMSException
100 * if the JMS provider fails to read the message due to some
101 * internal error.
102 * @exception MessageFormatException
103 * if this type conversion is invalid.
104 *
105 * @see javax.jms.MapMessage#getShort(String)
106 */
107 public short getShort(String name) throws JMSException {
108 return getShort(map.get(name));
109 }
110
111 /***
112 * Returns the Unicode character value with the specified name.
113 *
114 * @param name
115 * the name of the Unicode character
116 *
117 * @return the Unicode character value with the specified name
118 *
119 * @exception JMSException
120 * if the JMS provider fails to read the message due to some
121 * internal error.
122 * @exception MessageFormatException
123 * if this type conversion is invalid.
124 *
125 * @see javax.jms.MapMessage#getChar(String)
126 */
127 public char getChar(String name) throws JMSException {
128 return getChar(map.get(name));
129 }
130
131 /***
132 * Returns the <CODE>int</CODE> value with the specified name.
133 *
134 * @param name
135 * the name of the <CODE>int</CODE>
136 *
137 * @return the <CODE>int</CODE> value with the specified name
138 *
139 * @exception JMSException
140 * if the JMS provider fails to read the message due to some
141 * internal error.
142 * @exception MessageFormatException
143 * if this type conversion is invalid.
144 *
145 * @see javax.jms.MapMessage#getInt(String)
146 */
147 public int getInt(String name) throws JMSException {
148 return getInt(map.get(name));
149 }
150
151 /***
152 * Returns the <CODE>long</CODE> value with the specified name.
153 *
154 * @param name
155 * the name of the <CODE>long</CODE>
156 *
157 * @return the <CODE>long</CODE> value with the specified name
158 *
159 * @exception JMSException
160 * if the JMS provider fails to read the message due to some
161 * internal error.
162 * @exception MessageFormatException
163 * if this type conversion is invalid.
164 *
165 * @see javax.jms.MapMessage#getLong(String)
166 */
167 public long getLong(String name) throws JMSException {
168 return getLong(map.get(name));
169 }
170
171 /***
172 * Returns the <CODE>float</CODE> value with the specified name.
173 *
174 * @param name
175 * the name of the <CODE>float</CODE>
176 *
177 * @return the <CODE>float</CODE> value with the specified name
178 *
179 * @exception JMSException
180 * if the JMS provider fails to read the message due to some
181 * internal error.
182 * @exception MessageFormatException
183 * if this type conversion is invalid.
184 *
185 * @see javax.jms.MapMessage#getFloat(String)
186 */
187 public float getFloat(String name) throws JMSException {
188 return getFloat(map.get(name));
189 }
190
191 /***
192 * Returns the <CODE>double</CODE> value with the specified name.
193 *
194 * @param name
195 * the name of the <CODE>double</CODE>
196 *
197 * @return the <CODE>double</CODE> value with the specified name
198 *
199 * @exception JMSException
200 * if the JMS provider fails to read the message due to some
201 * internal error.
202 * @exception MessageFormatException
203 * if this type conversion is invalid.
204 *
205 * @see javax.jms.MapMessage#getDouble(String)
206 */
207 public double getDouble(String name) throws JMSException {
208 return getDouble(map.get(name));
209 }
210
211 /***
212 * Returns the <CODE>String</CODE> value with the specified name.
213 *
214 * @param name
215 * the name of the <CODE>String</CODE>
216 *
217 * @return the <CODE>String</CODE> value with the specified name; if
218 * there is no item by this name, a null value is returned
219 *
220 * @exception JMSException
221 * if the JMS provider fails to read the message due to some
222 * internal error.
223 * @exception MessageFormatException
224 * if this type conversion is invalid.
225 *
226 * @see javax.jms.MapMessage#getString(String)
227 */
228 public String getString(String name) throws JMSException {
229 return getString(map.get(name));
230 }
231
232 /***
233 * Returns the byte array value with the specified name.
234 *
235 * @param name
236 * the name of the byte array
237 *
238 * @return a copy of the byte array value with the specified name; if there
239 * is no item by this name, a null value is returned.
240 *
241 * @exception JMSException
242 * if the JMS provider fails to read the message due to some
243 * internal error.
244 * @exception MessageFormatException
245 * if this type conversion is invalid.
246 *
247 * @see javax.jms.MapMessage#getBytes(String)
248 */
249 public byte[] getBytes(String name) throws JMSException {
250 return getBytes(map.get(name));
251 }
252
253 /***
254 * Returns the value of the object with the specified name.
255 *
256 * <P>
257 * This method can be used to return, in objectified format, an object in
258 * the Java programming language ("Java object") that had been stored in
259 * the Map with the equivalent <CODE>setObject</CODE> method call, or its
260 * equivalent primitive <CODE>set <I>type</I></CODE> method.
261 *
262 * <P>
263 * Note that byte values are returned as <CODE>byte[]</CODE>, not <CODE>
264 * Byte[]</CODE>.
265 *
266 * @param name
267 * the name of the Java object
268 *
269 * @return a copy of the Java object value with the specified name, in
270 * objectified format (for example, if the object was set as an
271 * <CODE>int</CODE>, an <CODE>Integer</CODE> is returned); if
272 * there is no item by this name, a null value is returned
273 *
274 * @exception JMSException
275 * if the JMS provider fails to read the message due to some
276 * internal error.
277 *
278 * @see javax.jms.MapMessage#getObject(String)
279 */
280 public Object getObject(String name) throws JMSException {
281 return map.get(name);
282 }
283
284 /***
285 * Returns an <CODE>Enumeration</CODE> of all the names in the <CODE>
286 * MapMessage</CODE> object.
287 *
288 * @return an enumeration of all the names in this <CODE>MapMessage
289 * </CODE>
290 *
291 * @exception JMSException
292 * if the JMS provider fails to read the message due to some
293 * internal error.
294 *
295 * @see javax.jms.MapMessage#getMapNames()
296 */
297 public Enumeration getMapNames() throws JMSException {
298 return Collections.enumeration(map.keySet());
299 }
300
301 /***
302 * Sets a <CODE>boolean</CODE> value with the specified name into the
303 * Map.
304 *
305 * @param name
306 * the name of the <CODE>boolean</CODE>
307 * @param value
308 * the <CODE>boolean</CODE> value to set in the Map
309 *
310 * @exception JMSException
311 * if the JMS provider fails to write the message due to
312 * some internal error.
313 * @exception MessageNotWriteableException
314 * if the message is in read-only mode.
315 *
316 * @see javax.jms.MapMessage#setBoolean(String, boolean)
317 */
318 public void setBoolean(String name, boolean value) throws JMSException {
319 if (isBodyModifiable()) {
320 try {
321 map.put(name, new Boolean(value));
322 } catch (NullPointerException e) {
323 throw new JMSException(e.getMessage());
324 }
325 } else {
326 throw new MessageNotWriteableException("MapMesage read_only");
327 }
328 }
329
330 /***
331 * Sets a <CODE>byte</CODE> value with the specified name into the Map.
332 *
333 * @param name
334 * the name of the <CODE>byte</CODE>
335 * @param value
336 * the <CODE>byte</CODE> value to set in the Map
337 *
338 * @exception JMSException
339 * if the JMS provider fails to write the message due to
340 * some internal error.
341 * @exception MessageNotWriteableException
342 * if the message is in read-only mode.
343 *
344 * @see javax.jms.MapMessage#setByte(String, byte)
345 */
346 public void setByte(String name, byte value) throws JMSException {
347 if (isBodyModifiable()) {
348 try {
349 map.put(name, new Byte(value));
350 } catch (NullPointerException e) {
351 throw new JMSException(e.getMessage());
352 }
353 } else {
354 throw new MessageNotWriteableException("MapMessage read_only");
355 }
356 }
357
358 /***
359 * Sets a <CODE>short</CODE> value with the specified name into the Map.
360 *
361 * @param name
362 * the name of the <CODE>short</CODE>
363 * @param value
364 * the <CODE>short</CODE> value to set in the Map
365 *
366 * @exception JMSException
367 * if the JMS provider fails to write the message due to
368 * some internal error.
369 * @exception MessageNotWriteableException
370 * if the message is in read-only mode.
371 *
372 * @see javax.jms.MapMessage#setShort(String, short)
373 */
374 public void setShort(String name, short value) throws JMSException {
375 if (isBodyModifiable()) {
376 try {
377 map.put(name, new Short(value));
378 } catch (NullPointerException e) {
379 throw new JMSException(e.getMessage());
380 }
381 } else {
382 throw new MessageNotWriteableException("MapMessage read_only");
383 }
384 }
385
386 /***
387 * Sets a Unicode character value with the specified name into the Map.
388 *
389 * @param name
390 * the name of the Unicode character
391 * @param value
392 * the Unicode character value to set in the Map
393 *
394 * @exception JMSException
395 * if the JMS provider fails to write the message due to
396 * some internal error.
397 * @exception MessageNotWriteableException
398 * if the message is in read-only mode.
399 *
400 * @see javax.jms.MapMessage#setChar(String, char)
401 */
402 public void setChar(String name, char value) throws JMSException {
403 if (isBodyModifiable()) {
404 try {
405 map.put(name, new Character(value));
406 } catch (NullPointerException e) {
407 throw new JMSException(e.getMessage());
408 }
409 } else {
410 throw new MessageNotWriteableException("MapMessage read_only");
411 }
412 }
413
414 /***
415 * Sets an <CODE>int</CODE> value with the specified name into the Map.
416 *
417 * @param name
418 * the name of the <CODE>int</CODE>
419 * @param value
420 * the <CODE>int</CODE> value to set in the Map
421 *
422 * @exception JMSException
423 * if the JMS provider fails to write the message due to
424 * some internal error.
425 * @exception MessageNotWriteableException
426 * if the message is in read-only mode.
427 *
428 * @see javax.jms.MapMessage#setInt(String, int)
429 */
430 public void setInt(String name, int value) throws JMSException {
431 if (isBodyModifiable()) {
432 try {
433 map.put(name, new Integer(value));
434 } catch (NullPointerException e) {
435 throw new JMSException(e.getMessage());
436 }
437 } else {
438 throw new MessageNotWriteableException("MapMessage read_only");
439 }
440 }
441
442 /***
443 * Sets a <CODE>long</CODE> value with the specified name into the Map.
444 *
445 * @param name
446 * the name of the <CODE>long</CODE>
447 * @param value
448 * the <CODE>long</CODE> value to set in the Map
449 *
450 * @exception JMSException
451 * if the JMS provider fails to write the message due to
452 * some internal error.
453 * @exception MessageNotWriteableException
454 * if the message is in read-only mode.
455 *
456 * @see javax.jms.MapMessage#setLong(String, long)
457 */
458 public void setLong(String name, long value) throws JMSException {
459 if (isBodyModifiable()) {
460 try {
461 map.put(name, new Long(value));
462 } catch (NullPointerException e) {
463 throw new JMSException(e.getMessage());
464 }
465 } else {
466 throw new MessageNotWriteableException("MapMessage read_only");
467 }
468 }
469
470 /***
471 * Sets a <CODE>float</CODE> value with the specified name into the Map.
472 *
473 * @param name
474 * the name of the <CODE>float</CODE>
475 * @param value
476 * the <CODE>float</CODE> value to set in the Map
477 *
478 * @exception JMSException
479 * if the JMS provider fails to write the message due to
480 * some internal error.
481 * @exception MessageNotWriteableException
482 * if the message is in read-only mode.
483 *
484 * @see javax.jms.MapMessage#setFloat(String, float)
485 */
486 public void setFloat(String name, float value) throws JMSException {
487 if (isBodyModifiable()) {
488 try {
489 map.put(name, new Float(value));
490 } catch (NullPointerException e) {
491 throw new JMSException(e.getMessage());
492 }
493 } else {
494 throw new MessageNotWriteableException("MapMessage read_only");
495 }
496 }
497
498 /***
499 * Sets a <CODE>double</CODE> value with the specified name into the Map.
500 *
501 * @param name
502 * the name of the <CODE>double</CODE>
503 * @param value
504 * the <CODE>double</CODE> value to set in the Map
505 *
506 * @exception JMSException
507 * if the JMS provider fails to write the message due to
508 * some internal error.
509 * @exception MessageNotWriteableException
510 * if the message is in read-only mode.
511 *
512 * @see javax.jms.MapMessage#setDouble(String, double)
513 */
514 public void setDouble(String name, double value) throws JMSException {
515 if (isBodyModifiable()) {
516 try {
517 map.put(name, new Double(value));
518 } catch (NullPointerException e) {
519 throw new JMSException(e.getMessage());
520 }
521 } else {
522 throw new MessageNotWriteableException("MapMessage read_only");
523 }
524 }
525
526 /***
527 * Sets a <CODE>String</CODE> value with the specified name into the Map.
528 *
529 * @param name
530 * the name of the <CODE>String</CODE>
531 * @param value
532 * the <CODE>String</CODE> value to set in the Map
533 *
534 * @exception JMSException
535 * if the JMS provider fails to write the message due to
536 * some internal error.
537 * @exception MessageNotWriteableException
538 * if the message is in read-only mode.
539 *
540 * @see javax.jms.MapMessage#setString(String, String)
541 */
542 public void setString(String name, String value) throws JMSException {
543 if (isBodyModifiable()) {
544 try {
545 map.put(name, value);
546 } catch (NullPointerException e) {
547 throw new JMSException(e.getMessage());
548 }
549 } else {
550 throw new MessageNotWriteableException("MapMessage read_only");
551 }
552 }
553
554 /***
555 * Sets a byte array value with the specified name into the Map.
556 *
557 * @param name
558 * the name of the byte array
559 * @param value
560 * the byte array value to set in the Map; the array is copied
561 * so that the value for <CODE>name</CODE> will not be altered
562 * by future modifications
563 *
564 * @exception JMSException
565 * if the JMS provider fails to write the message due to
566 * some internal error.
567 * @exception MessageNotWriteableException
568 * if the message is in read-only mode.
569 *
570 * @see javax.jms.MapMessage#setBytes(String, byte[])
571 */
572 public void setBytes(String name, byte[] value) throws JMSException {
573 if (isBodyModifiable()) {
574 try {
575 int len = value.length;
576 byte[] valueCopy = new byte[len];
577 System.arraycopy(value, 0, valueCopy, 0, len);
578 map.put(name, valueCopy);
579 } catch (NullPointerException e) {
580 throw new JMSException(e.getMessage());
581 }
582 } else {
583 throw new MessageNotWriteableException("MapMmessage read_only");
584 }
585
586 }
587
588 /***
589 * Sets a portion of the byte array value with the specified name into the
590 * Map.
591 *
592 * @param name
593 * the name of the byte array
594 * @param value
595 * the byte array value to set in the Map
596 * @param offset
597 * the initial offset within the byte array
598 * @param length
599 * the number of bytes to use
600 *
601 * @exception JMSException
602 * if the JMS provider fails to write the message due to
603 * some internal error.
604 * @exception MessageNotWriteableException
605 * if the message is in read-only mode.
606 *
607 * @see javax.jms.MapMessage#setBytes(String, byte[], int, int)
608 */
609 public void setBytes(String name, byte[] value, int offset, int length)
610 throws JMSException {
611 if (isBodyModifiable()) {
612 try {
613 byte[] newValue = new byte[length];
614 System.arraycopy(value, offset, newValue, 0, length);
615 map.put(name, newValue);
616 } catch (NullPointerException e) {
617 throw new JMSException(e.getMessage());
618 }
619 } else {
620 throw new MessageNotWriteableException("MapMessageimpl read_only");
621 }
622 }
623
624 /***
625 * Sets an object value with the specified name into the Map.
626 *
627 * <P>
628 * This method works only for the objectified primitive object types (
629 * <code>Integer</code>,<code>Double</code>,<code>Long</code>
630 * ...), <code>String</code> objects, and byte arrays.
631 *
632 * @param name
633 * the name of the Java object
634 * @param value
635 * the Java object value to set in the Map
636 *
637 * @exception JMSException
638 * if the JMS provider fails to write the message due to
639 * some internal error.
640 * @exception MessageFormatException
641 * if the object is invalid.
642 * @exception MessageNotWriteableException
643 * if the message is in read-only mode.
644 *
645 * @see javax.jms.MapMessage#setObject(String, Object)
646 */
647 public void setObject(String name, Object value) throws JMSException {
648 if (isBodyModifiable()) {
649 try {
650 if ((value instanceof Boolean)
651 || (value instanceof Byte)
652 || (value instanceof Short)
653 || (value instanceof Character)
654 || (value instanceof Integer)
655 || (value instanceof Long)
656 || (value instanceof Float)
657 || (value instanceof Double)
658 || (value instanceof String)
659 || (value instanceof byte[])) {
660 map.put(name, value);
661 } else {
662 throw new MessageFormatException("MapMessagei invalid_type");
663 }
664 } catch (NullPointerException e) {
665 throw new JMSException(e.getMessage());
666 }
667 } else {
668 throw new MessageNotWriteableException("MapMessage read_only");
669 }
670 }
671
672 /***
673 * Indicates whether an item exists in this <CODE>MapMessage</CODE>
674 * object.
675 *
676 * @param name
677 * the name of the item to test
678 *
679 * @return true if the item exists
680 *
681 * @exception JMSException
682 * if the JMS provider fails to determine if the item exists
683 * due to some internal error.
684 *
685 * @see javax.jms.MapMessage#itemExists(String)
686 */
687 public boolean itemExists(String name) throws JMSException {
688 return map.containsKey(name);
689 }
690
691 /***
692 * Clear out the message body.
693 *
694 * @throws JMSException
695 */
696 public void clearBody() throws JMSException {
697 super.clearBody();
698 map = new HashMap();
699 }
700
701 /***
702 * Convert value to boolean
703 *
704 * @param value
705 * @return the converted boolean
706 * @exception MessageFormatException
707 * if the conversion is invalid
708 */
709 private boolean getBoolean(Object value) throws MessageFormatException {
710 if (value instanceof Boolean) {
711 return ((Boolean)value).booleanValue();
712 } else if (value instanceof String) {
713 return Boolean.valueOf((String)value).booleanValue();
714 } else if (value == null) {
715 return Boolean.valueOf((String)value).booleanValue();
716 } else {
717 throw new MessageFormatException(
718 "Can't convert value of type "
719 + value.getClass().getName()
720 + " to Boolean");
721 }
722 }
723
724 /***
725 * Convert value to byte
726 *
727 * @param value
728 * @return the converted byte
729 * @exception MessageFormatException
730 * if the conversion is invalid
731 */
732 private byte getByte(Object value) throws MessageFormatException {
733 if (value instanceof Byte) {
734 return ((Byte)value).byteValue();
735 } else if (value instanceof String) {
736 return Byte.parseByte((String)value);
737 } else if (value == null) {
738 return Byte.valueOf((String)value).byteValue();
739 } else {
740 throw new MessageFormatException(
741 "Can't convert value of type "
742 + value.getClass().getName()
743 + " to Byte");
744 }
745 }
746
747 /***
748 * Convert value to short
749 *
750 * @param value
751 * @return the converted short
752 * @exception MessageFormatException
753 * if the conversion is invalid
754 */
755 private short getShort(Object value) throws MessageFormatException {
756 if (value instanceof Short) {
757 return ((Short)value).shortValue();
758 } else if (value instanceof Byte) {
759 return ((Byte)value).shortValue();
760 } else if (value instanceof String) {
761 return Short.parseShort((String)value);
762 } else if (value == null) {
763 return Short.valueOf((String)value).shortValue();
764 } else {
765 throw new MessageFormatException(
766 "Can't convert value of type "
767 + value.getClass().getName()
768 + " to Short");
769 }
770 }
771
772 /***
773 * Convert value to char
774 *
775 * @param value
776 * the object to convert from
777 * @return the converted char
778 * @throws MessageFormatException
779 * if the conversion is invalid
780 * @throws NullPointerException
781 * if value is null
782 */
783 private char getChar(Object value) throws MessageFormatException {
784 if (value == null) {
785 return ((Character) null).charValue();
786 } else if (value instanceof Character) {
787 return ((Character)value).charValue();
788 } else {
789 throw new MessageFormatException(
790 "Can't convert value of type "
791 + value.getClass().getName()
792 + " to Char");
793 }
794 }
795
796 /***
797 * Convert value to int
798 *
799 * @param value
800 * @return the converted int
801 * @exception MessageFormatException
802 * if the conversion is invalid
803 * @exception NumberFormatException
804 * if value is a String and the conversion is invalid
805 */
806 private int getInt(Object value) throws MessageFormatException {
807 if (value instanceof Integer) {
808 return ((Integer)value).intValue();
809 } else if (value instanceof Byte) {
810 return ((Byte)value).intValue();
811 } else if (value instanceof Short) {
812 return ((Short)value).intValue();
813 } else if (value instanceof String) {
814 return Integer.parseInt((String)value);
815 } else if (value == null) {
816 return Integer.valueOf((String)value).intValue();
817 } else {
818 throw new MessageFormatException(
819 "Can't convert value of type "
820 + value.getClass().getName()
821 + " to Int");
822 }
823 }
824
825 /***
826 * Convert value to long
827 *
828 * @param value
829 * @return the converted long
830 * @exception MessageFormatException
831 * if the conversion is invalid
832 * @exception NumberFormatException
833 * if value is a String and the conversion is invalid
834 */
835 private long getLong(Object value) throws MessageFormatException {
836 if (value instanceof Long) {
837 return ((Long)value).longValue();
838 } else if (value instanceof Byte) {
839 return ((Byte)value).longValue();
840 } else if (value instanceof Short) {
841 return ((Short)value).longValue();
842 } else if (value instanceof Integer) {
843 return ((Integer)value).longValue();
844 } else if (value instanceof String) {
845 return Long.parseLong((String)value);
846 } else if (value == null) {
847 return Long.valueOf((String)value).longValue();
848 } else {
849 throw new MessageFormatException(
850 "Can't convert value of type "
851 + value.getClass().getName()
852 + " to Long");
853 }
854 }
855
856 /***
857 * Convert value to float
858 *
859 * @param value
860 * @return the converted float
861 * @exception MessageFormatException
862 * if the conversion is invalid
863 * @exception NumberFormatException
864 * if value is a String and the conversion is invalid
865 */
866 private float getFloat(Object value) throws MessageFormatException {
867 if (value instanceof Float) {
868 return ((Float)value).floatValue();
869 } else if (value instanceof String) {
870 return Float.parseFloat((String)value);
871 } else if (value == null) {
872 return Float.valueOf((String)value).floatValue();
873 } else {
874 throw new MessageFormatException(
875 "Can't convert value of type "
876 + value.getClass().getName()
877 + " to Float");
878 }
879 }
880
881 /***
882 * Convert value to double
883 *
884 * @param value
885 * the object to convert from
886 * @return the converted double
887 * @exception MessageFormatException
888 * if the conversion is invalid
889 * @exception NumberFormatException
890 * if value is a String and the conversion is invalid
891 */
892 private double getDouble(Object value) throws MessageFormatException {
893 if (value instanceof Double) {
894 return ((Double)value).doubleValue();
895 } else if (value instanceof Float) {
896 return ((Float)value).doubleValue();
897 } else if (value instanceof String) {
898 return Double.parseDouble((String)value);
899 } else if (value == null) {
900 return Double.valueOf((String)value).doubleValue();
901 } else {
902 throw new MessageFormatException(
903 "Can't convert value of type "
904 + value.getClass().getName()
905 + " to Double");
906 }
907 }
908
909 /***
910 * Convert value to String
911 *
912 * @param value
913 * the object to convert from
914 * @return the converted String
915 * @exception MessageFormatException
916 * if the conversion is invalid
917 */
918 private String getString(Object value) throws MessageFormatException {
919 return (value == null) ? null : String.valueOf(value);
920 }
921
922 /***
923 * Convert value to Bytes
924 *
925 * @param value
926 * the object to convert from
927 * @return the converted bytes
928 * @exception MessageFormatException
929 * if the conversion invalid
930 */
931 private byte[] getBytes(Object value) throws MessageFormatException {
932 if (value == null) {
933 return (byte[])value;
934 } else if (value instanceof byte[]) {
935 return (byte[])value;
936 } else {
937 throw new MessageFormatException(
938 "Can't convert value of type "
939 + value.getClass().getName()
940 + " to byte[].");
941 }
942 }
943 }
This page was automatically generated by Maven