- java.lang.Object
-
- org.praxislive.core.Value
-
- org.praxislive.core.types.PMap
-
public final class PMap extends Value
An ordered map of Strings to Values.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PMap.Builder
A PMap builder.-
Nested classes/interfaces inherited from class org.praxislive.core.Value
Value.Type<T extends Value>
-
-
Field Summary
Fields Modifier and Type Field Description static PMap
EMPTY
An empty PMap.static BinaryOperator<Value>
IF_ABSENT
An operator for use withmerge(org.praxislive.core.types.PMap, org.praxislive.core.types.PMap, java.util.function.BinaryOperator)
that only adds mappings where the key is not present in the base map.static BinaryOperator<Value>
REPLACE
An operator for use withmerge(org.praxislive.core.types.PMap, org.praxislive.core.types.PMap, java.util.function.BinaryOperator)
that will replace mapped values in the base map, unless the new value is empty in which case the mapping is removed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static PMap.Builder
builder()
Create a PMap.Builder.static PMap.Builder
builder(int initialCapacity)
Deprecated.boolean
equals(Object obj)
Values must override the default equals method.boolean
equivalent(Value arg)
Indicates whether some other Value is equivalent to this one.static Optional<PMap>
from(Value value)
Cast or convert the provided value into a PMap, wrapped in an Optional.Value
get(String key)
Get the value for the given key, or null if the key does not exist.boolean
getBoolean(String key, boolean def)
Get a boolean value from the map, returning a default value if the key is not mapped or mapped to a value that cannot be converted to a boolean.double
getDouble(String key, double def)
Get a double value from the map, returning a default value if the key is not mapped or mapped to a value that cannot be converted to a double.int
getInt(String key, int def)
Get an integer value from the map, returning a default value if the key is not mapped or mapped to a value that cannot be converted to an integer.String
getString(String key, String def)
Get a String value from the map, returning a default value if the key is not mapped.int
hashCode()
Values must override the default hashcode method.boolean
isEmpty()
Check whether this Value is an empty value and has a zero length string representation.List<String>
keys()
List of map keys.static PMap
merge(PMap base, PMap additional, BinaryOperator<Value> operator)
Create a new PMap by merging the additional map into the base map, according to the result of the provided operator.static PMap
of(String key, Object value)
Create a PMap with one mapping.static PMap
of(String key1, Object value1, String key2, Object value2)
Create a PMap with two mappings.static PMap
of(String key1, Object value1, String key2, Object value2, String key3, Object value3)
Create a PMap with three mappings.static PMap
of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4)
Create a PMap with four mappings.static PMap
of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5)
Create a PMap with five mappings.static PMap
parse(String text)
Parse the given text into a PMap.int
size()
Size of the map.String
toString()
Values must override the default method to return a string representation that is immutable.
-
-
-
Field Detail
-
EMPTY
public static final PMap EMPTY
An empty PMap.
-
IF_ABSENT
public static final BinaryOperator<Value> IF_ABSENT
An operator for use withmerge(org.praxislive.core.types.PMap, org.praxislive.core.types.PMap, java.util.function.BinaryOperator)
that only adds mappings where the key is not present in the base map.
-
REPLACE
public static final BinaryOperator<Value> REPLACE
An operator for use withmerge(org.praxislive.core.types.PMap, org.praxislive.core.types.PMap, java.util.function.BinaryOperator)
that will replace mapped values in the base map, unless the new value is empty in which case the mapping is removed.
-
-
Method Detail
-
get
public Value get(String key)
Get the value for the given key, or null if the key does not exist.- Parameters:
key
- map key- Returns:
- mapped value or null
-
getBoolean
public boolean getBoolean(String key, boolean def)
Get a boolean value from the map, returning a default value if the key is not mapped or mapped to a value that cannot be converted to a boolean.- Parameters:
key
- map keydef
- default if unmapped or invalid- Returns:
- value or default
-
getInt
public int getInt(String key, int def)
Get an integer value from the map, returning a default value if the key is not mapped or mapped to a value that cannot be converted to an integer.- Parameters:
key
- map keydef
- default if unmapped or invalid- Returns:
- value or default
-
getDouble
public double getDouble(String key, double def)
Get a double value from the map, returning a default value if the key is not mapped or mapped to a value that cannot be converted to a double.- Parameters:
key
- map keydef
- default if unmapped or invalid- Returns:
- value or default
-
getString
public String getString(String key, String def)
Get a String value from the map, returning a default value if the key is not mapped.- Parameters:
key
- map keydef
- default if unmapped- Returns:
- value or default
-
size
public int size()
Size of the map. This is the number of key-value pairs.- Returns:
- size of the map
-
keys
public List<String> keys()
List of map keys. This is returned as a List as the keys are ordered.- Returns:
- map keys
-
toString
public String toString()
Description copied from class:Value
Values must override the default method to return a string representation that is immutable.
-
isEmpty
public boolean isEmpty()
Description copied from class:Value
Check whether this Value is an empty value and has a zero length string representation. Subclasses may wish to override this for efficiency if the String representation is lazily created.
-
equivalent
public boolean equivalent(Value arg)
Description copied from class:Value
Indicates whether some other Value is equivalent to this one. UnlikeValue.equals(java.lang.Object)
this method is not symmetric - a value of a different type might be equivalent to this without the other type considering the reverse to be true.The default implementation uses identity or String equality.
- Overrides:
equivalent
in classValue
- Parameters:
arg
- value to test for equivalence- Returns:
- true if value is equivalent to this
-
hashCode
public int hashCode()
Description copied from class:Value
Values must override the default hashcode method.
-
equals
public boolean equals(Object obj)
Description copied from class:Value
Values must override the default equals method. This method should only returntrue
if the supplied Object is of the same type as the implementing Value. Values of an unknown type should be coerced before calling this method. This method does not have to guarantee thatthis.equals(that) == this.toString().equals(that.toString())
-
of
public static PMap of(String key, Object value)
Create a PMap with one mapping. Map values that are notValue
types will be converted automatically.- Parameters:
key
- map keyvalue
- mapped value- Returns:
- new PMap
-
of
public static PMap of(String key1, Object value1, String key2, Object value2)
Create a PMap with two mappings. Map values that are notValue
types will be converted automatically.- Parameters:
key1
- first map keyvalue1
- first mapped valuekey2
- second map keyvalue2
- second mapped value- Returns:
- new PMap
-
of
public static PMap of(String key1, Object value1, String key2, Object value2, String key3, Object value3)
Create a PMap with three mappings. Map values that are notValue
types will be converted automatically.- Parameters:
key1
- first map keyvalue1
- first mapped valuekey2
- second map keyvalue2
- second mapped valuekey3
- third map keyvalue3
- third mapped value- Returns:
- new PMap
-
of
public static PMap of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4)
Create a PMap with four mappings. Map values that are notValue
types will be converted automatically.- Parameters:
key1
- first map keyvalue1
- first mapped valuekey2
- second map keyvalue2
- second mapped valuekey3
- third map keyvalue3
- third mapped valuekey4
- fourth map keyvalue4
- fourth mapped value- Returns:
- new PMap
-
of
public static PMap of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5)
Create a PMap with five mappings. Map values that are notValue
types will be converted automatically.- Parameters:
key1
- first map keyvalue1
- first mapped valuekey2
- second map keyvalue2
- second mapped valuekey3
- third map keyvalue3
- third mapped valuekey4
- fourth map keyvalue4
- fourth mapped valuekey5
- fifth map keyvalue5
- fifth mapped value- Returns:
- new PMap
-
parse
public static PMap parse(String text) throws ValueFormatException
Parse the given text into a PMap.- Parameters:
text
- text to parse- Returns:
- parsed PArray
- Throws:
ValueFormatException
-
from
public static Optional<PMap> from(Value value)
Cast or convert the provided value into a PMap, wrapped in an Optional. If the value is already a PMap, the Optional will wrap the existing value. If the value is not a PMap and cannot be converted into one, an empty Optional is returned.- Parameters:
value
- value- Returns:
- optional PArray
-
merge
public static PMap merge(PMap base, PMap additional, BinaryOperator<Value> operator)
Create a new PMap by merging the additional map into the base map, according to the result of the provided operator. The operator will be called for all values in the additional map, even if no mapping exists in the base map. The operator must be able to handle null input. The operator should return the resulting mapped value, or null to remove the mapping. SeeREPLACE
andIF_ABSENT
for operators that should cover most common usage.- Parameters:
base
- base mapadditional
- additional mapoperator
- operator to compute result value- Returns:
- new PMap
-
builder
public static PMap.Builder builder()
Create a PMap.Builder.- Returns:
- new PMap.Builder
-
builder
@Deprecated public static PMap.Builder builder(int initialCapacity)
Deprecated.
-
-