- All Known Implementing Classes:
BinaryRegion
,SegmentationBackedRegion
public interface GenericProperties
Defines a primitive mechanism for attaching arbitrary properties to an object dynamically using generic types to
eliminate type casts. Objects of any type may be attached and some type checking is done at compile time. The keys to
be used for inserting and retrieving values are identified by name (a
String
) and associated with a specific
value type. An implementing class only needs to define method getPropertyMap()
. In principle, the
functionality is the same as HashMap
but definition as an interface avoids having to subclass
HashMap
. Typical usage example:
public class Foo implements GenericProperties { private final PropertyMap properties = new PropertyMap(); @Override public PropertyMap getPropertyMap() { return this.properties; } ... } Foo f = new Foo(); PropertyKey<double[]> key = new PropertyKey<>("UniqueName"); double[] x = {1, 2, 3, 4}; f.setProperty(key, x); ... double[] y = f.getProperty(key);Note that only values matching the key's type can be passed to
setProperty(PropertyKey, Object)
and no type
casts are required when using getProperty(PropertyKey)
. However, creating duplicate keys with the same name
but different type is an error and must be avoided (unfortunately this cannot be checked at compile time and
GenericProperties.PropertyKey
has no information about the associated value class at runtime).- Version:
- 2023/01/03
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final class
Defines a generic map key to be used withGenericProperties
.static final class
The underlying hash map class, to be instantiated by implementing classes. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Removes all properties.default <T> T
Returns the value associated with the specifiedGenericProperties.PropertyKey
, ornull
if this map contains no mapping for the key.Returns an instance ofGenericProperties.PropertyMap
with keys of typeString
and values of typeObject
.default <T> void
Removes the property associated with the specified key if defined, otherwise does nothing.default <T> void
setProperty
(GenericProperties.PropertyKey<T> key, T value) Associates the specified value with the specified key in this property map.
-
Method Details
-
getPropertyMap
Returns an instance ofGenericProperties.PropertyMap
with keys of typeString
and values of typeObject
. Implementing classes must define this method, which will typically return a reference to a local map instance. The returned object must not benull
.- Returns:
- the
GenericProperties.PropertyMap
associated with the implementing instance
-
setProperty
Associates the specified value with the specified key in this property map. The supplied value must match the generic type of key. If the property map previously contained an entry for that key, the old value is replaced by the specified value if it is of the same type as the new value. Otherwise, if the type of the existing entry is different to the type of the new value, an exception is thrown. This happens when twoGenericProperties.PropertyKey
with the same name but of different value type are used (which is an error).- Type Parameters:
T
- the generic key and value type- Parameters:
key
- the key of the property (may not benull
)value
- the value associated with this property (may not benull
)- Throws:
IllegalArgumentException
- if the supplied key or value isnull
or if the property map already contains an entry with a different type
-
getProperty
Returns the value associated with the specifiedGenericProperties.PropertyKey
, ornull
if this map contains no mapping for the key.- Type Parameters:
T
- the generic key and value type- Parameters:
key
- the name of the property (may not benull
)- Returns:
- the value (of type T) to which the specified key is mapped, or
null
if this map contains no mapping for the key - Throws:
IllegalArgumentException
- if the supplied key isnull
-
removeProperty
Removes the property associated with the specified key if defined, otherwise does nothing.- Type Parameters:
T
- the generic key type- Parameters:
key
- the name of the property
-
clearAllProperties
Removes all properties.
-