001: package dalma.endpoints.jms.impl;
002:
003: import javax.jms.MessageFormatException;
004: import javax.jms.JMSException;
005: import java.util.Map;
006: import java.util.HashMap;
007:
008: /**
009: * Converts the type of a value according to the following table in JMS:
010: *
011: * <pre>
012: * | | boolean byte short char int long float double String byte[]
013: * |----------------------------------------------------------------------
014: * |boolean | X X
015: * |byte | X X X X X
016: * |short | X X X X
017: * |char | X X
018: * |int | X X X
019: * |long | X X
020: * |float | X X X
021: * |double | X X
022: * |String | X X X X X X X X
023: * |byte[] | X
024: * |----------------------------------------------------------------------
025: * </pre>
026: *
027: * @author Kohsuke Kawaguchi
028: */
029: abstract class Converter<T> {
030: private final Class<T> sourceType;
031:
032: private Converter(Class<T> sourceType) {
033: this .sourceType = sourceType;
034: convs.put(sourceType, this ); // register
035: }
036:
037: //
038: // conversion methods
039: //
040: boolean asBoolean(T source) throws JMSException {
041: throw error("boolean");
042: }
043:
044: byte asByte(T source) throws JMSException {
045: throw error("byte");
046: }
047:
048: short asShort(T source) throws JMSException {
049: throw error("short");
050: }
051:
052: char asChar(T source) throws JMSException {
053: throw error("char");
054: }
055:
056: int asInt(T source) throws JMSException {
057: throw error("int");
058: }
059:
060: long asLong(T source) throws JMSException {
061: throw error("long");
062: }
063:
064: float asFloat(T source) throws JMSException {
065: throw error("float");
066: }
067:
068: double asDouble(T source) throws JMSException {
069: throw error("double");
070: }
071:
072: String asString(T source) throws JMSException {
073: // throw error("string");
074: return source.toString();
075: }
076:
077: byte[] asByteArray(T source) throws JMSException {
078: throw error("byte[]");
079: }
080:
081: // table
082: private static Map<Class, Converter> convs = new HashMap<Class, Converter>();
083:
084: /**
085: * Gets the converter for the given class.
086: */
087: public static <T> Converter<T> get(Class<T> type) {
088: return (Converter<T>) convs.get(type);
089: }
090:
091: public static Converter get(Object o) {
092: if (o == null)
093: return NULL;
094: return convs.get(o.getClass());
095: }
096:
097: //
098: // converters
099: //
100: static final Converter<Boolean> BOOLEAN = new Converter<Boolean>(
101: Boolean.class) {
102: boolean asBoolean(Boolean source) {
103: return source;
104: }
105: };
106:
107: static final Converter<Byte> BYTE = new Converter<Byte>(Byte.class) {
108: byte asByte(Byte source) {
109: return source;
110: }
111:
112: short asShort(Byte source) {
113: return source;
114: }
115:
116: int asInt(Byte source) {
117: return source;
118: }
119:
120: long asLong(Byte source) {
121: return source;
122: }
123: };
124:
125: static final Converter<Short> SHORT = new Converter<Short>(
126: Short.class) {
127: short asShort(Short source) {
128: return source;
129: }
130:
131: int asInt(Short source) {
132: return source;
133: }
134:
135: long asLong(Short source) {
136: return source;
137: }
138: };
139:
140: static final Converter<Character> CHARACTER = new Converter<Character>(
141: Character.class) {
142: char asChar(Character source) {
143: return source;
144: }
145: };
146:
147: static final Converter<Integer> INTEGER = new Converter<Integer>(
148: Integer.class) {
149: int asInt(Integer source) {
150: return source;
151: }
152:
153: long asLong(Integer source) {
154: return source;
155: }
156: };
157:
158: static final Converter<Long> LONG = new Converter<Long>(Long.class) {
159: long asLong(Long source) {
160: return source;
161: }
162: };
163:
164: static final Converter<Float> FLOAT = new Converter<Float>(
165: Float.class) {
166: float asFloat(Float source) {
167: return source;
168: }
169:
170: double asDouble(Float source) {
171: return source;
172: }
173: };
174:
175: static final Converter<Double> DOUBLE = new Converter<Double>(
176: Double.class) {
177: double asDouble(Double source) {
178: return source;
179: }
180: };
181:
182: static final Converter<String> STRING = new Converter<String>(
183: String.class) {
184: boolean asBoolean(String source) {
185: return Boolean.parseBoolean(source);
186: }
187:
188: byte asByte(String source) {
189: return Byte.parseByte(source);
190: }
191:
192: short asShort(String source) {
193: return Short.parseShort(source);
194: }
195:
196: int asInt(String source) {
197: return Integer.parseInt(source);
198: }
199:
200: long asLong(String source) {
201: return Long.parseLong(source);
202: }
203:
204: float asFloat(String source) {
205: return Float.parseFloat(source);
206: }
207:
208: double asDouble(String source) {
209: return Double.parseDouble(source);
210: }
211: };
212:
213: static final Converter<byte[]> BYTE_ARRAY = new Converter<byte[]>(
214: byte[].class) {
215: String asString(byte[] source) throws JMSException {
216: throw error("byte[]");
217: }
218:
219: byte[] asByteArray(byte[] source) {
220: return source;
221: }
222: };
223:
224: static final Converter<?> NULL = new Converter<Object>(Object.class) {
225: boolean asBoolean(Object source) {
226: return false;
227: }
228:
229: byte asByte(Object source) {
230: return 0;
231: }
232:
233: short asShort(Object source) {
234: return 0;
235: }
236:
237: char asChar(Object source) {
238: return 0;
239: }
240:
241: int asInt(Object source) {
242: return 0;
243: }
244:
245: long asLong(Object source) {
246: return 0;
247: }
248:
249: float asFloat(Object source) {
250: return 0;
251: }
252:
253: double asDouble(Object source) {
254: return 0;
255: }
256:
257: String asString(Object source) {
258: return null;
259: }
260:
261: byte[] asByteArray(Object source) {
262: return null;
263: }
264: };
265:
266: //
267: // impl
268: //
269: protected final JMSException error(String name)
270: throws MessageFormatException {
271: throw new MessageFormatException(sourceType.getName()
272: + " cannot be converted to " + name);
273: }
274: }
|