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 if (value instanceof String) { 204 return new BigInteger((String) value); 205 } 206 207 Long num = null; 208 if (value instanceof Long) { 209 num = (Long) value; 210 } else if (value instanceof Number) { 211 Number number = (Number) value; 212 num = number.longValue(); 213 } 214 if (num != null) { 215 return BigInteger.valueOf(num); 216 } else { 217 return null; 218 } 219 } 220 221 /** 222 * Returns the converted value, or null if the value is null 223 */ 224 @Converter 225 public static Float toFloat(Object value) { 226 if (value instanceof Float) { 227 return (Float) value; 228 } else if (value instanceof Number) { 229 if (ObjectHelper.isNaN(value)) { 230 return Float.NaN; 231 } 232 Number number = (Number) value; 233 return number.floatValue(); 234 } else if (value instanceof String) { 235 return Float.valueOf((String) value); 236 } else { 237 return null; 238 } 239 } 240 241 /** 242 * Returns the converted value, or null if the value is null 243 */ 244 @Converter 245 public static Double toDouble(Object value) { 246 if (value instanceof Double) { 247 return (Double) value; 248 } else if (value instanceof Number) { 249 if (ObjectHelper.isNaN(value)) { 250 return Double.NaN; 251 } 252 Number number = (Number) value; 253 return number.doubleValue(); 254 } else if (value instanceof String) { 255 return Double.valueOf((String) value); 256 } else { 257 return null; 258 } 259 } 260 261 // add fast type converters from most common used 262 263 @Converter 264 public static String toString(Integer value) { 265 return value.toString(); 266 } 267 268 @Converter 269 public static String toString(Long value) { 270 return value.toString(); 271 } 272 273 @Converter 274 public static String toString(Boolean value) { 275 return value.toString(); 276 } 277 278 @Converter 279 public static String toString(StringBuffer value) { 280 return value.toString(); 281 } 282 283 @Converter 284 public static String toString(StringBuilder value) { 285 return value.toString(); 286 } 287 288 @Converter 289 public static Integer toInteger(String value) { 290 return Integer.valueOf(value); 291 } 292 293 @Converter 294 public static Long toLong(String value) { 295 return Long.valueOf(value); 296 } 297 298 @Converter 299 public static Float toFloat(String value) { 300 return Float.valueOf(value); 301 } 302 303 @Converter 304 public static Double toDouble(String value) { 305 return Double.valueOf(value); 306 } 307 308 @Converter 309 public static Boolean toBoolean(String value) { 310 return Boolean.parseBoolean(value); 311 } 312 313}