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 <T> T convertTo(Class<T> type, Object value) throws TypeConversionException { 034 return convertTo(type, null, value); 035 } 036 037 @Override 038 public <T> T mandatoryConvertTo(Class<T> type, Object value) throws TypeConversionException, NoTypeConversionAvailableException { 039 T t = convertTo(type, null, value); 040 if (t == null) { 041 throw new NoTypeConversionAvailableException(value, type); 042 } else { 043 return t; 044 } 045 } 046 047 @Override 048 public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException, NoTypeConversionAvailableException { 049 T t = convertTo(type, exchange, value); 050 if (t == null) { 051 throw new NoTypeConversionAvailableException(value, type); 052 } else { 053 return t; 054 } 055 } 056 057 @Override 058 public <T> T tryConvertTo(Class<T> type, Object value) { 059 try { 060 return convertTo(type, null, value); 061 } catch (Exception e) { 062 // ignore 063 } 064 return null; 065 } 066 067 @Override 068 public <T> T tryConvertTo(Class<T> type, Exchange exchange, Object value) { 069 try { 070 return convertTo(type, exchange, value); 071 } catch (Exception e) { 072 // ignore 073 } 074 return null; 075 } 076 }