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://camel.apache.org/type-converter.html">Type Converters</a>
028 *
029 * @version $Revision: 835531 $
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 return value instanceof Collection || (value != null && value.getClass().isArray());
042 }
043
044 /**
045 * Converts the given value to a boolean, handling strings or Boolean
046 * objects; otherwise returning false if the value could not be converted to
047 * a boolean
048 */
049 @Converter
050 public static boolean toBool(Object value) {
051 Boolean answer = toBoolean(value);
052 return answer != null && answer;
053 }
054
055 /**
056 * Converts the given value to a Boolean, handling strings or Boolean
057 * objects; otherwise returning null if the value cannot be converted to a
058 * boolean
059 */
060 @Converter
061 public static Boolean toBoolean(Object value) {
062 return ObjectHelper.toBoolean(value);
063 }
064
065 /**
066 * Creates an iterator over the value
067 */
068 @SuppressWarnings("unchecked")
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 String fromCharArray(char[] value) {
099 return new String(value);
100 }
101
102 /**
103 * Returns the converted value, or null if the value is null
104 */
105 @Converter
106 public static Short toShort(Object value) {
107 if (value instanceof Short) {
108 return (Short) value;
109 } else if (value instanceof Number) {
110 Number number = (Number) value;
111 return number.shortValue();
112 } else if (value instanceof String) {
113 return Short.valueOf((String) value);
114 } else {
115 return null;
116 }
117 }
118
119 /**
120 * Returns the converted value, or null if the value is null
121 */
122 @Converter
123 public static Integer toInteger(Object value) {
124 if (value instanceof Integer) {
125 return (Integer) value;
126 } else if (value instanceof Number) {
127 Number number = (Number) value;
128 return number.intValue();
129 } else if (value instanceof String) {
130 return Integer.valueOf((String) value);
131 } else {
132 return null;
133 }
134 }
135
136 /**
137 * Returns the converted value, or null if the value is null
138 */
139 @Converter
140 public static Long toLong(Object value) {
141 if (value instanceof Long) {
142 return (Long) value;
143 } else if (value instanceof Number) {
144 Number number = (Number) value;
145 return number.longValue();
146 } else if (value instanceof String) {
147 return Long.valueOf((String) value);
148 } else {
149 return null;
150 }
151 }
152
153 /**
154 * Returns the converted value, or null if the value is null
155 */
156 @Converter
157 public static Float toFloat(Object value) {
158 if (value instanceof Float) {
159 return (Float) value;
160 } else if (value instanceof Number) {
161 Number number = (Number) value;
162 return number.floatValue();
163 } else if (value instanceof String) {
164 return Float.valueOf((String) value);
165 } else {
166 return null;
167 }
168 }
169
170 /**
171 * Returns the converted value, or null if the value is null
172 */
173 @Converter
174 public static Double toDouble(Object value) {
175 if (value instanceof Double) {
176 return (Double) value;
177 } else if (value instanceof Number) {
178 Number number = (Number) value;
179 return number.doubleValue();
180 } else if (value instanceof String) {
181 return Double.valueOf((String) value);
182 } else {
183 return null;
184 }
185 }
186
187 @Converter
188 public static String toString(Integer value) {
189 return value.toString();
190 }
191
192 @Converter
193 public static String toString(Long value) {
194 return value.toString();
195 }
196
197 @Converter
198 public static String toString(Boolean value) {
199 return value.toString();
200 }
201
202 }