org.apache.wicket.util.value
Class ValueMap

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

public class ValueMap
extends LinkedHashMap<String,Object>
implements IValueMap

A IValueMap 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 ValueMap 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 RuntimeException.

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

Since:
1.2.6
Author:
Jonathan Locke, Doug Donohoe
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
static ValueMap EMPTY_MAP
          an empty ValueMap.
 
Constructor Summary
ValueMap()
          Constructs empty ValueMap.
ValueMap(Map<? extends String,? extends Object> map)
          Copy constructor.
ValueMap(String keyValuePairs)
          Constructor.
ValueMap(String keyValuePairs, String delimiter)
          Constructor.
ValueMap(String keyValuePairs, String delimiter, MetaPattern valuePattern)
          Constructor.
 
Method Summary
 Object add(String key, String value)
          Adds the value to this ValueMap with the given key.
 void clear()
           
 Boolean getAsBoolean(String key)
          Retrieves a Boolean value by key.
 boolean getAsBoolean(String key, boolean defaultValue)
          Retrieves a boolean value by key.
 Double getAsDouble(String key)
          Retrieves a Double value by key.
 double getAsDouble(String key, double defaultValue)
          Retrieves a double value by key.
 Duration getAsDuration(String key)
          Retrieves a Duration value by key.
 Duration getAsDuration(String key, Duration defaultValue)
          Retrieves a Duration value by key.
<T extends Enum<T>>
T
getAsEnum(String key, Class<T> eClass)
          Retrieves an Enum value by key.
<T extends Enum<T>>
T
getAsEnum(String key, Class<T> eClass, T defaultValue)
          Retrieves an Enum value by key.
<T extends Enum<T>>
T
getAsEnum(String key, T defaultValue)
          Retrieves an Enum value by key.
 Integer getAsInteger(String key)
          Retrieves an Integer value by key.
 int getAsInteger(String key, int defaultValue)
          Retrieves an integer value by key.
 Long getAsLong(String key)
          Retrieves a Long value by key.
 long getAsLong(String key, long defaultValue)
          Retrieves a long value by key.
 Time getAsTime(String key)
          Retrieves a Time value by key.
 Time getAsTime(String key, Time defaultValue)
          Retrieves a Time value by key.
 boolean getBoolean(String key)
          Retrieves a boolean value by key.
 CharSequence getCharSequence(String key)
          Retrieves a CharSequence by key.
 double getDouble(String key)
          Retrieves a double value by key.
 double getDouble(String key, double defaultValue)
          Retrieves a double value by key, using a default value if not found.
 Duration getDuration(String key)
          Retrieves a Duration by key.
 int getInt(String key)
          Retrieves an int value by key.
 int getInt(String key, int defaultValue)
          Retrieves an int value by key, using a default value if not found.
 String getKey(String key)
          Provided that the hash key is a String and you need to access the value ignoring the key's case (upper- or lowercase letters), then you may use this method to get the correct writing.
 long getLong(String key)
          Retrieves a long value by key.
 long getLong(String key, long defaultValue)
          Retrieves a long value by key, using a default value if not found.
 String getString(String key)
          Retrieves a String by key.
 String getString(String key, String defaultValue)
          Retrieves a String by key, using a default value if not found.
 String[] getStringArray(String key)
          Retrieves a String array by key.
 StringValue getStringValue(String key)
          Retrieves a StringValue object by key.
 Time getTime(String key)
          Retrieves a Time object by key.
 boolean isImmutable()
          Returns whether or not this IValueMap is immutable.
 IValueMap makeImmutable()
          Makes this IValueMap immutable by changing the underlying map representation to a Collections.unmodifiableMap.
 Object put(String key, Object value)
           
 void putAll(Map<? extends String,? extends Object> map)
           
 Object remove(Object key)
           
 String toString()
          Generates a String representation of this object.
 
Methods inherited from class java.util.LinkedHashMap
containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, 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
containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, size, values
 

Field Detail

EMPTY_MAP

public static final ValueMap EMPTY_MAP
an empty ValueMap.

Constructor Detail

ValueMap

public ValueMap()
Constructs empty ValueMap.


ValueMap

public ValueMap(Map<? extends String,? extends Object> map)
Copy constructor.

Parameters:
map - the ValueMap to copy

ValueMap

public ValueMap(String keyValuePairs)
Constructor.

NOTE: Please use RequestUtils.decodeParameters() if you wish to properly decode a request URL.

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

ValueMap

public ValueMap(String keyValuePairs,
                String delimiter)
Constructor.

NOTE: Please use RequestUtils.decodeParameters() if you wish to properly decode a request URL.

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(String keyValuePairs,
                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 Map<String,Object>
Overrides:
clear in class LinkedHashMap<String,Object>
See Also:
Map.clear()

getBoolean

public final boolean getBoolean(String key)
                         throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a boolean value by key.

Specified by:
getBoolean in interface IValueMap
Parameters:
key - the key
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getBoolean(String)

getDouble

public final double getDouble(String key)
                       throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a double value by key.

Specified by:
getDouble in interface IValueMap
Parameters:
key - the key
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getDouble(String)

getDouble

public final double getDouble(String key,
                              double defaultValue)
                       throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a double value by key, using a default value if not found.

Specified by:
getDouble in interface IValueMap
Parameters:
key - the key
defaultValue - value to use if no value is in this IValueMap
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getDouble(String, double)

getDuration

public final Duration getDuration(String key)
                           throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a Duration by key.

Specified by:
getDuration in interface IValueMap
Parameters:
key - the key
Returns:
the Duration value
Throws:
StringValueConversionException
See Also:
IValueMap.getDuration(String)

getInt

public final int getInt(String key)
                 throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves an int value by key.

Specified by:
getInt in interface IValueMap
Parameters:
key - the key
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getInt(String)

getInt

public final int getInt(String key,
                        int defaultValue)
                 throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves an int value by key, using a default value if not found.

Specified by:
getInt in interface IValueMap
Parameters:
key - the key
defaultValue - value to use if no value is in this IValueMap
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getInt(String, int)

getLong

public final long getLong(String key)
                   throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a long value by key.

Specified by:
getLong in interface IValueMap
Parameters:
key - the key
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getLong(String)

getLong

public final long getLong(String key,
                          long defaultValue)
                   throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a long value by key, using a default value if not found.

Specified by:
getLong in interface IValueMap
Parameters:
key - the key
defaultValue - value to use if no value in this IValueMap
Returns:
the value
Throws:
StringValueConversionException
See Also:
IValueMap.getLong(String, long)

getString

public final String getString(String key,
                              String defaultValue)
Description copied from interface: IValueMap
Retrieves a String by key, using a default value if not found.

Specified by:
getString in interface IValueMap
Parameters:
key - the key
defaultValue - default value to return if value is null
Returns:
the String
See Also:
IValueMap.getString(String, String)

getString

public final String getString(String key)
Description copied from interface: IValueMap
Retrieves a String by key.

Specified by:
getString in interface IValueMap
Parameters:
key - the key
Returns:
the String
See Also:
IValueMap.getString(String)

getCharSequence

public final CharSequence getCharSequence(String key)
Description copied from interface: IValueMap
Retrieves a CharSequence by key.

Specified by:
getCharSequence in interface IValueMap
Parameters:
key - the key
Returns:
the CharSequence
See Also:
IValueMap.getCharSequence(String)

getStringArray

public String[] getStringArray(String key)
Description copied from interface: IValueMap
Retrieves 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 length one. If it was an array of another type, a String array will be made and each element will be converted to a String.

Specified by:
getStringArray in interface IValueMap
Parameters:
key - the key
Returns:
the String array of that key
See Also:
IValueMap.getStringArray(String)

getStringValue

public StringValue getStringValue(String key)
Description copied from interface: IValueMap
Retrieves a StringValue object by key.

Specified by:
getStringValue in interface IValueMap
Parameters:
key - the key
Returns:
the StringValue object
See Also:
IValueMap.getStringValue(String)

getTime

public final Time getTime(String key)
                   throws StringValueConversionException
Description copied from interface: IValueMap
Retrieves a Time object by key.

Specified by:
getTime in interface IValueMap
Parameters:
key - the key
Returns:
the Time object
Throws:
StringValueConversionException
See Also:
IValueMap.getTime(String)

isImmutable

public final boolean isImmutable()
Description copied from interface: IValueMap
Returns whether or not this IValueMap is immutable.

Specified by:
isImmutable in interface IValueMap
Returns:
whether or not this IValueMap is immutable
See Also:
IValueMap.isImmutable()

makeImmutable

public final IValueMap makeImmutable()
Description copied from interface: IValueMap
Makes this IValueMap immutable by changing the underlying map representation to a Collections.unmodifiableMap. After calling this method, any attempt to modify this IValueMap will result in a RuntimeException being thrown by the Collections framework.

Specified by:
makeImmutable in interface IValueMap
Returns:
this IValueMap
See Also:
IValueMap.makeImmutable()

put

public Object put(String key,
                  Object value)
Specified by:
put in interface Map<String,Object>
Overrides:
put in class HashMap<String,Object>
See Also:
Map.put(Object, Object)

add

public final Object add(String key,
                        String value)
Adds the value to this ValueMap with the given key. If the key already is in the ValueMap 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 ValueMap
Returns:
the value itself if there was no previous value, or a String array with the combined values

putAll

public void putAll(Map<? extends String,? extends Object> map)
Specified by:
putAll in interface Map<String,Object>
Overrides:
putAll in class HashMap<String,Object>
See Also:
Map.putAll(java.util.Map)

remove

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

getKey

public String getKey(String key)
Description copied from interface: IValueMap
Provided that the hash key is a String and you need to access the value ignoring the key's case (upper- or lowercase letters), then you may use this method to get the correct writing.

Specified by:
getKey in interface IValueMap
Parameters:
key - the key
Returns:
the key with the correct writing
See Also:
IValueMap.getKey(String)

toString

public String toString()
Generates a String representation of this object.

Overrides:
toString in class AbstractMap<String,Object>
Returns:
String representation of this ValueMap consistent with the tag-attribute style of markup elements. For example: a="x" b="y" c="z".

getAsBoolean

public Boolean getAsBoolean(String key)
Description copied from interface: IValueMap
Retrieves a Boolean value by key.

Specified by:
getAsBoolean in interface IValueMap
Parameters:
key - the key
Returns:
the value or null if value is not a valid boolean or no value is in this IValueMap
See Also:
IValueMap.getAsBoolean(String)

getAsBoolean

public boolean getAsBoolean(String key,
                            boolean defaultValue)
Description copied from interface: IValueMap
Retrieves a boolean value by key.

Specified by:
getAsBoolean in interface IValueMap
Parameters:
key - the key
defaultValue - the default to return
Returns:
the value or defaultValue if value is not a valid boolean or no value is in this IValueMap
See Also:
IValueMap.getAsBoolean(String, boolean)

getAsInteger

public Integer getAsInteger(String key)
Description copied from interface: IValueMap
Retrieves an Integer value by key.

Specified by:
getAsInteger in interface IValueMap
Parameters:
key - the key
Returns:
the value or null if value is not a valid integer or no value is in this IValueMap
See Also:
IValueMap.getAsInteger(String)

getAsInteger

public int getAsInteger(String key,
                        int defaultValue)
Description copied from interface: IValueMap
Retrieves an integer value by key.

Specified by:
getAsInteger in interface IValueMap
Parameters:
key - the key
defaultValue - the default to return
Returns:
the value or defaultValue if value is not a valid integer or no value is in this IValueMap
See Also:
IValueMap.getAsInteger(String, int)

getAsLong

public Long getAsLong(String key)
Description copied from interface: IValueMap
Retrieves a Long value by key.

Specified by:
getAsLong in interface IValueMap
Parameters:
key - the key
Returns:
the value or null if value is not a valid long or no value is in this IValueMap
See Also:
IValueMap.getAsLong(String)

getAsLong

public long getAsLong(String key,
                      long defaultValue)
Description copied from interface: IValueMap
Retrieves a long value by key.

Specified by:
getAsLong in interface IValueMap
Parameters:
key - the key
defaultValue - the default to return
Returns:
the value or defaultValue if value is not a valid long or no value is in this IValueMap
See Also:
IValueMap.getAsLong(String, long)

getAsDouble

public Double getAsDouble(String key)
Description copied from interface: IValueMap
Retrieves a Double value by key.

Specified by:
getAsDouble in interface IValueMap
Parameters:
key - the key
Returns:
the value or null if value is not a valid double or no value is in this IValueMap
See Also:
IValueMap.getAsDouble(String)

getAsDouble

public double getAsDouble(String key,
                          double defaultValue)
Description copied from interface: IValueMap
Retrieves a double value by key.

Specified by:
getAsDouble in interface IValueMap
Parameters:
key - the key
defaultValue - the default to return
Returns:
the value or defaultValue if value is not a valid double or no value is in this IValueMap
See Also:
IValueMap.getAsDouble(String, double)

getAsDuration

public Duration getAsDuration(String key)
Description copied from interface: IValueMap
Retrieves a Duration value by key.

Specified by:
getAsDuration in interface IValueMap
Parameters:
key - the key
Returns:
the value or null if value is not a valid Duration or no value is in this IValueMap
See Also:
IValueMap.getAsDuration(String)

getAsDuration

public Duration getAsDuration(String key,
                              Duration defaultValue)
Description copied from interface: IValueMap
Retrieves a Duration value by key.

Specified by:
getAsDuration in interface IValueMap
Parameters:
key - the key
defaultValue - the default to return
Returns:
the value or defaultValue if value is not a valid Duration or no value is in this IValueMap
See Also:
IValueMap.getAsDuration(String, Duration)

getAsTime

public Time getAsTime(String key)
Description copied from interface: IValueMap
Retrieves a Time value by key.

Specified by:
getAsTime in interface IValueMap
Parameters:
key - the key
Returns:
the value or null if value is not a valid Time or no value is in this IValueMap
See Also:
IValueMap.getAsTime(String)

getAsTime

public Time getAsTime(String key,
                      Time defaultValue)
Description copied from interface: IValueMap
Retrieves a Time value by key.

Specified by:
getAsTime in interface IValueMap
Parameters:
key - the key
defaultValue - the default to return
Returns:
the value or defaultValue if value is not a valid Time or no value is in this IValueMap
See Also:
IValueMap.getAsTime(String, Time)

getAsEnum

public <T extends Enum<T>> T getAsEnum(String key,
                                       Class<T> eClass)
Description copied from interface: IValueMap
Retrieves an Enum value by key.

Specified by:
getAsEnum in interface IValueMap
Type Parameters:
T - type of enum
Parameters:
key - the key
eClass - the enumeration class
Returns:
the value or null if value is not a valid value of the Enumeration or no value is in this IValueMap
See Also:
IValueMap.getAsEnum(java.lang.String, java.lang.Class)

getAsEnum

public <T extends Enum<T>> T getAsEnum(String key,
                                       T defaultValue)
Description copied from interface: IValueMap
Retrieves an Enum value by key.

Specified by:
getAsEnum in interface IValueMap
Type Parameters:
T - type of enum
Parameters:
key - the key
defaultValue - the default value from the Enumeration (cannot be null)
Returns:
the value or defaultValue if value is not a valid value of the Enumeration or no value is in this IValueMap
See Also:
IValueMap.getAsEnum(java.lang.String, java.lang.Enum)

getAsEnum

public <T extends Enum<T>> T getAsEnum(String key,
                                       Class<T> eClass,
                                       T defaultValue)
Description copied from interface: IValueMap
Retrieves an Enum value by key.

Specified by:
getAsEnum in interface IValueMap
Type Parameters:
T - type of enum
Parameters:
key - the key
eClass - the enumeration class
defaultValue - the default value from the Enumeration (may be null)
Returns:
the value or defaultValue if value is not a valid value of the Enumeration or no value is in this IValueMap
See Also:
IValueMap.getAsEnum(java.lang.String, java.lang.Class, java.lang.Enum)


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.