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     */
017    package org.apache.camel.converter;
018    
019    import java.util.Collection;
020    import java.util.Iterator;
021    
022    import org.apache.camel.Converter;
023    import org.apache.camel.Exchange;
024    import 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
033    public 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        /**
076         * Returns the converted value, or null if the value is null
077         */
078        @Converter
079        public static Byte toByte(Object value) {
080            if (value instanceof Byte) {
081                return (Byte) value;
082            } else if (value instanceof Number) {
083                Number number = (Number) value;
084                return number.byteValue();
085            } else if (value instanceof String) {
086                return Byte.valueOf((String) value);
087            } else {
088                return null;
089            }
090        }
091    
092        @Converter
093        public static char[] toCharArray(String value) {
094            return value.toCharArray();
095        }
096    
097        @Converter
098        public static char toChar(String value) {
099            // must be string with the length of 1
100            if (value == null || value.length() != 1) {
101                throw new IllegalArgumentException("String must have exactly a length of 1: " + value);
102            }
103            return value.charAt(0);
104        }
105    
106        @Converter
107        public static String fromCharArray(char[] value) {
108            return new String(value);
109        }
110        
111        /**
112         * Returns the converted value, or null if the value is null
113         */
114        @Converter
115        public static Class<?> toClass(Object value, Exchange exchange) {
116            if (value instanceof Class) {
117                return (Class<?>) value;
118            } else if (value instanceof String) {
119                // prefer to use class resolver API
120                if (exchange != null) {
121                    return exchange.getContext().getClassResolver().resolveClass((String) value);
122                } else {
123                    return ObjectHelper.loadClass((String) value);
124                }
125            } else {
126                return null;
127            }
128        }
129    
130        /**
131         * Returns the converted value, or null if the value is null
132         */
133        @Converter
134        public static Short toShort(Object value) {
135            if (value instanceof Short) {
136                return (Short) value;
137            } else if (value instanceof Number) {
138                Number number = (Number) value;
139                return number.shortValue();
140            } else if (value instanceof String) {
141                return Short.valueOf((String) value);
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 Integer toInteger(Object value) {
152            if (value instanceof Integer) {
153                return (Integer) value;
154            } else if (value instanceof Number) {
155                Number number = (Number) value;
156                return number.intValue();
157            } else if (value instanceof String) {
158                return Integer.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 Long toLong(Object value) {
169            if (value instanceof Long) {
170                return (Long) value;
171            } else if (value instanceof Number) {
172                Number number = (Number) value;
173                return number.longValue();
174            } else if (value instanceof String) {
175                return Long.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 Float toFloat(Object value) {
186            if (value instanceof Float) {
187                return (Float) value;
188            } else if (value instanceof Number) {
189                if (ObjectHelper.isNaN(value)) {
190                    return Float.NaN;
191                }
192                Number number = (Number) value;
193                return number.floatValue();
194            } else if (value instanceof String) {
195                return Float.valueOf((String) value);
196            } else {
197                return null;
198            }
199        }
200    
201        /**
202         * Returns the converted value, or null if the value is null
203         */
204        @Converter
205        public static Double toDouble(Object value) {
206            if (value instanceof Double) {
207                return (Double) value;
208            } else if (value instanceof Number) {
209                if (ObjectHelper.isNaN(value)) {
210                    return Double.NaN;
211                }
212                Number number = (Number) value;
213                return number.doubleValue();
214            } else if (value instanceof String) {
215                return Double.valueOf((String) value);
216            } else {
217                return null;
218            }
219        }
220    
221        // add fast type converters from most common used
222    
223        @Converter
224        public static String toString(Integer value) {
225            return value.toString();
226        }
227    
228        @Converter
229        public static String toString(Long value) {
230            return value.toString();
231        }
232    
233        @Converter
234        public static String toString(Boolean value) {
235            return value.toString();
236        }
237    
238        @Converter
239        public static String toString(StringBuffer value) {
240            return value.toString();
241        }
242    
243        @Converter
244        public static String toString(StringBuilder value) {
245            return value.toString();
246        }
247    
248        @Converter
249        public static Integer toInteger(String value) {
250            return Integer.valueOf(value);
251        }
252    
253        @Converter
254        public static Long toLong(String value) {
255            return Long.valueOf(value);
256        }
257    
258        @Converter
259        public static Float toFloat(String value) {
260            return Float.valueOf(value);
261        }
262    
263        @Converter
264        public static Double toDouble(String value) {
265            return Double.valueOf(value);
266        }
267    
268        @Converter
269        public static Boolean toBoolean(String value) {
270            return Boolean.parseBoolean(value);
271        }
272    
273    }