Class LiveAttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>,IMPL_TYPE extends LiveAttributeMap<IFACE_TYPE,IMPL_TYPE>>

All Implemented Interfaces:
LogOutputAppendable, LivenessManager, LivenessNode, LivenessReferent, AttributeMap<IFACE_TYPE>, Serializable
Direct Known Subclasses:
BaseGridAttributes

public abstract class LiveAttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>,IMPL_TYPE extends LiveAttributeMap<IFACE_TYPE,IMPL_TYPE>> extends LivenessArtifact implements AttributeMap<IFACE_TYPE>
Re-usable AttributeMap implementation that is also a LivenessArtifact.
See Also:
ImplNote:
Rather than rely on final, explicitly-immutable Map instances for storage, this implementation does allow for mutation after construction. This allows a pattern wherein operations fill their result AttributeMap after construction using setAttribute(String, Object), which by convention must only be done before the result is published. No mutation is permitted after first access using any of getAttribute(String), getAttributeKeys(), hasAttribute(String), getAttributes(), or AttributeMap.getAttributes(Predicate).
  • Constructor Details

    • LiveAttributeMap

      protected LiveAttributeMap(@Nullable @Nullable Map<String,Object> initialAttributes)
      Parameters:
      initialAttributes - The attributes map to use until mutated, or else null to allocate a new one
  • Method Details

    • setAttribute

      @InternalUseOnly public void setAttribute(@NotNull @NotNull String key, @NotNull @NotNull Object object)
      Set the value of an attribute. This is for internal use by operations that build result AttributeMaps, and should never be used from multiple threads or after a result has been published.
      Parameters:
      key - The name of the attribute; must not be null
      object - The value to be assigned; must not be null
    • setAttribute

      @InternalUseOnly public void setAttribute(@NotNull @NotNull String key, @NotNull @NotNull UnaryOperator<Object> updater)
      Read and update the value of an attribute. This is for internal use by operations that build result AttributeMaps, and should never be used from multiple threads or after a result has been published.
      Parameters:
      key - The name of the attribute; must not be null
      updater - Function on the (possibly-null) existing value to produce the non-null new value
    • copyAttributes

      protected static void copyAttributes(@NotNull @NotNull AttributeMap<?> source, @NotNull @NotNull LiveAttributeMap<?,?> destination, @NotNull @NotNull Predicate<String> shouldCopy)
      Copy attributes between AttributeMaps, filtered by a predicate.
      Parameters:
      source - The AttributeMap to copy attributes from
      destination - The LiveAttributeMap to copy attributes to
      shouldCopy - Should we copy this attribute key?
    • published

      public boolean published()
      Test if this LiveAttributeMap has been published yet. This determines whether it's safe to call setAttribute(String, Object) or setAttribute(String, UnaryOperator).
      Returns:
      Whether this LiveAttributeMap has been published
    • prepareReturnThis

      protected IFACE_TYPE prepareReturnThis()
    • prepareReturnCopy

      protected IFACE_TYPE prepareReturnCopy()
    • withAttributes

      public IFACE_TYPE withAttributes(@NotNull @NotNull Map<String,Object> toAdd, @NotNull @NotNull Collection<String> toRemove)
      Description copied from interface: AttributeMap
      Get an AttributeMap that is the same as this, but with the specified attributes added/replaced or removed. If the supplied attributes toAdd and toRemove would not result in any changes to this, implementations may return this.
      Specified by:
      withAttributes in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      toAdd - Attribute key-value pairs to add or replace (if the key already exists on this). Neither keys nor values may be null.
      toRemove - Attribute keys to remove
      Returns:
      The result AttributeMap
    • withAttributes

      public IFACE_TYPE withAttributes(@NotNull @NotNull Map<String,Object> toAdd)
      Description copied from interface: AttributeMap
      Get an AttributeMap that is the same as this, but with the specified attributes added/replaced. If the supplied attributes toAdd would not result in any changes to this, implementations may return this.
      Specified by:
      withAttributes in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      toAdd - Attribute key-value pairs to add or replace (if the key already exists on this)
      Returns:
      The result AttributeMap
    • withoutAttributes

      public IFACE_TYPE withoutAttributes(@NotNull @NotNull Collection<String> toRemove)
      Description copied from interface: AttributeMap
      Get an AttributeMap that is the same as this, but with the specified attributes removed. If the supplied attributes toRemove would not result in any changes to this, implementations may return this.
      Specified by:
      withoutAttributes in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      toRemove - Attribute keys to remove
      Returns:
      The result AttributeMap
    • retainingAttributes

      public IFACE_TYPE retainingAttributes(@NotNull @NotNull Collection<String> toRetain)
      Description copied from interface: AttributeMap
      Get an AttributeMap that is the same as this, but with only the specified attributes retained. If the supplied attributes toAdd would not result in any changes to this, implementations may return this.
      Specified by:
      retainingAttributes in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      toRetain - Attribute keys to retain
      Returns:
      The result AttributeMap
    • copy

      protected abstract IMPL_TYPE copy()
      Create a copy of this with initially-shared mutableAttributes.
    • getAttribute

      @ConcurrentMethod @Nullable public @Nullable Object getAttribute(@NotNull @NotNull String key)
      Description copied from interface: AttributeMap
      Get the value for the specified attribute key.
      Specified by:
      getAttribute in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      key - The name of the attribute
      Returns:
      The value, or null if there was none.
    • getAttributeKeys

      @ConcurrentMethod @NotNull public @NotNull Set<String> getAttributeKeys()
      Description copied from interface: AttributeMap
      Get an immutable set of all the attributes that have values in this AttributeMap.
      Specified by:
      getAttributeKeys in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Returns:
      An immutable set of attribute keys (names)
    • hasAttribute

      @ConcurrentMethod public boolean hasAttribute(@NotNull @NotNull String name)
      Description copied from interface: AttributeMap
      Check if the specified attribute exists in this AttributeMap.
      Specified by:
      hasAttribute in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      name - The key (name) of the attribute
      Returns:
      true if the attribute exists
    • getAttributes

      @NotNull public @NotNull Map<String,Object> getAttributes()
      Description copied from interface: AttributeMap
      Get all attributes in this AttributeMap.
      Specified by:
      getAttributes in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Returns:
      An immutable map containing all attributes from this AttributeMap
    • getAttributes

      @ConcurrentMethod @NotNull public @NotNull Map<String,Object> getAttributes(@NotNull @NotNull Predicate<String> included)
      Description copied from interface: AttributeMap
      Get all attributes from this AttributeMap whose keys are accepted by included.
      Specified by:
      getAttributes in interface AttributeMap<IFACE_TYPE extends AttributeMap<IFACE_TYPE>>
      Parameters:
      included - A predicate to determine which attribute keys to include
      Returns:
      An immutable map containing AttributeMap's attributes whose keys are accepted by included