org.geotoolkit.util.converter
Class AnyConverter

Object
  extended by AnyConverter

@NotThreadSafe
public class AnyConverter
extends Object

An utility for converting arbitrary objects to arbitrary target types without the need to handle ObjectConverter and ConverterRegistry explicitly. This is a convenience class for converting a few instances of unknown type.

Note: If a large amount of objects of the same type need to be converted to the same target type, or if the source type is known at compile-time, then it is often more advisable to use ObjectConverter directly.


Thread safety
This class is not thread safe. If conversions need to be performed by concurrent threads, then each thread shall use its own AnyConverter instance.

Since:
3.04
Version:
3.04
Author:
Martin Desruisseaux (Geomatys)
Module:
utility/geotk-utility (download)    View source code for this class

Constructor Summary
AnyConverter()
          Creates a new AnyConverter which will use the system converter registry.
AnyConverter(ConverterRegistry registry)
          Creates a new AnyConverter which will use the given converter registry.
 
Method Summary
protected  void conversionFailed(NonconvertibleObjectException exception)
          Invoked by tryConvert when an object can not be converted.
<T> T
convert(Object value, Class<T> targetType)
          Converts the given value to the given type.
 Object tryConvert(Object value, Class<?> targetType)
          Tries to convert the given value to the given type.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnyConverter

public AnyConverter()
Creates a new AnyConverter which will use the system converter registry.


AnyConverter

public AnyConverter(ConverterRegistry registry)
Creates a new AnyConverter which will use the given converter registry.

Parameters:
registry - The converter registry to use, or null for the system one.
Method Detail

convert

public <T> T convert(Object value,
                     Class<T> targetType)
          throws NonconvertibleObjectException
Converts the given value to the given type.

Type Parameters:
T - The parameterized type of targetType.
Parameters:
value - The value to convert (can be null).
targetType - The desired type.
Returns:
The given object converted to the given type.
Throws:
NonconvertibleObjectException - If the conversion can not be performed.

tryConvert

public Object tryConvert(Object value,
                         Class<?> targetType)
Tries to convert the given value to the given type. If the conversion fail, then this method invokes conversionFailed and returns the value unchanged.

Parameters:
value - The value to convert (can be null).
targetType - The desired type.
Returns:
The given object converted to the given type.

conversionFailed

protected void conversionFailed(NonconvertibleObjectException exception)
Invoked by tryConvert when an object can not be converted. The default implementation logs the exception at the FINE level. Subclasses can override this method if they want to logs the message in a different way.

Parameters:
exception - The exception that occurred while trying to convert an object.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.