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.support;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.NoTypeConversionAvailableException;
021    import org.apache.camel.TypeConversionException;
022    import org.apache.camel.TypeConverter;
023    
024    /**
025     * Base class for {@link TypeConverter} implementations.
026     * <p/>
027     * Implementators need only to implement the {@link TypeConverter#convertTo(Class, org.apache.camel.Exchange, Object)}
028     * method, and can rely on the default implementations of the other methods from this support class.
029     */
030    public abstract class TypeConverterSupport implements TypeConverter {
031    
032        @Override
033        public boolean allowNull() {
034            return false;
035        }
036    
037        @Override
038        public <T> T convertTo(Class<T> type, Object value) throws TypeConversionException {
039            return convertTo(type, null, value);
040        }
041    
042        @Override
043        public <T> T mandatoryConvertTo(Class<T> type, Object value) throws TypeConversionException, NoTypeConversionAvailableException {
044            T t = convertTo(type, null, value);
045            if (t == null) {
046                throw new NoTypeConversionAvailableException(value, type);
047            } else {
048                return t;
049            }
050        }
051    
052        @Override
053        public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException, NoTypeConversionAvailableException {
054            T t = convertTo(type, exchange, value);
055            if (t == null) {
056                throw new NoTypeConversionAvailableException(value, type);
057            } else {
058                return t;
059            }
060        }
061    
062        @Override
063        public <T> T tryConvertTo(Class<T> type, Object value) {
064            try {
065                return convertTo(type, null, value);
066            } catch (Exception e) {
067                // ignore
068            }
069            return null;
070        }
071    
072        @Override
073        public <T> T tryConvertTo(Class<T> type, Exchange exchange, Object value) {
074            try {
075                return convertTo(type, exchange, value);
076            } catch (Exception e) {
077                // ignore
078            }
079            return null;
080        }
081    }