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.io.UnsupportedEncodingException; 020 import java.util.Collection; 021 import java.util.Iterator; 022 023 import org.apache.camel.Converter; 024 import org.apache.camel.Exchange; 025 import org.apache.camel.util.ObjectHelper; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * Some core java.lang based <a 031 * href="http://camel.apache.org/type-converter.html">Type Converters</a> 032 * 033 * @version $Revision: 793862 $ 034 */ 035 @Converter 036 public final class ObjectConverter { 037 038 private static final transient Log LOG = LogFactory.getLog(ObjectConverter.class); 039 040 /** 041 * Utility classes should not have a public constructor. 042 */ 043 private ObjectConverter() { 044 } 045 046 public static boolean isCollection(Object value) { 047 // TODO we should handle primitive array types? 048 return value instanceof Collection || (value != null && value.getClass().isArray()); 049 } 050 051 /** 052 * Converts the given value to a boolean, handling strings or Boolean 053 * objects; otherwise returning false if the value could not be converted to 054 * a boolean 055 */ 056 @Converter 057 public static boolean toBool(Object value) { 058 Boolean answer = toBoolean(value); 059 return answer != null && answer; 060 } 061 062 /** 063 * Converts the given value to a Boolean, handling strings or Boolean 064 * objects; otherwise returning null if the value cannot be converted to a 065 * boolean 066 */ 067 @Converter 068 public static Boolean toBoolean(Object value) { 069 return ObjectHelper.toBoolean(value); 070 } 071 072 /** 073 * Returns the boolean value, or null if the value is null 074 */ 075 @Converter 076 public static Boolean toBoolean(Boolean value) { 077 if (value != null) { 078 return value; 079 } 080 return Boolean.FALSE; 081 } 082 083 /** 084 * Creates an iterator over the value 085 */ 086 @SuppressWarnings("unchecked") 087 @Converter 088 public static Iterator iterator(Object value) { 089 return ObjectHelper.createIterator(value); 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 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, Exchange exchange) { 112 byte[] bytes = null; 113 if (exchange != null) { 114 String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class); 115 if (charsetName != null) { 116 try { 117 bytes = value.getBytes(charsetName); 118 } catch (UnsupportedEncodingException e) { 119 LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e); 120 } 121 } 122 } 123 if (bytes == null) { 124 bytes = value.getBytes(); 125 } 126 127 return bytes; 128 } 129 130 @Converter 131 public static char[] toCharArray(String value) { 132 return value.toCharArray(); 133 } 134 135 @Converter 136 public static String fromCharArray(char[] value) { 137 return new String(value); 138 } 139 140 /** 141 * Returns the converted value, or null if the value is null 142 */ 143 @Converter 144 public static Short toShort(Object value) { 145 if (value instanceof Short) { 146 return (Short) value; 147 } else if (value instanceof Number) { 148 Number number = (Number) value; 149 return number.shortValue(); 150 } else if (value instanceof String) { 151 return Short.valueOf((String) value); 152 } else { 153 return null; 154 } 155 } 156 157 /** 158 * Returns the converted value, or null if the value is null 159 */ 160 @Converter 161 public static Integer toInteger(Object value) { 162 if (value instanceof Integer) { 163 return (Integer) value; 164 } else if (value instanceof Number) { 165 Number number = (Number) value; 166 return number.intValue(); 167 } else if (value instanceof String) { 168 return Integer.valueOf((String) value); 169 } else { 170 return null; 171 } 172 } 173 174 /** 175 * Returns the converted value, or null if the value is null 176 */ 177 @Converter 178 public static Long toLong(Object value) { 179 if (value instanceof Long) { 180 return (Long) value; 181 } else if (value instanceof Number) { 182 Number number = (Number) value; 183 return number.longValue(); 184 } else if (value instanceof String) { 185 return Long.valueOf((String) value); 186 } else { 187 return null; 188 } 189 } 190 191 /** 192 * Returns the converted value, or null if the value is null 193 */ 194 @Converter 195 public static Float toFloat(Object value) { 196 if (value instanceof Float) { 197 return (Float) value; 198 } else if (value instanceof Number) { 199 Number number = (Number) value; 200 return number.floatValue(); 201 } else if (value instanceof String) { 202 return Float.valueOf((String) value); 203 } else { 204 return null; 205 } 206 } 207 208 /** 209 * Returns the converted value, or null if the value is null 210 */ 211 @Converter 212 public static Double toDouble(Object value) { 213 if (value instanceof Double) { 214 return (Double) value; 215 } else if (value instanceof Number) { 216 Number number = (Number) value; 217 return number.doubleValue(); 218 } else if (value instanceof String) { 219 return Double.valueOf((String) value); 220 } else { 221 return null; 222 } 223 } 224 225 @Converter 226 public static String toString(Integer value) { 227 return value.toString(); 228 } 229 230 @Converter 231 public static String toString(Long value) { 232 return value.toString(); 233 } 234 235 @Converter 236 public static String toString(Boolean value) { 237 return value.toString(); 238 } 239 240 }