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.Arrays; 020 import java.util.Collection; 021 import java.util.Collections; 022 import java.util.Iterator; 023 024 import org.apache.camel.Converter; 025 026 /** 027 * Some core java.lang based <a 028 * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> 029 * 030 * @version $Revision: 563607 $ 031 */ 032 @Converter 033 public class ObjectConverter { 034 035 /** 036 * Utility classes should not have a public constructor. 037 */ 038 private ObjectConverter() { 039 } 040 041 public static boolean isCollection(Object value) { 042 // TODO we should handle primitive array types? 043 return value instanceof Collection || (value != null && value.getClass().isArray()); 044 } 045 046 /** 047 * Creates an iterator over the value if the value is a collection, an 048 * Object[] or a primitive type array; otherwise to simplify the caller's 049 * code, we just create a singleton collection iterator over a single value 050 */ 051 @Converter 052 public static Iterator iterator(Object value) { 053 if (value == null) { 054 return Collections.EMPTY_LIST.iterator(); 055 } else if (value instanceof Collection) { 056 Collection collection = (Collection)value; 057 return collection.iterator(); 058 } else if (value.getClass().isArray()) { 059 // TODO we should handle primitive array types? 060 return Arrays.asList(value).iterator(); 061 } else { 062 return Collections.singletonList(value).iterator(); 063 } 064 } 065 066 /** 067 * Converts the given value to a boolean, handling strings or Boolean 068 * objects; otherwise returning false if the value could not be converted to 069 * a boolean 070 */ 071 @Converter 072 public static boolean toBool(Object value) { 073 Boolean answer = toBoolean(value); 074 if (answer != null) { 075 return answer.booleanValue(); 076 } 077 return false; 078 } 079 080 /** 081 * Converts the given value to a Boolean, handling strings or Boolean 082 * objects; otherwise returning null if the value cannot be converted to a 083 * boolean 084 */ 085 @Converter 086 public static Boolean toBoolean(Object value) { 087 if (value instanceof Boolean) { 088 return (Boolean)value; 089 } 090 if (value instanceof String) { 091 return "true".equalsIgnoreCase(value.toString()) ? Boolean.TRUE : Boolean.FALSE; 092 } 093 return null; 094 } 095 096 /** 097 * Returns the boolean value, or null if the value is null 098 */ 099 @Converter 100 public static Boolean toBoolean(Boolean value) { 101 if (value != null) { 102 return value.booleanValue(); 103 } 104 return false; 105 } 106 107 }