Class AttributeMap

java.lang.Object
com.mayam.wf.attributes.shared.AttributeMap
All Implemented Interfaces:
AttributeManager, AttributeMutator, Copyable, Navigable

public class AttributeMap extends Object implements AttributeManager, Copyable, Navigable
Map from Attribute to value. When properly injected, the map will validate input to make sure it conforms to Attribute annotations. Setting an attribute will mark it as dirty.
Author:
Markus MÃ¥rtensson
  • Constructor Details

    • AttributeMap

      public AttributeMap()
  • Method Details

    • injectHelpers

      @Inject public void injectHelpers(AttributeValidator validator, AttributeDescription.Producer describer, AttributeFieldMapper attributeFieldMapper)
      Injects helper objects. Generally called by injector during creation, later using Injector.injectMembers() or manually by an object having access to both instances. A missing validator will cause the map to silently accept all input whereas a missing describer will cause setAttributeFromString(Attribute, String) to fail. When creating a copy (full or subset), a missing mapProvider means the copy will in turn have no helpers injected.
    • hasInjectedHelpers

      public boolean hasInjectedHelpers()
      Checks for the existence of AttributeValidator and AttributeDescription.Producer helper instances.
      Returns:
      true if both helpers are injected.
    • managesAttribute

      public boolean managesAttribute(Attribute attribute)
      Description copied from interface: AttributeManager
      Determines if the given attribute is part of the subset managed by the class.
      Specified by:
      managesAttribute in interface AttributeManager
      Parameters:
      attribute - the attribute to test.
      Returns:
      true, if the attribute can be accessed and/or mutated by the class.
    • setAttribute

      public @NonNull AttributeMap setAttribute(Attribute attribute, Object value)
      Description copied from interface: AttributeManager
      Attribute mutator.
      Specified by:
      setAttribute in interface AttributeManager
      Specified by:
      setAttribute in interface AttributeMutator
      Parameters:
      attribute - the attribute to set.
      value - the new value of the attribute.
      Returns:
      the manager, to allow method chaining.
    • setAttribute

      public @NonNull AttributeMap setAttribute(Attribute attribute, Object value, boolean markAsDirty)
      Attribute mutator with the option of disabling dirty flagging.
      Parameters:
      attribute - the attribute to set.
      value - the new value of the attribute.
      markAsDirty - if true, value will be marked as dirty within the map.
      Returns:
      the manager, to allow method chaining.
    • setAttributeFromString

      public @NonNull AttributeMap setAttributeFromString(Attribute attribute, String valueStr)
      Sets attribute value based on on a string, converting it to the class defined by Declare.valueClass().
      Parameters:
      attribute - the Attribute to set.
      valueStr - the string representation of the value to set.
      Returns:
      the map to allow method chaining.
    • removeAttribute

      public void removeAttribute(Attribute attribute)
      Removes a value from the map.
      Parameters:
      attribute - the attribute to have its value removed.
    • clear

      public void clear()
      Removes all values from the map.
    • getAttribute

      public <T> @Nullable T getAttribute(Attribute attribute)
      Description copied from interface: AttributeManager
      Attribute accessor.
      Specified by:
      getAttribute in interface AttributeManager
      Type Parameters:
      T - the requested type for casting.
      Parameters:
      attribute - the attribute to get.
      Returns:
      the current value of the attribute, cast as requested.
    • getAttribute

      public <T> @Nullable T getAttribute(Attribute attribute, Class<T> cls)
      Attribute accessor.
      Parameters:
      attribute - the attribute to get.
      cls - the requested type for casting.
      Returns:
      the current value of the attribute, cast as requested.
      Throws:
      ClassCastException - if the value cannot be cast as requested.
    • getAttributeByFieldId

      public <T> @Nullable T getAttributeByFieldId(String fieldId)
      Attribute accessor based on field identifier. Requires a AttributeFieldMapper implementation, which is typically only bound on the server side.
      Type Parameters:
      T - the requested type for casting.
      Parameters:
      fieldId - the field identifier.
      Returns:
      the current value of the attribute, cast as requested.
    • getAttributeByFieldId

      public <T> @Nullable T getAttributeByFieldId(String fieldId, Class<T> cls)
      Attribute accessor based on field identifier. Requires a AttributeFieldMapper implementation, which is typically only bound on the server side.
      Parameters:
      fieldId - the field identifier.
      cls - the requested type for casting.
      Returns:
      the current value of the attribute, cast as requested.
    • getAttributeByFieldIdAsString

      public @Nullable String getAttributeByFieldIdAsString(String fieldId)
      Gets an attribute value based on field identifier, converted to a string. Requires a AttributeFieldMapper implementation, which is typically only bound on the server side.
      Parameters:
      fieldId - the field id
      Returns:
      attribute value converted to a string.
    • setAttributeByFieldId

      public @NonNull AttributeMap setAttributeByFieldId(String fieldId, Object value)
      Attribute mutator based on field identifier. Requires a AttributeFieldMapper implementation, which is typically only bound on the server side.
      Parameters:
      fieldId - the field identifier.
      value - the new value of the attribute.
      Returns:
      the manager, to allow method chaining.
    • setAttributeByFieldIdFromString

      public @NonNull AttributeMap setAttributeByFieldIdFromString(String fieldId, String valueStr)
      Attribute mutator based on field identifier. Requires a AttributeFieldMapper implementation, which is typically only bound on the server side.
      Parameters:
      fieldId - the field identifier.
      valueStr - the string representation of the value to set.
      Returns:
      the manager, to allow method chaining.
    • removeAttributeByFieldId

      public void removeAttributeByFieldId(String fieldId)
      Remove attribute based on field identifier. Requires a AttributeFieldMapper implementation, which is typically only bound on the server side.
      Parameters:
      fieldId - the field identifier.
    • requireAttributeByFieldId

      public <T> @NonNull T requireAttributeByFieldId(String fieldId)
      Attribute accessor, based on field identifier, with included non-check. Should the value value prove to be null, a exception is thrown.
      Type Parameters:
      T - the requested type for casting.
      Parameters:
      fieldId - the field identifier.
      Returns:
      the current non-null value of the attribute, cast as requested.
      Throws:
      IllegalArgumentException - if the value is null.
    • requireAttribute

      public <T> @NonNull T requireAttribute(Attribute attribute)
      Attribute accessor with included null-check. Should the value value prove to be null, a exception is thrown.
      Type Parameters:
      T - the requested type for casting.
      Parameters:
      attribute - the attribute to get.
      Returns:
      the current non-null value of the attribute, cast as requested.
      Throws:
      IllegalArgumentException - if the value is null.
    • requireAttribute

      public <T> @NonNull T requireAttribute(Attribute attribute, Class<T> cls)
      Attribute accessor with included null-check. Should the value value prove to be null, a exception is thrown.
      Parameters:
      attribute - the attribute to get.
      cls - the requested type for casting.
      Returns:
      the current non-null value of the attribute, cast as requested.
      Throws:
      ClassCastException - if the value cannot be cast as requested.
    • getAttributeAsString

      public @Nullable String getAttributeAsString(Attribute attribute)
      Gets an attribute value, converted to a string.
      Parameters:
      attribute - attribute to be retrieved and converted.
      Returns:
      attribute value converted to a string.
    • putAll

      public void putAll(AttributeMap source)
      Copy values from another attribute map into this, regardless of dirty status. Anything flagged as dirty in the source will also be dirty in this one, however this map will not have any attributes marked clean even if they were clean in the source.
      Parameters:
      source - source attribute map.
    • putClean

      public void putClean(AttributeMap source)
      Copy values from another attribute map into this, unless they already exist in dirty state. The copied values will be considered clean.
      Parameters:
      source - source attribute map.
      See Also:
    • putDirty

      public void putDirty(AttributeMap source)
      Copy dirty values from another attribute map into this. The copied values will be marked as dirty in the this map.
      Parameters:
      source - source attribute map.
      See Also:
    • containsAttribute

      public boolean containsAttribute(Attribute attribute)
    • getAttributeSet

      public @NonNull Set<Attribute> getAttributeSet()
      Compile a set of attributes with values inside this attribute map.
      Returns:
      attribute set.
    • getDirtySet

      public @NonNull Set<Attribute> getDirtySet()
      Compile a set of attributes with dirty values inside this attribute map.
      Returns:
      dirty attribute set.
    • clearDirty

      public @NonNull AttributeMap clearDirty()
      Clears the dirty flags on all attribute values within the map.
      Returns:
      the map to allow method chaining.
    • attachCache

      public void attachCache(AttributeMap cache)
      Attaches a cache for use in scenarios where the AttributeMap describes a subject with operation parameters. The cache could serve as a snapshot of the full subject state and provide getAttribute(Attribute) and getAttributeAsString(Attribute) with results should the primary map have no value for the attribute (null being a valid value).
      Parameters:
      cache - a map containing fallback values.
      Throws:
      IllegalArgumentException - if the cache itself has an attached cache.
    • detachCache

      public @Nullable AttributeMap detachCache()
      Detaches the current cache, returning it.
    • hasCache

      public boolean hasCache()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • toLongString

      public @NonNull String toLongString()
    • toLongStringIgnoreNulls

      public @NonNull String toLongStringIgnoreNulls()
    • toLongStringIgnoreNulls

      public @NonNull String toLongStringIgnoreNulls(String separator)
    • toLongString

      public @NonNull String toLongString(String separator)
    • toLongString

      public @NonNull String toLongString(String separator, boolean ignoreNulls)
    • toLongString

      public @NonNull String toLongString(String separator, boolean ignoreNulls, boolean print_dirty)
    • toString

      public @NonNull String toString()
      Overrides:
      toString in class Object
    • merge

      public static AttributeMap merge(AttributeMap original, AttributeMap overrides)
      Merges two AttributeMap objects with priority given to the one referred to as "overrides". Expected to be called as "original = merge(original,overrides)". For this reason, original may become updated whereas overrides will not be touched.
      Parameters:
      original - original map, or null.
      overrides - map with overrides, or null.
      Returns:
      merged map, or null if both arguments were null.
    • export

      public AttributeMap.Exporter export()
      Begins a fluid export.
      See Also: