2.17.2: 2011-06-17

net.sf.basedb.util.formatter
Class LookupFormatter<T>

java.lang.Object
  extended by net.sf.basedb.util.formatter.LookupFormatter<T>
All Implemented Interfaces:
Formatter<T>

public class LookupFormatter<T>
extends Object
implements Formatter<T>

A formatter implementation that uses a map to lookup values from a set of keys. A default value may be specified that is returned in case no lookup has been defined for a given key.

Version:
2.12
Author:
nicklas
Last modified
$Date: 2009-03-27 12:47:31 +0100 (Fri, 27 Mar 2009) $

Field Summary
private  String defaultValue
           
private  Map<T,String> map
           
 
Constructor Summary
LookupFormatter()
          Create a new lookup formatter
LookupFormatter(Enumeration<? extends T,? extends String> in)
          Create a new lookup formatter and initialise it with the key-value pairs in the given enumeration.
LookupFormatter(Map<? extends T,? extends String> in)
          Create a new lookup formatter and initialise it with the key-value pairs in the given map.
 
Method Summary
 void clear()
          Remove all registered lookup entries.
 boolean containsKey(Object key)
          Checks if a given key has been registered.
 String format(T key)
          Format a value by converting it to a string.
 String get(Object key)
          Get registered lookup value for a given key
 String getDefaultValue()
          Get the default value.
 boolean isEmpty()
          Checks if the lookup map is empty or not.
 T parseString(String value)
          Parse a string and return a value of the correct type.
 void put(T key, String value)
          Add a lookup key/value pair.
 void putAll(Enumeration<? extends T,? extends String> in)
          Copy multiple lookup key/value pairs from an enumeration.
 void putAll(Map<? extends T,? extends String> in)
          Copy multiple lookup key/value pairs from a map
 String remove(Object key)
          Remove an entry from the lookup map.
 void setDefaultValue(String defaultValue)
          Set a default value that is returned by the format(Object) method in case no lookup has been specified for a key.
 int size()
          Get the number of registered lookup entries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

private final Map<T,String> map

defaultValue

private String defaultValue
Constructor Detail

LookupFormatter

public LookupFormatter()
Create a new lookup formatter


LookupFormatter

public LookupFormatter(Map<? extends T,? extends String> in)
Create a new lookup formatter and initialise it with the key-value pairs in the given map. The key value pairs are copied to a new map internal to the lookup formatter.

Parameters:
in - A map object or null

LookupFormatter

public LookupFormatter(Enumeration<? extends T,? extends String> in)
Create a new lookup formatter and initialise it with the key-value pairs in the given enumeration. The key value pairs are copied to a new map internal to the lookup formatter.

Parameters:
in - An enumeration object or null
Method Detail

format

public String format(T key)
Description copied from interface: Formatter
Format a value by converting it to a string.

Specified by:
format in interface Formatter<T>
Parameters:
key - The value to format
Returns:
A formatted string

parseString

public T parseString(String value)
Description copied from interface: Formatter
Parse a string and return a value of the correct type. A null input string should (unless otherwise documented by a specific formatter implementation) be allowed and may return null or any other "default" value.

Specified by:
parseString in interface Formatter<T>
Parameters:
value - The string to parse
Returns:
An object

setDefaultValue

public void setDefaultValue(String defaultValue)
Set a default value that is returned by the format(Object) method in case no lookup has been specified for a key.


getDefaultValue

public String getDefaultValue()
Get the default value.


put

public void put(T key,
                String value)
Add a lookup key/value pair.

Parameters:
key - The key
value - The value that is returned as the lookup value for the given key

putAll

public void putAll(Map<? extends T,? extends String> in)
Copy multiple lookup key/value pairs from a map

Parameters:
in - The map to copy key/value pair from

putAll

public void putAll(Enumeration<? extends T,? extends String> in)
Copy multiple lookup key/value pairs from an enumeration.

Parameters:
in - The enumeration to copy key/value pair from

containsKey

public boolean containsKey(Object key)
Checks if a given key has been registered.

Parameters:
key - The key
Returns:
TRUE if an entry exists for the key, or FALSE otherwise

get

public String get(Object key)
Get registered lookup value for a given key

Parameters:
key - The key
Returns:
The registered lookup value, or null if no value has been registered

size

public int size()
Get the number of registered lookup entries.


isEmpty

public boolean isEmpty()
Checks if the lookup map is empty or not.


clear

public void clear()
Remove all registered lookup entries.


remove

public String remove(Object key)
Remove an entry from the lookup map.

Parameters:
key - The key
Returns:
The value that was associated with the key

2.17.2: 2011-06-17