wicket.util.value
Class ValueMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by wicket.util.value.ValueMap
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map
Direct Known Subclasses:
AttributeMap, PageParameters

public class ValueMap
extends java.util.HashMap

A Map implementation that holds values, parses strings and exposes a variety of convenience methods.

In addition to a no-arg constructor and a copy constructor that takes a Map argument, ValueMaps can be constructed using a parsing constructor. ValueMap(String) will parse values from the string in comma separated key/value assignment pairs. For example, new ValueMap("a=9,b=foo").

Values can be retrieved from the map in the usual way or with methods that do handy conversions to various types, including String, StringValue, int, long, double, Time and Duration.

The makeImmutable method will make the underlying map immutable. Further attempts to change the map will result in a runtime exception.

The toString() method converts a ValueMap object to a readable key/value string for diagnostics.

Author:
Jonathan Locke
See Also:
Serialized Form

Field Summary
static ValueMap EMPTY_MAP
          An empty ValueMap.
 
Constructor Summary
ValueMap()
          Constructs empty value map.
ValueMap(java.util.Map map)
          Copy constructor.
ValueMap(java.lang.String keyValuePairs)
          Constructor.
ValueMap(java.lang.String keyValuePairs, java.lang.String delimiter)
          Constructor.
ValueMap(java.lang.String keyValuePairs, java.lang.String delimiter, MetaPattern valuePattern)
          Constructor.
 
Method Summary
 java.lang.Object add(java.lang.String key, java.lang.String value)
          This methods adds the value to this map under the given key If the key already is in the map it will combine the values into a String array else it will just store the value itself
 void clear()
           
 boolean getBoolean(java.lang.String key)
          Gets a boolean value by key.
 java.lang.CharSequence getCharSequence(java.lang.String key)
          Gets a string by key.
 double getDouble(java.lang.String key)
          Gets a double value by key.
 double getDouble(java.lang.String key, double defaultValue)
          Gets a double using a default if not found.
 Duration getDuration(java.lang.String key)
          Gets a duration.
 int getInt(java.lang.String key)
          Gets an int.
 int getInt(java.lang.String key, int defaultValue)
          Gets an int, using a default if not found.
 java.lang.String getKey(java.lang.String key)
          Provided the hash key is a string and you need to access the value ignoring ignoring the keys case (upper or lower letter), than you may use this method to get the correct writing.
 long getLong(java.lang.String key)
          Gets a long.
 long getLong(java.lang.String key, long defaultValue)
          Gets a long using a default if not found.
 java.lang.String getString(java.lang.String key)
          Gets a string by key.
 java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
          Gets a string by key.
 java.lang.String[] getStringArray(java.lang.String key)
          Gets a String array by key.
 StringValue getStringValue(java.lang.String key)
          Gets a StringValue by key.
 Time getTime(java.lang.String key)
          Gets a time.
 boolean isImmutable()
          Gets whether this value map is made immutable.
 void makeImmutable()
          Makes this value map immutable by changing the underlying map representation to a collections "unmodifiableMap".
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
           
 void putAll(java.util.Map map)
           
 java.lang.Object remove(java.lang.Object key)
           
 java.lang.String toString()
          Gets a string representation of this object
 
Methods inherited from class java.util.HashMap
clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

EMPTY_MAP

public static final ValueMap EMPTY_MAP
An empty ValueMap.

Constructor Detail

ValueMap

public ValueMap()
Constructs empty value map.


ValueMap

public ValueMap(java.util.Map map)
Copy constructor.

Parameters:
map - The map to copy

ValueMap

public ValueMap(java.lang.String keyValuePairs)
Constructor.

Parameters:
keyValuePairs - List of key value pairs separated by commas. For example, "param1=foo,param2=bar"

ValueMap

public ValueMap(java.lang.String keyValuePairs,
                java.lang.String delimiter)
Constructor.

Parameters:
keyValuePairs - List of key value pairs separated by a given delimiter. For example, "param1=foo,param2=bar" where delimiter is ",".
delimiter - Delimiter string used to separate key/value pairs

ValueMap

public ValueMap(java.lang.String keyValuePairs,
                java.lang.String delimiter,
                MetaPattern valuePattern)
Constructor.

Parameters:
keyValuePairs - List of key value pairs separated by a given delimiter. For example, "param1=foo,param2=bar" where delimiter is ",".
delimiter - Delimiter string used to separate key/value pairs
valuePattern - Pattern for value. To pass a simple regular expression pass "new MetaPattern(regexp)".
Method Detail

clear

public final void clear()
Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.HashMap
See Also:
Map.clear()

getBoolean

public final boolean getBoolean(java.lang.String key)
                         throws StringValueConversionException
Gets a boolean value by key.

Parameters:
key - The key
Returns:
The value
Throws:
StringValueConversionException

getDouble

public final double getDouble(java.lang.String key)
                       throws StringValueConversionException
Gets a double value by key.

Parameters:
key - The key
Returns:
The value
Throws:
StringValueConversionException

getDouble

public final double getDouble(java.lang.String key,
                              double defaultValue)
                       throws StringValueConversionException
Gets a double using a default if not found.

Parameters:
key - The key
defaultValue - Value to use if no value in map
Returns:
The value
Throws:
StringValueConversionException

getDuration

public final Duration getDuration(java.lang.String key)
                           throws StringValueConversionException
Gets a duration.

Parameters:
key - The key
Returns:
The value
Throws:
StringValueConversionException

getInt

public final int getInt(java.lang.String key)
                 throws StringValueConversionException
Gets an int.

Parameters:
key - The key
Returns:
The value
Throws:
StringValueConversionException

getInt

public final int getInt(java.lang.String key,
                        int defaultValue)
                 throws StringValueConversionException
Gets an int, using a default if not found.

Parameters:
key - The key
defaultValue - Value to use if no value in map
Returns:
The value
Throws:
StringValueConversionException

getLong

public final long getLong(java.lang.String key)
                   throws StringValueConversionException
Gets a long.

Parameters:
key - The key
Returns:
The value
Throws:
StringValueConversionException

getLong

public final long getLong(java.lang.String key,
                          long defaultValue)
                   throws StringValueConversionException
Gets a long using a default if not found.

Parameters:
key - The key
defaultValue - Value to use if no value in map
Returns:
The value
Throws:
StringValueConversionException

getString

public final java.lang.String getString(java.lang.String key,
                                        java.lang.String defaultValue)
Gets a string by key.

Parameters:
key - The get
defaultValue - Default value to return if value is null
Returns:
The string

getString

public final java.lang.String getString(java.lang.String key)
Gets a string by key.

Parameters:
key - The get
Returns:
The string

getCharSequence

public final java.lang.CharSequence getCharSequence(java.lang.String key)
Gets a string by key.

Parameters:
key - The get
Returns:
The string

getStringArray

public java.lang.String[] getStringArray(java.lang.String key)
Gets a String array by key. If the value was a String[] it will be returned directly. If it was a String it will be converted to a String array of one. If it was an array of another type a String array will be made and the elements will be converted to a string.

Parameters:
key -
Returns:
The String array of that key

getStringValue

public StringValue getStringValue(java.lang.String key)
Gets a StringValue by key.

Parameters:
key - The key
Returns:
The string value object

getTime

public final Time getTime(java.lang.String key)
                   throws StringValueConversionException
Gets a time.

Parameters:
key - The key
Returns:
The value
Throws:
StringValueConversionException

isImmutable

public final boolean isImmutable()
Gets whether this value map is made immutable.

Returns:
whether this value map is made immutable

makeImmutable

public final void makeImmutable()
Makes this value map immutable by changing the underlying map representation to a collections "unmodifiableMap". After calling this method, any attempt to modify this map will result in a runtime exception being thrown by the collections classes.


put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
See Also:
Map.put(java.lang.Object, java.lang.Object)

add

public final java.lang.Object add(java.lang.String key,
                                  java.lang.String value)
This methods adds the value to this map under the given key If the key already is in the map it will combine the values into a String array else it will just store the value itself

Parameters:
key - The key to store the value under.
value - The value that must be added/merged to the map
Returns:
The value itself if there was no previous value or a string array with the combined values.

putAll

public void putAll(java.util.Map map)
Specified by:
putAll in interface java.util.Map
Overrides:
putAll in class java.util.HashMap
See Also:
Map.putAll(java.util.Map)

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.HashMap
See Also:
Map.remove(java.lang.Object)

getKey

public java.lang.String getKey(java.lang.String key)
Provided the hash key is a string and you need to access the value ignoring ignoring the keys case (upper or lower letter), than you may use this method to get the correct writing.

Parameters:
key -
Returns:
The key with the correct writing

toString

public java.lang.String toString()
Gets a string representation of this object

Overrides:
toString in class java.util.AbstractMap
Returns:
String representation of map consistent with tag attribute style of markup elements, for example: a="x" b="y" c="z"


Copyright © 2004-2007 Wicket developers. All Rights Reserved.