Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2004-2008 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.binding.convert.converters;
Base class for converters provided as a convenience to implementors.

Author(s):
Keith Donald
public abstract class AbstractConverter implements Converter {

Convenience convert method that converts the provided source to the first target object supported by this converter. Useful when a converter only supports conversion to a single target.

Parameters:
source the source to convert
Returns:
the converted object
Throws:
org.springframework.binding.convert.ConversionException an exception occured converting the source value
	public Object convert(Object sourcethrows ConversionException {
		return convert(sourcegetTargetClasses()[0], null);
	}

Convenience convert method that converts the provided source to the target class specified with an empty conversion context.

Parameters:
source the source to convert
targetClass the target class to convert the source to, must be one of the supported targetClasses
Returns:
the converted object
Throws:
org.springframework.binding.convert.ConversionException an exception occured converting the source value
	public Object convert(Object sourceClass targetClassthrows ConversionException {
		return convert(sourcetargetClassnull);
	}

Convenience convert method that converts the provided source to the first target object supported by this converter. Useful when a converter only supports conversion to a single target.

Parameters:
source the source to convert
context the conversion context, useful for influencing the behavior of the converter
Returns:
the converted object
Throws:
org.springframework.binding.convert.ConversionException an exception occured converting the source value
	public Object convert(Object sourceObject contextthrows ConversionException {
		return convert(sourcegetTargetClasses()[0], context);
	}
	public Object convert(Object sourceClass targetClassObject contextthrows ConversionException {
		try {
			return doConvert(sourcetargetClasscontext);
catch (ConversionException e) {
			throw e;
catch (Throwable e) {
			// wrap in a ConversionException
			if (targetClass == null) {
				targetClass = getTargetClasses()[0];
			}
			throw new ConversionException(sourcetargetClasse);
		}
	}

Template method subclasses should override to actually perform the type conversion.

Parameters:
source the source to convert from
targetClass the target type to convert to
context an optional conversion context that may be used to influence the conversion process, could be null
Returns:
the converted source value
Throws:
java.lang.Exception an exception occurred, will be wrapped in a conversion exception if necessary
	protected abstract Object doConvert(Object sourceClass targetClassObject contextthrows Exception;
New to GrepCode? Check out our FAQ X