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