KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > util > JMSMap


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.util;
8
9 import javax.jms.JMSException JavaDoc;
10 import javax.jms.MapMessage JavaDoc;
11 import javax.jms.MessageFormatException JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * JMS specific map backed by a {@link HashMap}. Does not implement
18  * <code>Map</code> but instead implements the body elements of the
19  * {@link MapMessage}. Serves as he basis for {@link MessageProperties}
20  * and the body for the <code>MapMessage</code>.
21  *
22  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
23  * @version $Revision: 1.2 $ $Date: 2003/08/21 10:13:33 $
24  */

25 public class JMSMap implements java.io.Serializable JavaDoc
26 {
27
28     protected HashMap JavaDoc contents = new HashMap JavaDoc();
29
30     public static final JMSMap createInstance(Class JavaDoc type)
31     {
32         if (type.equals(MapMessage JavaDoc.class))
33         {
34             return new JMSMap();
35         }
36         else
37         {
38             return new MessageProperties();
39         }
40     }
41
42     public final void clear()
43     {
44         this.contents.clear();
45     }
46
47     public boolean getBoolean(String JavaDoc name) throws JMSException JavaDoc
48     {
49         Object JavaDoc value = this.contents.get(name);
50         return JMSTypeConversions.getBoolean(value);
51     }
52
53     public byte getByte(String JavaDoc name) throws JMSException JavaDoc
54     {
55         Object JavaDoc value = this.contents.get(name);
56         return JMSTypeConversions.getByte(value);
57     }
58
59     public byte[] getBytes(String JavaDoc name) throws JMSException JavaDoc
60     {
61         Object JavaDoc value = this.contents.get(name);
62         return JMSTypeConversions.getBytes(value);
63     }
64
65     public char getChar(String JavaDoc name) throws JMSException JavaDoc
66     {
67         Object JavaDoc value = this.contents.get(name);
68         return JMSTypeConversions.getChar(value);
69     }
70
71     public double getDouble(String JavaDoc name) throws JMSException JavaDoc
72     {
73         Object JavaDoc value = this.contents.get(name);
74         return JMSTypeConversions.getDouble(value);
75     }
76
77     public float getFloat(String JavaDoc name) throws JMSException JavaDoc
78     {
79         Object JavaDoc value = this.contents.get(name);
80         return JMSTypeConversions.getFloat(value);
81     }
82
83     public int getInt(String JavaDoc name) throws JMSException JavaDoc
84     {
85         Object JavaDoc value = this.contents.get(name);
86         return JMSTypeConversions.getInt(value);
87     }
88
89     public long getLong(String JavaDoc name) throws JMSException JavaDoc
90     {
91         Object JavaDoc value = this.contents.get(name);
92         return JMSTypeConversions.getLong(value);
93     }
94
95     public Enumeration JavaDoc getMapNames() throws JMSException JavaDoc
96     {
97         return Collections.enumeration(this.contents.keySet());
98     }
99
100     public Object JavaDoc getObject(String JavaDoc name) throws JMSException JavaDoc
101     {
102         return this.contents.get(name);
103     }
104
105     public short getShort(String JavaDoc name) throws JMSException JavaDoc
106     {
107         Object JavaDoc value = this.contents.get(name);
108         return JMSTypeConversions.getShort(value);
109     }
110
111     public String JavaDoc getString(String JavaDoc name) throws JMSException JavaDoc
112     {
113         Object JavaDoc value = this.contents.get(name);
114         return JMSTypeConversions.getString(value);
115     }
116
117     public boolean itemExists(String JavaDoc name)
118     {
119         return this.contents.containsKey(name);
120     }
121
122     public void setBoolean(String JavaDoc name, boolean value) throws JMSException JavaDoc
123     {
124         this.contents.put(name, new Boolean JavaDoc(value));
125     }
126
127     public void setByte(String JavaDoc name, byte value) throws JMSException JavaDoc
128     {
129         this.contents.put(name, new Byte JavaDoc(value));
130     }
131
132     public void setBytes(String JavaDoc name, byte[] value) throws JMSException JavaDoc
133     {
134         byte[] bytes = new byte[value.length];
135         System.arraycopy(value, 0, bytes, 0, bytes.length);
136         this.contents.put(name, bytes);
137     }
138
139     public void setBytes(String JavaDoc name, byte[] value, int offset, int length)
140             throws JMSException JavaDoc
141     {
142         byte[] bytes = new byte[length];
143         System.arraycopy(value, offset, bytes, 0, length);
144         this.contents.put(name, bytes);
145     }
146
147     public void setChar(String JavaDoc name, char value) throws JMSException JavaDoc
148     {
149         this.contents.put(name, new Character JavaDoc(value));
150     }
151
152     public void setDouble(String JavaDoc name, double value) throws JMSException JavaDoc
153     {
154         this.contents.put(name, new Double JavaDoc(value));
155     }
156
157     public void setFloat(String JavaDoc name, float value) throws JMSException JavaDoc
158     {
159         this.contents.put(name, new Float JavaDoc(value));
160     }
161
162     public void setInt(String JavaDoc name, int value) throws JMSException JavaDoc
163     {
164         this.contents.put(name, new Integer JavaDoc(value));
165     }
166
167     public void setLong(String JavaDoc name, long value) throws JMSException JavaDoc
168     {
169         this.contents.put(name, new Long JavaDoc(value));
170     }
171
172     public void setObject(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc
173     {
174         if (value instanceof Boolean JavaDoc
175                 || value instanceof Byte JavaDoc
176                 || value instanceof Character JavaDoc
177                 || value instanceof Double JavaDoc
178                 || value instanceof Float JavaDoc
179                 || value instanceof Integer JavaDoc
180                 || value instanceof Long JavaDoc
181                 || value instanceof Short JavaDoc
182                 || value instanceof String JavaDoc)
183         {
184             this.contents.put(name, value);
185         }
186         else
187         {
188             throw new MessageFormatException JavaDoc(""); //TOD: Implement message
189
}
190     }
191
192     public void setShort(String JavaDoc name, short value) throws JMSException JavaDoc
193     {
194         this.contents.put(name, new Short JavaDoc(value));
195     }
196
197     public void setString(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc
198     {
199         this.contents.put(name, value);
200     }
201 }
Popular Tags