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.util.ObjectHelper;
024    
025    /**
026     * Some core java.lang based <a
027     * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
028     *
029     * @version $Revision: 42486 $
030     */
031    @Converter
032    public final class ObjectConverter {
033    
034        /**
035         * Utility classes should not have a public constructor.
036         */
037        private ObjectConverter() {
038        }
039    
040        public static boolean isCollection(Object value) {
041            // TODO we should handle primitive array types?
042            return value instanceof Collection || (value != null && value.getClass().isArray());
043        }
044    
045        /**
046         * Creates an iterator over the value
047         *
048         * @deprecated use {@link org.apache.camel.util.ObjectHelper#createIterator(Object)}. Will be removed in Camel 2.0.
049         */
050        @SuppressWarnings("unchecked")
051        @Converter
052        @Deprecated
053        public static Iterator iterator(Object value) {
054            return ObjectHelper.createIterator(value);
055        }
056    
057        /**
058         * Converts the given value to a boolean, handling strings or Boolean
059         * objects; otherwise returning false if the value could not be converted to
060         * a boolean
061         */
062        @Converter
063        public static boolean toBool(Object value) {
064            Boolean answer = toBoolean(value);
065            if (answer != null) {
066                return answer.booleanValue();
067            }
068            return false;
069        }
070    
071        /**
072         * Converts the given value to a Boolean, handling strings or Boolean
073         * objects; otherwise returning null if the value cannot be converted to a
074         * boolean
075         */
076        @Converter
077        public static Boolean toBoolean(Object value) {
078            return ObjectHelper.toBoolean(value);
079        }
080    
081        /**
082         * Returns the boolean value, or null if the value is null
083         */
084        @Converter
085        public static Boolean toBoolean(Boolean value) {
086            if (value != null) {
087                return value;
088            }
089            return Boolean.FALSE;
090        }
091    
092    
093        /**
094         * Returns the converted value, or null if the value is null
095         */
096        @Converter
097        public static Byte toByte(Object value) {
098            if (value instanceof Byte) {
099                return (Byte) value;
100            } else if (value instanceof Number) {
101                Number number = (Number) value;
102                return Byte.valueOf(number.byteValue());
103            } else if (value instanceof String) {
104                return Byte.valueOf((String) value);
105            } else {
106                return null;
107            }
108        }
109    
110        @Converter
111        public static byte[] toByteArray(String value) {
112            return value.getBytes();
113        }
114    
115        @Converter
116        public static char[] toCharArray(String value) {
117            return value.toCharArray();
118        }
119    
120        @Converter
121        public static String fromCharArray(char[] value) {
122            return new String(value);
123        }
124    
125        /**
126         * Returns the converted value, or null if the value is null
127         */
128        @Converter
129        public static Short toShort(Object value) {
130            if (value instanceof Short) {
131                return (Short) value;
132            } else if (value instanceof Number) {
133                Number number = (Number) value;
134                return Short.valueOf(number.shortValue());
135            } else if (value instanceof String) {
136                return Short.valueOf((String) value);
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 Integer toInteger(Object value) {
147            if (value instanceof Integer) {
148                return (Integer) value;
149            } else if (value instanceof Number) {
150                Number number = (Number) value;
151                return Integer.valueOf(number.intValue());
152            } else if (value instanceof String) {
153                return Integer.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 Long toLong(Object value) {
164            if (value instanceof Long) {
165                return (Long) value;
166            } else if (value instanceof Number) {
167                Number number = (Number) value;
168                return Long.valueOf(number.longValue());
169            } else if (value instanceof String) {
170                return Long.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 Float toFloat(Object value) {
181            if (value instanceof Float) {
182                return (Float) value;
183            } else if (value instanceof Number) {
184                Number number = (Number) value;
185                return Float.valueOf(number.floatValue());
186            } else if (value instanceof String) {
187                return Float.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 Double toDouble(Object value) {
198            if (value instanceof Double) {
199                return (Double) value;
200            } else if (value instanceof Number) {
201                Number number = (Number) value;
202                return Double.valueOf(number.doubleValue());
203            } else if (value instanceof String) {
204                return Double.valueOf((String) value);
205            } else {
206                return null;
207            }
208        }
209    
210    
211    
212    }