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: 747062 $ 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 * Converts the given value to a boolean, handling strings or Boolean 047 * objects; otherwise returning false if the value could not be converted to 048 * a boolean 049 */ 050 @Converter 051 public static boolean toBool(Object value) { 052 Boolean answer = toBoolean(value); 053 return answer != null && answer; 054 } 055 056 /** 057 * Converts the given value to a Boolean, handling strings or Boolean 058 * objects; otherwise returning null if the value cannot be converted to a 059 * boolean 060 */ 061 @Converter 062 public static Boolean toBoolean(Object value) { 063 return ObjectHelper.toBoolean(value); 064 } 065 066 /** 067 * Returns the boolean value, or null if the value is null 068 */ 069 @Converter 070 public static Boolean toBoolean(Boolean value) { 071 if (value != null) { 072 return value; 073 } 074 return Boolean.FALSE; 075 } 076 077 /** 078 * Creates an iterator over the value 079 */ 080 @SuppressWarnings("unchecked") 081 @Converter 082 public static Iterator iterator(Object value) { 083 return ObjectHelper.createIterator(value); 084 } 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 byte[] toByteArray(String value) { 106 return value.getBytes(); 107 } 108 109 @Converter 110 public static char[] toCharArray(String value) { 111 return value.toCharArray(); 112 } 113 114 @Converter 115 public static String fromCharArray(char[] value) { 116 return new String(value); 117 } 118 119 /** 120 * Returns the converted value, or null if the value is null 121 */ 122 @Converter 123 public static Short toShort(Object value) { 124 if (value instanceof Short) { 125 return (Short) value; 126 } else if (value instanceof Number) { 127 Number number = (Number) value; 128 return number.shortValue(); 129 } else if (value instanceof String) { 130 return Short.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 Integer toInteger(Object value) { 141 if (value instanceof Integer) { 142 return (Integer) value; 143 } else if (value instanceof Number) { 144 Number number = (Number) value; 145 return number.intValue(); 146 } else if (value instanceof String) { 147 return Integer.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 Long toLong(Object value) { 158 if (value instanceof Long) { 159 return (Long) value; 160 } else if (value instanceof Number) { 161 Number number = (Number) value; 162 return number.longValue(); 163 } else if (value instanceof String) { 164 return Long.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 Float toFloat(Object value) { 175 if (value instanceof Float) { 176 return (Float) value; 177 } else if (value instanceof Number) { 178 Number number = (Number) value; 179 return number.floatValue(); 180 } else if (value instanceof String) { 181 return Float.valueOf((String) value); 182 } else { 183 return null; 184 } 185 } 186 187 /** 188 * Returns the converted value, or null if the value is null 189 */ 190 @Converter 191 public static Double toDouble(Object value) { 192 if (value instanceof Double) { 193 return (Double) value; 194 } else if (value instanceof Number) { 195 Number number = (Number) value; 196 return number.doubleValue(); 197 } else if (value instanceof String) { 198 return Double.valueOf((String) value); 199 } else { 200 return null; 201 } 202 } 203 204 }