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.Iterator;
020
021import org.apache.camel.Exchange;
022import org.apache.camel.util.ObjectHelper;
023
024/**
025 * Optimised {@link ObjectConverter}
026 */
027public final class ObjectConverterOptimised {
028
029    private ObjectConverterOptimised() {
030    }
031
032    public static Object convertTo(final Class<?> type, final Exchange exchange, final Object value) throws Exception {
033        // converting to a String is very common
034        if (type == String.class) {
035            Class fromType = value.getClass();
036            if (fromType == boolean.class || fromType == Boolean.class) {
037                return value.toString();
038            } else if (fromType == int.class || fromType == Integer.class) {
039                return value.toString();
040            } else if (fromType == long.class || fromType == Long.class) {
041                return value.toString();
042            } else if (fromType == char[].class) {
043                return ObjectConverter.fromCharArray((char[]) value);
044            } else if (fromType == StringBuffer.class || fromType == StringBuilder.class) {
045                return value.toString();
046            }
047            return null;
048        }
049
050        if (type == boolean.class || type == Boolean.class) {
051            return ObjectConverter.toBoolean(value);
052        } else if (type == int.class || type == Integer.class) {
053            return ObjectConverter.toInteger(value);
054        } else if (type == long.class || type == Long.class) {
055            return ObjectConverter.toLong(value);
056        } else if (type == byte.class || type == Byte.class) {
057            return ObjectConverter.toByte(value);
058        } else if (type == double.class || type == Double.class) {
059            return ObjectConverter.toDouble(value);
060        } else if (type == float.class || type == Float.class) {
061            return ObjectConverter.toFloat(value);
062        } else if (type == short.class || type == Short.class) {
063            return ObjectConverter.toShort(value);
064        } else if ((type == char.class || type == Character.class) && value.getClass() == String.class) {
065            return ObjectConverter.toCharacter((String) value);
066        } else if ((type == char[].class || type == Character[].class) && value.getClass() == String.class) {
067            return ObjectConverter.toCharArray((String) value);
068        }
069
070        if (type == Iterator.class) {
071            return ObjectHelper.createIterator(value);
072        } else if (type == Iterable.class) {
073            return ObjectHelper.createIterable(value);
074        }
075
076        if (type == Class.class) {
077            return ObjectConverter.toClass(value, exchange);
078        }
079
080        // no optimised type converter found
081        return null;
082    }
083
084}