001: package org.mockejb.jms;
002:
003: import java.util.*;
004: import javax.jms.*;
005:
006: /**
007: * Map that sotre name-value pairs, where the values are <code>null</code> or
008: * instances of String, Boolean, Byte, Character, Short, Integer, Long, Float,
009: * Double or byte[].
010: * <p>
011: * Conversion between types is supported as specified in
012: * <code>javax.jms.StreamMessage</code>
013: * @author Dimitar Gospodinov
014: */
015: class PrimitiveMap {
016:
017: private final Map map = new HashMap();
018:
019: private byte[] copyBytes(byte[] source) {
020: if (source == null) {
021: return null;
022: } else {
023: return (byte[]) source.clone();
024: }
025: }
026:
027: public boolean getBoolean(String name) throws JMSException {
028: checkName(name);
029: Object value = map.get(name);
030:
031: if (value instanceof Boolean) {
032: return ((Boolean) value).booleanValue();
033: }
034: if (value == null || value instanceof String) {
035: return Boolean.valueOf((String) value).booleanValue();
036: }
037: throw new MessageFormatException(name
038: + " does not represent valid Boolean value!");
039: }
040:
041: public byte getByte(String name) throws JMSException {
042: checkName(name);
043: Object value = map.get(name);
044:
045: if (value instanceof Byte) {
046: return ((Byte) value).byteValue();
047: }
048: if (value == null || value instanceof String) {
049: return Byte.valueOf((String) value).byteValue();
050: }
051: throw new MessageFormatException(name
052: + " does not represent valid Byte value!");
053: }
054:
055: public byte[] getBytes(String name) throws JMSException {
056: checkName(name);
057: Object value = map.get(name);
058: if (value == null || value instanceof byte[]) {
059: return copyBytes((byte[]) value);
060: }
061: throw new MessageFormatException(name
062: + " does not represent valid byte[] value!");
063: }
064:
065: public char getChar(String name) throws JMSException {
066: checkName(name);
067: Object value = map.get(name);
068: if (value == null) {
069: throw new NullPointerException();
070: }
071: if (value instanceof Character) {
072: return ((Character) value).charValue();
073: }
074: throw new MessageFormatException(name
075: + " does not represent valid Char value!");
076: }
077:
078: public short getShort(String name) throws JMSException {
079: checkName(name);
080: Object value = map.get(name);
081:
082: if (value instanceof Byte || value instanceof Short) {
083: return ((Number) value).shortValue();
084: }
085: if (value == null || value instanceof String) {
086: return Short.valueOf((String) value).shortValue();
087: }
088: throw new MessageFormatException(name
089: + " does not represent valid Short value!");
090: }
091:
092: public int getInt(String name) throws JMSException {
093: checkName(name);
094: Object value = map.get(name);
095:
096: if (value instanceof Byte || value instanceof Short
097: || value instanceof Integer) {
098: return ((Number) value).intValue();
099: }
100: if (value == null || value instanceof String) {
101: return Integer.valueOf((String) value).intValue();
102: }
103: throw new MessageFormatException(name
104: + " does not represent valid Integer value!");
105: }
106:
107: public long getLong(String name) throws JMSException {
108: checkName(name);
109: Object value = map.get(name);
110:
111: if (value instanceof Byte || value instanceof Short
112: || value instanceof Integer || value instanceof Long) {
113:
114: return ((Number) value).longValue();
115: }
116: if (value == null || value instanceof String) {
117: return Long.valueOf((String) value).longValue();
118: }
119: throw new MessageFormatException(name
120: + " does not represent valid Long value!");
121: }
122:
123: public float getFloat(String name) throws JMSException {
124: checkName(name);
125: Object value = map.get(name);
126:
127: if (value instanceof Float) {
128: return ((Float) value).floatValue();
129: }
130: if (value == null || value instanceof String) {
131: return Float.valueOf((String) value).floatValue();
132: }
133: throw new MessageFormatException(name
134: + " does not represent valid Float value!");
135: }
136:
137: public double getDouble(String name) throws JMSException {
138: checkName(name);
139: Object value = map.get(name);
140:
141: if (value instanceof Float || value instanceof Double) {
142: return ((Number) value).doubleValue();
143: }
144: if (value == null || value instanceof String) {
145: return Double.valueOf((String) value).doubleValue();
146: }
147: throw new MessageFormatException(name
148: + " does not represent valid Double value!");
149: }
150:
151: public String getString(String name) throws JMSException {
152: checkName(name);
153: Object value = map.get(name);
154: if (value == null) {
155: return null;
156: }
157: if (value instanceof Boolean || value instanceof Byte
158: || value instanceof Short || value instanceof Character
159: || value instanceof Integer || value instanceof Long
160: || value instanceof Float || value instanceof Double
161: || value instanceof String) {
162:
163: return value.toString();
164: }
165: throw new MessageFormatException(name
166: + " does not represent valid String value!");
167: }
168:
169: public Object getObject(String name) throws JMSException {
170: checkName(name);
171: Object result = map.get(name);
172: if (result instanceof byte[]) {
173: result = copyBytes((byte[]) result);
174: }
175: return result;
176: }
177:
178: public void setBoolean(String name, boolean value)
179: throws JMSException {
180:
181: checkName(name);
182: map.put(name, new Boolean(value));
183: }
184:
185: public void setByte(String name, byte value) throws JMSException {
186: checkName(name);
187: map.put(name, new Byte(value));
188: }
189:
190: public void setBytes(String name, byte[] value) {
191: checkName(name);
192: map.put(name, copyBytes(value));
193: }
194:
195: public void setChar(String name, char value) {
196: checkName(name);
197: map.put(name, new Character(value));
198: }
199:
200: public void setShort(String name, short value) throws JMSException {
201: checkName(name);
202: map.put(name, new Short(value));
203: }
204:
205: public void setInt(String name, int value) throws JMSException {
206: checkName(name);
207: map.put(name, new Integer(value));
208: }
209:
210: public void setLong(String name, long value) throws JMSException {
211: checkName(name);
212: map.put(name, new Long(value));
213: }
214:
215: public void setFloat(String name, float value) throws JMSException {
216: checkName(name);
217: map.put(name, new Float(value));
218: }
219:
220: public void setDouble(String name, double value)
221: throws JMSException {
222:
223: checkName(name);
224: map.put(name, new Double(value));
225: }
226:
227: public void setString(String name, String value)
228: throws JMSException {
229:
230: checkName(name);
231: map.put(name, value);
232: }
233:
234: /* (non-Javadoc)
235: * @see javax.jms.Message#setObjectProperty(java.lang.String, java.lang.Object)
236: */
237: public void setObject(String name, Object value)
238: throws JMSException {
239:
240: checkName(name);
241: if (!(value == null || value instanceof byte[]
242: || value instanceof Boolean || value instanceof Byte
243: || value instanceof Character || value instanceof Short
244: || value instanceof Integer || value instanceof Long
245: || value instanceof Float || value instanceof Double || value instanceof String)) {
246:
247: throw new MessageFormatException("Incorrect object type!");
248: }
249: if (value instanceof byte[]) {
250: value = copyBytes((byte[]) value);
251: }
252: map.put(name, value);
253: }
254:
255: private void checkName(String name) {
256: if (name == null || name.length() == 0) {
257: throw new IllegalArgumentException(
258: "Name must be non-empty string!");
259: }
260: }
261:
262: public void clear() {
263: map.clear();
264: }
265:
266: public boolean containsKey(String key) {
267: checkName(key);
268: return map.containsKey(key);
269: }
270:
271: public Enumeration getNames() {
272: return new CollectionEnumeration(map.keySet().iterator());
273: }
274:
275: }
|