com.abstractics.utils.converter
Class CachingConverterWrapper
java.lang.Object
com.abstractics.utils.converter.CachingConverterWrapper
- All Implemented Interfaces:
- ObjectConverter
- public class CachingConverterWrapper
- extends java.lang.Object
- implements ObjectConverter
An instance of ObjectConverter that can wrap another ObjectConverter and
provide caching for the objects that are converted. An example is the
ConverterImageIcon
class that is
often wrapped to provide a cache for the loaded images.
ConvertObject.register(new CachingConverterWrapper(new ConverterImageIcon()));
When an object is asked to be converted, it's toString()
method is called to determine a key for the cache. If an object is already in
the cache with that key, then it is returned. Otherwise the wrapped converter
is used to do the conversion and the result is cached with the key.
NOTE: Since the toString()
method is used to
determine keys, it becomes important that they are unique. This is often the
case beacuse Strings are the objects being converted, but be aware.
- Version:
- $Id: CachingConverterWrapper.java,v 1.2 2006/03/15 05:37:30 andyman99 Exp $
- Author:
- Andrew Lawrence, Copyright 2006 Abstractics, L.L.C., Licensed under the Apache
License, Version 2.0
Method Summary |
java.lang.Object |
convert(java.lang.Object obj,
java.util.Locale locale)
Implemented here to intercept conversion calls and check for the objects
in the cache first. |
java.lang.Class |
getClassToConvertTo()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
CachingConverterWrapper
public CachingConverterWrapper(ObjectConverter wrappedConverter)
- Parameters:
wrappedConverter
- The ObjectConverter object to wrap, intercepting its calls to
cache them
convert
public java.lang.Object convert(java.lang.Object obj,
java.util.Locale locale)
throws ObjectConverterException
- Implemented here to intercept conversion calls and check for the objects
in the cache first. The given object has its
toString()
method called to determine the key for the cache Map. If an object with
that key already exists, then it is returned. Otherwise the
wrappedConverter specified in the constructor is called to do the
conversion. The resulting object is then stored in the cache
- 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.- See Also:
com.abstractics.utils.converter.ObjectConverter#convert(java.lang.Object)
getClassToConvertTo
public java.lang.Class getClassToConvertTo()
- Specified by:
getClassToConvertTo
in interface ObjectConverter
- Returns:
- The class that this converter converts to