001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.converter;
019
020 import org.apache.camel.Converter;
021
022 import java.util.Arrays;
023 import java.util.Collection;
024 import java.util.Collections;
025 import java.util.Iterator;
026
027 /**
028 * Some core java.lang based
029 * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
030 *
031 * @version $Revision: 544115 $
032 */
033 @Converter
034 public class ObjectConverter {
035 public static boolean isCollection(Object value) {
036 // TODO we should handle primitive array types?
037 return value instanceof Collection || (value != null && value.getClass().isArray());
038 }
039
040 /**
041 * Creates an iterator over the value if the value is a collection, an Object[] or a primitive type array; otherwise
042 * to simplify the caller's code, we just create a singleton collection iterator over a single value
043 */
044 @Converter
045 public static Iterator iterator(Object value) {
046 if (value == null) {
047 return Collections.EMPTY_LIST.iterator();
048 }
049 else if (value instanceof Collection) {
050 Collection collection = (Collection) value;
051 return collection.iterator();
052 }
053 else if (value.getClass().isArray()) {
054 // TODO we should handle primitive array types?
055 return Arrays.asList(value).iterator();
056 }
057 else {
058 return Collections.singletonList(value).iterator();
059 }
060 }
061
062 /**
063 * Converts the given value to a boolean, handling strings or Boolean objects;
064 * otherwise returning false if the value could not be converted to a boolean
065 */
066 @Converter
067 public static boolean toBool(Object value) {
068 Boolean answer = toBoolean(value);
069 if (answer != null) {
070 return answer.booleanValue();
071 }
072 return false;
073 }
074
075 /**
076 * Converts the given value to a Boolean, handling strings or Boolean objects;
077 * otherwise returning null if the value cannot be converted to a boolean
078 */
079 @Converter
080 public static Boolean toBoolean(Object value) {
081 if (value instanceof Boolean) {
082 return (Boolean) value;
083 }
084 if (value instanceof String) {
085 return "true".equalsIgnoreCase(value.toString()) ? Boolean.TRUE : Boolean.FALSE;
086 }
087 return null;
088 }
089 }