Interface GenericProperties

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
  • Method Details

    • getPropertyMap

      Returns an instance of GenericProperties.PropertyMap with keys of type String and values of type Object. Implementing classes must define this method, which will typically return a reference to a local map instance. The returned object must not be null.
      Returns:
      the GenericProperties.PropertyMap associated with the implementing instance
    • setProperty

      default <T> void setProperty(GenericProperties.PropertyKey<T> key, T value)
      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 two GenericProperties.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 be null)
      value - the value associated with this property (may not be null)
      Throws:
      IllegalArgumentException - if the supplied key or value is null or if the property map already contains an entry with a different type
    • getProperty

      default <T> T getProperty(GenericProperties.PropertyKey<T> key)
      Returns the value associated with the specified GenericProperties.PropertyKey, or null 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 be null)
      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 is null
    • removeProperty

      default <T> void removeProperty(GenericProperties.PropertyKey<T> key)
      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

      default void clearAllProperties()
      Removes all properties.