com.abstractics.utils.converter
Class DefaultingConverterWrapper
java.lang.Object
com.abstractics.utils.converter.DefaultingConverterWrapper
- All Implemented Interfaces:
- ObjectConverter
- public class DefaultingConverterWrapper
- extends java.lang.Object
- implements ObjectConverter
An instance of ObjectConverter that can wrap another ObjectConverter and
provide a default value if the wrapped converter's conversion fails. An
example is the ConverterImageIcon
class that is often wrapped to provide a default "missing image" icon when
conversion fails.
ConvertObject.register(new DefaultingConverterWrapper(new ConverterImageIcon(),
someImage));
When an object is asked to be converted, it is passed to the wrapped
converter's
com.abstractics.utils.converter.ObjectConverter#convert(Object)
method. If that method throws an ObjectConverterException then the default
value is returned, instead of throwing the exception.
NOTE: For obvious reasons, it is important that the default
value is of the correct class type for the wrapped converter.
- Version:
- $Id: DefaultingConverterWrapper.java,v 1.1 2005/10/26 15:18:21
andrew Exp $
- Author:
- Andrew Lawrence, Copyright 2006 Abstractics, L.L.C., Licensed under the Apache
License, Version 2.0
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DefaultingConverterWrapper
public DefaultingConverterWrapper(ObjectConverter wrappedConverter,
java.lang.Object defaultValue)
- Defaults logging to true
- Parameters:
wrappedConverter - The ObjectConverter object to wrap, intercepting its callsdefaultValue - The value to return if conversion results in an
ObjectConverterException. Make sure this object is an instance
of the correct class.
getClassToConvertTo
public java.lang.Class getClassToConvertTo()
- Specified by:
getClassToConvertTo in interface ObjectConverter
- Returns:
- The class that this converter converts to
convert
public java.lang.Object convert(java.lang.Object obj,
java.util.Locale locale)
throws ObjectConverterException
- Specified by:
convert in interface ObjectConverter
- Parameters:
obj - Object to be converted. Guaranteed not to be null and not to
already be an instanceof the Class returned by
ObjectConverter.getClassToConvertTo(). Those conditions are checked
before the converter is even called.locale - The locale that is asking for the conversion. This may or may
not be necessary for the conversion.
- Returns:
- The converted object - should NEVER return anything that is not
an instanceof the class returned by
ObjectConverter.getClassToConvertTo()
- Throws:
ObjectConverterException - Throw this exception if the string cannot be converted, for
whatever reason.