Class ValueMap
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,V>
-
- java.util.LinkedHashMap<String,Object>
-
- org.apache.wicket.util.value.ValueMap
-
- Direct Known Subclasses:
AttributeMap
public class ValueMap extends LinkedHashMap<String,Object> implements IValueMap
AIValueMap
implementation that holds values, parsesString
s, and exposes a variety of convenience methods.In addition to a no-arg constructor and a copy constructor that takes a
Map
argument,ValueMap
s 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, includingString
,StringValue
,int
,long
,double
,Time
andDuration
.The
makeImmutable
method will make the underlyingMap
immutable. Further attempts to change theMap
will result in aRuntimeException
.The
toString
method converts aValueMap
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 extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description ValueMap()
Constructs emptyValueMap
.ValueMap(String keyValuePairs)
Constructor.ValueMap(String keyValuePairs, String delimiter)
Constructor.ValueMap(String keyValuePairs, String delimiter, MetaPattern valuePattern)
Constructor.ValueMap(Map<? extends String,?> map)
Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
add(String key, String value)
Adds the value to thisValueMap
with the given key.void
clear()
Boolean
getAsBoolean(String key)
Retrieves aBoolean
value by key.boolean
getAsBoolean(String key, boolean defaultValue)
Retrieves aboolean
value by key.Double
getAsDouble(String key)
Retrieves aDouble
value by key.double
getAsDouble(String key, double defaultValue)
Retrieves adouble
value by key.Duration
getAsDuration(String key)
Retrieves aDuration
value by key.Duration
getAsDuration(String key, Duration defaultValue)
Retrieves aDuration
value by key.<T extends Enum<T>>
TgetAsEnum(String key, Class<T> eClass)
Retrieves anEnum
value by key.<T extends Enum<T>>
TgetAsEnum(String key, Class<T> eClass, T defaultValue)
Retrieves anEnum
value by key.<T extends Enum<T>>
TgetAsEnum(String key, T defaultValue)
Retrieves anEnum
value by key.Instant
getAsInstant(String key)
Retrieves aTime
value by key.Integer
getAsInteger(String key)
Retrieves anInteger
value by key.int
getAsInteger(String key, int defaultValue)
Retrieves aninteger
value by key.Long
getAsLong(String key)
Retrieves aLong
value by key.long
getAsLong(String key, long defaultValue)
Retrieves along
value by key.Instant
getAsTime(String key, Instant defaultValue)
Retrieves aTime
value by key.boolean
getBoolean(String key)
Retrieves aboolean
value by key.CharSequence
getCharSequence(String key)
Retrieves aCharSequence
by key.double
getDouble(String key)
Retrieves adouble
value by key.double
getDouble(String key, double defaultValue)
Retrieves adouble
value by key, using a default value if not found.Duration
getDuration(String key)
Retrieves aDuration
by key.Instant
getInstant(String key)
Retrieves aInstant
object by key.int
getInt(String key)
Retrieves anint
value by key.int
getInt(String key, int defaultValue)
Retrieves anint
value by key, using a default value if not found.String
getKey(String key)
Provided that the hash key is aString
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 along
value by key.long
getLong(String key, long defaultValue)
Retrieves along
value by key, using a default value if not found.String
getString(String key)
Retrieves aString
by key.String
getString(String key, String defaultValue)
Retrieves aString
by key, using a default value if not found.String[]
getStringArray(String key)
Retrieves aString
array by key.StringValue
getStringValue(String key)
Retrieves aStringValue
object by key.boolean
isImmutable()
Returns whether or not thisIValueMap
is immutable.IValueMap
makeImmutable()
Makes thisIValueMap
immutable by changing the underlying map representation to aCollections.unmodifiableMap
.Object
put(String key, Object value)
void
putAll(Map<? extends String,?> map)
Object
remove(Object key)
String
toString()
Generates aString
representation of this object.-
Methods inherited from class java.util.LinkedHashMap
containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, putIfAbsent, remove, replace, replace, size
-
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
compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll, size, values
-
-
-
-
Constructor Detail
-
ValueMap
public ValueMap()
Constructs emptyValueMap
.
-
ValueMap
public ValueMap(Map<? extends String,?> map)
Copy constructor.- Parameters:
map
- theValueMap
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
- delimiterString
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 pairsvaluePattern
- pattern for value. To pass a simple regular expression, pass "new MetaPattern(regexp)
".
-
-
Method Detail
-
clear
public final void clear()
-
getBoolean
public final boolean getBoolean(String key) throws StringValueConversionException
Description copied from interface:IValueMap
Retrieves aboolean
value by key.- Specified by:
getBoolean
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value
- Throws:
StringValueConversionException
-
getDouble
public final double getDouble(String key) throws StringValueConversionException
Description copied from interface:IValueMap
Retrieves adouble
value by key.- Specified by:
getDouble
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value
- Throws:
StringValueConversionException
-
getDouble
public final double getDouble(String key, double defaultValue)
Description copied from interface:IValueMap
Retrieves adouble
value by key, using a default value if not found.
-
getDuration
public final Duration getDuration(String key) throws StringValueConversionException
Description copied from interface:IValueMap
Retrieves aDuration
by key.- Specified by:
getDuration
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the
Duration
value - Throws:
StringValueConversionException
-
getInt
public final int getInt(String key) throws StringValueConversionException
Description copied from interface:IValueMap
Retrieves anint
value by key.- Specified by:
getInt
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value
- Throws:
StringValueConversionException
-
getInt
public final int getInt(String key, int defaultValue)
Description copied from interface:IValueMap
Retrieves anint
value by key, using a default value if not found.
-
getLong
public final long getLong(String key) throws StringValueConversionException
Description copied from interface:IValueMap
Retrieves along
value by key.- Specified by:
getLong
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value
- Throws:
StringValueConversionException
-
getLong
public final long getLong(String key, long defaultValue)
Description copied from interface:IValueMap
Retrieves along
value by key, using a default value if not found.
-
getString
public final String getString(String key, String defaultValue)
Description copied from interface:IValueMap
Retrieves aString
by key, using a default value if not found.
-
getString
public final String getString(String key)
Description copied from interface:IValueMap
Retrieves aString
by key.
-
getCharSequence
public final CharSequence getCharSequence(String key)
Description copied from interface:IValueMap
Retrieves aCharSequence
by key.- Specified by:
getCharSequence
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the
CharSequence
-
getStringArray
public String[] getStringArray(String key)
Description copied from interface:IValueMap
Retrieves aString
array by key. If the value was aString[]
it will be returned directly. If it was aString
it will be converted to aString
array of length one. If it was an array of another type, aString
array will be made and each element will be converted to aString
.- Specified by:
getStringArray
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the
String
array of that key
-
getStringValue
public StringValue getStringValue(String key)
Description copied from interface:IValueMap
Retrieves aStringValue
object by key.- Specified by:
getStringValue
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the
StringValue
object
-
getInstant
public final Instant getInstant(String key) throws StringValueConversionException
Description copied from interface:IValueMap
Retrieves aInstant
object by key.- Specified by:
getInstant
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the
Instant
object - Throws:
StringValueConversionException
-
isImmutable
public final boolean isImmutable()
Description copied from interface:IValueMap
Returns whether or not thisIValueMap
is immutable.- Specified by:
isImmutable
in interfaceIValueMap
- Returns:
- whether or not this
IValueMap
is immutable
-
makeImmutable
public final IValueMap makeImmutable()
Description copied from interface:IValueMap
Makes thisIValueMap
immutable by changing the underlying map representation to aCollections.unmodifiableMap
. After calling this method, any attempt to modify thisIValueMap
will result in aRuntimeException
being thrown by theCollections
framework.- Specified by:
makeImmutable
in interfaceIValueMap
- Returns:
- this
IValueMap
-
add
public final Object add(String key, String value)
Adds the value to thisValueMap
with the given key. If the key already is in theValueMap
it will combine the values into aString
array, else it will just store the value itself.- Parameters:
key
- the key to store the value undervalue
- the value that must be added/merged to theValueMap
- Returns:
- the value itself if there was no previous value, or a
String
array with the combined values
-
getKey
public String getKey(String key)
Description copied from interface:IValueMap
Provided that the hash key is aString
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.
-
toString
public String toString()
Generates aString
representation of this object.- Overrides:
toString
in classAbstractMap<String,Object>
- Returns:
String
representation of thisValueMap
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 aBoolean
value by key.- Specified by:
getAsBoolean
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value or null if value is not a valid boolean or no value is in this
IValueMap
-
getAsBoolean
public boolean getAsBoolean(String key, boolean defaultValue)
Description copied from interface:IValueMap
Retrieves aboolean
value by key.- Specified by:
getAsBoolean
in interfaceIValueMap
- Parameters:
key
- the keydefaultValue
- the default to return- Returns:
- the value or defaultValue if value is not a valid boolean or no value is in this
IValueMap
-
getAsInteger
public Integer getAsInteger(String key)
Description copied from interface:IValueMap
Retrieves anInteger
value by key.- Specified by:
getAsInteger
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value or null if value is not a valid integer or no value is in this
IValueMap
-
getAsInteger
public int getAsInteger(String key, int defaultValue)
Description copied from interface:IValueMap
Retrieves aninteger
value by key.- Specified by:
getAsInteger
in interfaceIValueMap
- Parameters:
key
- the keydefaultValue
- the default to return- Returns:
- the value or defaultValue if value is not a valid integer or no value is in this
IValueMap
-
getAsLong
public Long getAsLong(String key)
Description copied from interface:IValueMap
Retrieves aLong
value by key.
-
getAsLong
public long getAsLong(String key, long defaultValue)
Description copied from interface:IValueMap
Retrieves along
value by key.
-
getAsDouble
public Double getAsDouble(String key)
Description copied from interface:IValueMap
Retrieves aDouble
value by key.- Specified by:
getAsDouble
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value or null if value is not a valid double or no value is in this
IValueMap
-
getAsDouble
public double getAsDouble(String key, double defaultValue)
Description copied from interface:IValueMap
Retrieves adouble
value by key.- Specified by:
getAsDouble
in interfaceIValueMap
- Parameters:
key
- the keydefaultValue
- the default to return- Returns:
- the value or defaultValue if value is not a valid double or no value is in this
IValueMap
-
getAsDuration
public Duration getAsDuration(String key)
Description copied from interface:IValueMap
Retrieves aDuration
value by key.- Specified by:
getAsDuration
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value or null if value is not a valid Duration or no value is in this
IValueMap
-
getAsDuration
public Duration getAsDuration(String key, Duration defaultValue)
Description copied from interface:IValueMap
Retrieves aDuration
value by key.- Specified by:
getAsDuration
in interfaceIValueMap
- Parameters:
key
- the keydefaultValue
- the default to return- Returns:
- the value or defaultValue if value is not a valid Duration or no value is in this
IValueMap
-
getAsInstant
public Instant getAsInstant(String key)
Description copied from interface:IValueMap
Retrieves aTime
value by key.- Specified by:
getAsInstant
in interfaceIValueMap
- Parameters:
key
- the key- Returns:
- the value or null if value is not a valid Time or no value is in this
IValueMap
-
getAsTime
public Instant getAsTime(String key, Instant defaultValue)
Description copied from interface:IValueMap
Retrieves aTime
value by key.
-
getAsEnum
public <T extends Enum<T>> T getAsEnum(String key, Class<T> eClass)
Description copied from interface:IValueMap
Retrieves anEnum
value by key.
-
getAsEnum
public <T extends Enum<T>> T getAsEnum(String key, T defaultValue)
Description copied from interface:IValueMap
Retrieves anEnum
value by key.
-
getAsEnum
public <T extends Enum<T>> T getAsEnum(String key, Class<T> eClass, T defaultValue)
Description copied from interface:IValueMap
Retrieves anEnum
value by key.- Specified by:
getAsEnum
in interfaceIValueMap
- Type Parameters:
T
- type of enum- Parameters:
key
- the keyeClass
- the enumeration classdefaultValue
- 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
-
-