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.impl.converter;
018
019import java.util.Map;
020
021import org.apache.camel.Exchange;
022import org.apache.camel.converter.IOConverterOptimised;
023import org.apache.camel.converter.NIOConverterOptimised;
024import org.apache.camel.converter.ObjectConverterOptimised;
025import org.apache.camel.converter.TimePatternConverterOptimised;
026
027/**
028 * Optimised type converter for performing the most common conversions using the type converters
029 * from camel-core.
030 * <p/>
031 * The most commonly used type converters has been optimised to be invoked in a faster by
032 * using direct method calls instead of a calling via a reflection method call via
033 * {@link InstanceMethodTypeConverter} or {@link StaticMethodTypeConverter}.
034 * In addition the performance is faster because the type converter is not looked up
035 * via a key in the type converter {@link Map}; which requires creating a new object
036 * as they key and perform the map lookup. The caveat is that for any new type converter
037 * to be included it must be manually added by adding the nessasary source code to the
038 * optimised classes such as {@link ObjectConverterOptimised}.
039 */
040public class OptimisedTypeConverter {
041
042    private final EnumTypeConverter enumTypeConverter = new EnumTypeConverter();
043
044    /**
045     * Attempts to convert the value to the given type
046     *
047     * @param type     the type to convert to
048     * @param exchange the exchange, may be <tt>null</tt>
049     * @param value    the value
050     * @return the converted value, or <tt>null</tt> if no optimised core type converter exists to convert
051     */
052    public Object convertTo(final Class<?> type, final Exchange exchange, final Object value) throws Exception {
053        Object answer;
054
055        // use the optimised type converters and use them in the most commonly used order
056
057        // we need time pattern first as it can do a special String -> long conversion which should happen first
058        answer = TimePatternConverterOptimised.convertTo(type, exchange, value);
059        if (answer == null) {
060            answer = ObjectConverterOptimised.convertTo(type, exchange, value);
061        }
062        if (answer == null) {
063            answer = IOConverterOptimised.convertTo(type, exchange, value);
064        }
065        if (answer == null) {
066            answer = NIOConverterOptimised.convertTo(type, exchange, value);
067        }
068
069        // specially optimised for enums
070        if (answer == null && type.isEnum()) {
071            answer = enumTypeConverter.convertTo(type, exchange, value);
072        }
073
074        return answer;
075    }
076
077}