001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.converter;
018
019import java.math.BigInteger;
020import java.util.Collection;
021import java.util.Iterator;
022
023import org.apache.camel.Converter;
024import org.apache.camel.Exchange;
025import org.apache.camel.util.ObjectHelper;
026
027/**
028 * Some core java.lang based <a
029 * href="http://camel.apache.org/type-converter.html">Type Converters</a>
030 *
031 * @version 
032 */
033@Converter
034public final class ObjectConverter {
035
036    /**
037     * Utility classes should not have a public constructor.
038     */
039    private ObjectConverter() {
040    }
041
042    /**
043     * @deprecated not in use
044     */
045    @Deprecated
046    public static boolean isCollection(Object value) {
047        return value instanceof Collection || (value != null && value.getClass().isArray());
048    }
049
050    /**
051     * Converts the given value to a boolean, handling strings or Boolean
052     * objects; otherwise returning false if the value could not be converted to
053     * a boolean
054     */
055    @Converter
056    public static boolean toBool(Object value) {
057        Boolean answer = toBoolean(value);
058        return answer != null && answer;
059    }
060
061    /**
062     * Converts the given value to a Boolean, handling strings or Boolean
063     * objects; otherwise returning null if the value cannot be converted to a
064     * boolean
065     */
066    @Converter
067    public static Boolean toBoolean(Object value) {
068        return ObjectHelper.toBoolean(value);
069    }
070
071    /**
072     * Creates an iterator over the value
073     */
074    @Converter
075    public static Iterator<?> iterator(Object value) {
076        return ObjectHelper.createIterator(value);
077    }
078
079    /**
080     * Creates an iterable over the value
081     */
082    @Converter
083    public static Iterable<?> iterable(Object value) {
084        return ObjectHelper.createIterable(value);
085    }
086
087    /**
088     * Returns the converted value, or null if the value is null
089     */
090    @Converter
091    public static Byte toByte(Object value) {
092        if (value instanceof Byte) {
093            return (Byte) value;
094        } else if (value instanceof Number) {
095            Number number = (Number) value;
096            return number.byteValue();
097        } else if (value instanceof String) {
098            return Byte.valueOf((String) value);
099        } else {
100            return null;
101        }
102    }
103
104    @Converter
105    public static char[] toCharArray(String value) {
106        return value.toCharArray();
107    }
108
109    @Converter
110    public static Character toCharacter(String value) {
111        return toChar(value);
112    }
113
114    @Converter
115    public static char toChar(String value) {
116        // must be string with the length of 1
117        if (value.length() != 1) {
118            throw new IllegalArgumentException("String must have exactly a length of 1: " + value);
119        }
120        return value.charAt(0);
121    }
122
123    @Converter
124    public static String fromCharArray(char[] value) {
125        return new String(value);
126    }
127    
128    /**
129     * Returns the converted value, or null if the value is null
130     */
131    @Converter
132    public static Class<?> toClass(Object value, Exchange exchange) {
133        if (value instanceof Class) {
134            return (Class<?>) value;
135        } else if (value instanceof String) {
136            // prefer to use class resolver API
137            if (exchange != null) {
138                return exchange.getContext().getClassResolver().resolveClass((String) value);
139            } else {
140                return ObjectHelper.loadClass((String) value);
141            }
142        } else {
143            return null;
144        }
145    }
146
147    /**
148     * Returns the converted value, or null if the value is null
149     */
150    @Converter
151    public static Short toShort(Object value) {
152        if (value instanceof Short) {
153            return (Short) value;
154        } else if (value instanceof Number) {
155            Number number = (Number) value;
156            return number.shortValue();
157        } else if (value instanceof String) {
158            return Short.valueOf((String) value);
159        } else {
160            return null;
161        }
162    }
163
164    /**
165     * Returns the converted value, or null if the value is null
166     */
167    @Converter
168    public static Integer toInteger(Object value) {
169        if (value instanceof Integer) {
170            return (Integer) value;
171        } else if (value instanceof Number) {
172            Number number = (Number) value;
173            return number.intValue();
174        } else if (value instanceof String) {
175            return Integer.valueOf((String) value);
176        } else {
177            return null;
178        }
179    }
180
181    /**
182     * Returns the converted value, or null if the value is null
183     */
184    @Converter
185    public static Long toLong(Object value) {
186        if (value instanceof Long) {
187            return (Long) value;
188        } else if (value instanceof Number) {
189            Number number = (Number) value;
190            return number.longValue();
191        } else if (value instanceof String) {
192            return Long.valueOf((String) value);
193        } else {
194            return null;
195        }
196    }
197
198    /**
199     * Returns the converted value, or null if the value is null
200     */
201    @Converter
202    public static BigInteger toBigInteger(Object value) {
203        Long num = null;
204        if (value instanceof Long) {
205            num = (Long) value;
206        } else if (value instanceof Number) {
207            Number number = (Number) value;
208            num = number.longValue();
209        } else if (value instanceof String) {
210            num = Long.valueOf((String) value);
211        }
212        if (num != null) {
213            return BigInteger.valueOf(num);
214        } else {
215            return null;
216        }
217    }
218
219    /**
220     * Returns the converted value, or null if the value is null
221     */
222    @Converter
223    public static Float toFloat(Object value) {
224        if (value instanceof Float) {
225            return (Float) value;
226        } else if (value instanceof Number) {
227            if (ObjectHelper.isNaN(value)) {
228                return Float.NaN;
229            }
230            Number number = (Number) value;
231            return number.floatValue();
232        } else if (value instanceof String) {
233            return Float.valueOf((String) value);
234        } else {
235            return null;
236        }
237    }
238
239    /**
240     * Returns the converted value, or null if the value is null
241     */
242    @Converter
243    public static Double toDouble(Object value) {
244        if (value instanceof Double) {
245            return (Double) value;
246        } else if (value instanceof Number) {
247            if (ObjectHelper.isNaN(value)) {
248                return Double.NaN;
249            }
250            Number number = (Number) value;
251            return number.doubleValue();
252        } else if (value instanceof String) {
253            return Double.valueOf((String) value);
254        } else {
255            return null;
256        }
257    }
258
259    // add fast type converters from most common used
260
261    @Converter
262    public static String toString(Integer value) {
263        return value.toString();
264    }
265
266    @Converter
267    public static String toString(Long value) {
268        return value.toString();
269    }
270
271    @Converter
272    public static String toString(Boolean value) {
273        return value.toString();
274    }
275
276    @Converter
277    public static String toString(StringBuffer value) {
278        return value.toString();
279    }
280
281    @Converter
282    public static String toString(StringBuilder value) {
283        return value.toString();
284    }
285
286    @Converter
287    public static Integer toInteger(String value) {
288        return Integer.valueOf(value);
289    }
290
291    @Converter
292    public static Long toLong(String value) {
293        return Long.valueOf(value);
294    }
295
296    @Converter
297    public static Float toFloat(String value) {
298        return Float.valueOf(value);
299    }
300
301    @Converter
302    public static Double toDouble(String value) {
303        return Double.valueOf(value);
304    }
305
306    @Converter
307    public static Boolean toBoolean(String value) {
308        return Boolean.parseBoolean(value);
309    }
310
311}