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.util.Collection;
020import java.util.Iterator;
021
022import org.apache.camel.Converter;
023import org.apache.camel.Exchange;
024import org.apache.camel.util.ObjectHelper;
025
026/**
027 * Some core java.lang based <a
028 * href="http://camel.apache.org/type-converter.html">Type Converters</a>
029 *
030 * @version 
031 */
032@Converter
033public final class ObjectConverter {
034
035    /**
036     * Utility classes should not have a public constructor.
037     */
038    private ObjectConverter() {
039    }
040
041    public static boolean isCollection(Object value) {
042        return value instanceof Collection || (value != null && value.getClass().isArray());
043    }
044
045    /**
046     * Converts the given value to a boolean, handling strings or Boolean
047     * objects; otherwise returning false if the value could not be converted to
048     * a boolean
049     */
050    @Converter
051    public static boolean toBool(Object value) {
052        Boolean answer = toBoolean(value);
053        return answer != null && answer;
054    }
055
056    /**
057     * Converts the given value to a Boolean, handling strings or Boolean
058     * objects; otherwise returning null if the value cannot be converted to a
059     * boolean
060     */
061    @Converter
062    public static Boolean toBoolean(Object value) {
063        return ObjectHelper.toBoolean(value);
064    }
065
066    /**
067     * Creates an iterator over the value
068     */
069    @Converter
070    public static Iterator<?> iterator(Object value) {
071        return ObjectHelper.createIterator(value);
072    }
073
074    /**
075     * Creates an iterable over the value
076     */
077    @Converter
078    public static Iterable<?> iterable(Object value) {
079        return ObjectHelper.createIterable(value);
080    }
081
082    /**
083     * Returns the converted value, or null if the value is null
084     */
085    @Converter
086    public static Byte toByte(Object value) {
087        if (value instanceof Byte) {
088            return (Byte) value;
089        } else if (value instanceof Number) {
090            Number number = (Number) value;
091            return number.byteValue();
092        } else if (value instanceof String) {
093            return Byte.valueOf((String) value);
094        } else {
095            return null;
096        }
097    }
098
099    @Converter
100    public static char[] toCharArray(String value) {
101        return value.toCharArray();
102    }
103
104    @Converter
105    public static Character toCharacter(String value) {
106        return toChar(value);
107    }
108
109    @Converter
110    public static char toChar(String value) {
111        // must be string with the length of 1
112        if (value.length() != 1) {
113            throw new IllegalArgumentException("String must have exactly a length of 1: " + value);
114        }
115        return value.charAt(0);
116    }
117
118    @Converter
119    public static String fromCharArray(char[] value) {
120        return new String(value);
121    }
122    
123    /**
124     * Returns the converted value, or null if the value is null
125     */
126    @Converter
127    public static Class<?> toClass(Object value, Exchange exchange) {
128        if (value instanceof Class) {
129            return (Class<?>) value;
130        } else if (value instanceof String) {
131            // prefer to use class resolver API
132            if (exchange != null) {
133                return exchange.getContext().getClassResolver().resolveClass((String) value);
134            } else {
135                return ObjectHelper.loadClass((String) value);
136            }
137        } else {
138            return null;
139        }
140    }
141
142    /**
143     * Returns the converted value, or null if the value is null
144     */
145    @Converter
146    public static Short toShort(Object value) {
147        if (value instanceof Short) {
148            return (Short) value;
149        } else if (value instanceof Number) {
150            Number number = (Number) value;
151            return number.shortValue();
152        } else if (value instanceof String) {
153            return Short.valueOf((String) value);
154        } else {
155            return null;
156        }
157    }
158
159    /**
160     * Returns the converted value, or null if the value is null
161     */
162    @Converter
163    public static Integer toInteger(Object value) {
164        if (value instanceof Integer) {
165            return (Integer) value;
166        } else if (value instanceof Number) {
167            Number number = (Number) value;
168            return number.intValue();
169        } else if (value instanceof String) {
170            return Integer.valueOf((String) value);
171        } else {
172            return null;
173        }
174    }
175
176    /**
177     * Returns the converted value, or null if the value is null
178     */
179    @Converter
180    public static Long toLong(Object value) {
181        if (value instanceof Long) {
182            return (Long) value;
183        } else if (value instanceof Number) {
184            Number number = (Number) value;
185            return number.longValue();
186        } else if (value instanceof String) {
187            return Long.valueOf((String) value);
188        } else {
189            return null;
190        }
191    }
192
193    /**
194     * Returns the converted value, or null if the value is null
195     */
196    @Converter
197    public static Float toFloat(Object value) {
198        if (value instanceof Float) {
199            return (Float) value;
200        } else if (value instanceof Number) {
201            if (ObjectHelper.isNaN(value)) {
202                return Float.NaN;
203            }
204            Number number = (Number) value;
205            return number.floatValue();
206        } else if (value instanceof String) {
207            return Float.valueOf((String) value);
208        } else {
209            return null;
210        }
211    }
212
213    /**
214     * Returns the converted value, or null if the value is null
215     */
216    @Converter
217    public static Double toDouble(Object value) {
218        if (value instanceof Double) {
219            return (Double) value;
220        } else if (value instanceof Number) {
221            if (ObjectHelper.isNaN(value)) {
222                return Double.NaN;
223            }
224            Number number = (Number) value;
225            return number.doubleValue();
226        } else if (value instanceof String) {
227            return Double.valueOf((String) value);
228        } else {
229            return null;
230        }
231    }
232
233    // add fast type converters from most common used
234
235    @Converter
236    public static String toString(Integer value) {
237        return value.toString();
238    }
239
240    @Converter
241    public static String toString(Long value) {
242        return value.toString();
243    }
244
245    @Converter
246    public static String toString(Boolean value) {
247        return value.toString();
248    }
249
250    @Converter
251    public static String toString(StringBuffer value) {
252        return value.toString();
253    }
254
255    @Converter
256    public static String toString(StringBuilder value) {
257        return value.toString();
258    }
259
260    @Converter
261    public static Integer toInteger(String value) {
262        return Integer.valueOf(value);
263    }
264
265    @Converter
266    public static Long toLong(String value) {
267        return Long.valueOf(value);
268    }
269
270    @Converter
271    public static Float toFloat(String value) {
272        return Float.valueOf(value);
273    }
274
275    @Converter
276    public static Double toDouble(String value) {
277        return Double.valueOf(value);
278    }
279
280    @Converter
281    public static Boolean toBoolean(String value) {
282        return Boolean.parseBoolean(value);
283    }
284
285}