Class GenericProperties<K>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,Object>
io.github.thecsdev.tcdcommons.api.util.collections.GenericProperties<K>
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,
Object>
A
This implementation allows for storing and retrieving properties and their values using generics.
HashMap
that stores Object
s that represent "property" values,
and the keys represent "property" names.This implementation allows for storing and retrieving properties and their values using generics.
- See Also:
-
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
-
Method Summary
Modifier and TypeMethodDescriptionfinal <V> V
getProperty
(K key) UsesHashMap.get(Object)
to obtain the value of a given property, after which it is casted to<V>
and returned.final <V> V
getProperty
(K key, V defaultValue) UsesgetProperty(Object)
to obtain the value of a given property.final <V> V
getPropertyOrDefault
(K key, V defaultValue) UsesgetProperty(Object)
to obtain the value of a given property.final <V> V
setProperty
(K key, V... value) Assigns a new value<V>
for a given property.Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
Constructor Details
-
GenericProperties
public GenericProperties()
-
-
Method Details
-
getProperty
UsesHashMap.get(Object)
to obtain the value of a given property, after which it is casted to<V>
and returned.- Parameters:
key
- The property key.- Throws:
ClassCastException
- If a value is present, but it cannot be cast to type<V>
.- See Also:
-
getProperty
UsesgetProperty(Object)
to obtain the value of a given property. IfgetProperty(Object)
returnsnull
, the "default value" will be returned instead.- Parameters:
key
- The property key.defaultValue
- The value that will be returned ifgetProperty(Object)
returnsnull
.- Throws:
ClassCastException
- IfgetProperty(Object)
throws it.
-
getPropertyOrDefault
UsesgetProperty(Object)
to obtain the value of a given property. IfgetProperty(Object)
returnsnull
or throws aClassCastException
,setProperty(Object, Object...)
will be used to set the property to the "default value", after which the "default value" will be returned.- Parameters:
key
- The property key.defaultValue
- The value that will be returned ifgetProperty(Object)
returnsnull
.
-
setProperty
@SafeVarargs @Nullable public final <V> V setProperty(K key, V... value) throws IllegalArgumentException Assigns a new value<V>
for a given property.- Parameters:
key
- The property key.value
- The new value for the given property. Must pass a singleObject
here. Do not pass any arrays, null arrays, or arrays whose length is not 1.- Returns:
- The previous value of the given property, or
null
if no value of the given type was assigned. - Throws:
IllegalArgumentException
- If the "value" array is invalid, null, or its length is not 1.
-