Class PropertyContainer

java.lang.Object
nl.pim16aap2.animatedarchitecture.core.structures.properties.PropertyContainer
All Implemented Interfaces:
Iterable<PropertyValuePair<?>>, IPropertyContainerConst, IPropertyHolder, IPropertyHolderConst

@NotThreadSafe public final class PropertyContainer extends Object implements IPropertyHolder, IPropertyContainerConst
Manages the properties of a structure.

New instances of this class can be created using forType(StructureType).

A property container is created for a specific structure type. It contains all properties that are defined for that type as defined by StructureType.getProperties().

It is not possible to set a property that is not defined for the structure type this property container was created for. Attempting to do so will result in an IllegalArgumentException.

  • Constructor Details

    • PropertyContainer

      public PropertyContainer()
      Creates a new empty property container.

      It is generally recommended to use forType(StructureType) instead to create a property container with the default properties for a specific structure type. This ensures that the required properties for that structure type are correctly marked as required and have their default values set.

  • Method Details

    • of

      public static PropertyContainer of(IPropertyContainerConst other)
      Creates a copy of the given property container.
      Parameters:
      other - The property container to copy.
      Returns:
      A new property container with the same properties as the given property container.
    • setPropertyValue

      public <T> IPropertyValue<T> setPropertyValue(Property<T> property, @Nullable T value)
      Description copied from interface: IPropertyHolder
      Sets the value of the given property.
      Specified by:
      setPropertyValue in interface IPropertyHolder
      Type Parameters:
      T - The type of the property.
      Parameters:
      property - The property to set the value for.
      value - The value to set.

      If this is null, the property will be removed.

      Returns:
      The previous value of the property, or null if the property had no value set.
    • removeProperty

      public <T> IPropertyValue<T> removeProperty(Property<T> property)
      Description copied from interface: IPropertyHolder
      Removes the given property from this property container.

      Properties cannot be removed under certain conditions. When in doubt, check IPropertyHolder.canRemoveProperty(Property).

      Specified by:
      removeProperty in interface IPropertyHolder
      Type Parameters:
      T - The type of the property.
      Parameters:
      property - The property to remove.
      Returns:
      The previous value of the property, or PropertyContainer.UnsetPropertyValue.INSTANCE if the property was not set.
    • setUntypedPropertyValue

      public <T> IPropertyValue<?> setUntypedPropertyValue(Property<T> property, @Nullable Object value)
      Sets the value of the given property without type checking.

      If possible, consider using setPropertyValue(Property, Object) instead for better type safety.

      Parameters:
      property - The property to set the value for.
      value - The value to set.
      Returns:
      The previous value of the property, or PropertyContainer.UnsetPropertyValue.INSTANCE if the property was not set.
      Throws:
      ClassCastException - If the value cannot be cast to the type of the property.
    • getPropertyValue

      public <T> IPropertyValue<T> getPropertyValue(Property<T> property)
      Description copied from interface: IPropertyHolderConst
      Gets the value of the given property.
      Specified by:
      getPropertyValue in interface IPropertyHolderConst
      Type Parameters:
      T - The type of the property.
      Parameters:
      property - The property to get the value of.
      Returns:
      The value of the property.
    • hasProperty

      public boolean hasProperty(Property<?> property)
      Description copied from interface: IPropertyHolderConst
      Checks if the given property has a value set.
      Specified by:
      hasProperty in interface IPropertyHolderConst
      Parameters:
      property - The property to check.
      Returns:
      true if the property has a value set, false otherwise.
    • hasProperties

      public boolean hasProperties(Collection<Property<?>> properties)
      Description copied from interface: IPropertyHolderConst
      Checks if this property holder has all the given properties.
      Specified by:
      hasProperties in interface IPropertyHolderConst
      Parameters:
      properties - The properties to check.
      Returns:
      true if this property holder has all the given properties, false otherwise.
    • canRemoveProperty

      public boolean canRemoveProperty(Property<?> property)
      Description copied from interface: IPropertyHolder
      Checks if a property can be removed from this property holder.

      A property can not be removed under the following conditions:

      Specified by:
      canRemoveProperty in interface IPropertyHolder
      Parameters:
      property - The property to check.
      Returns:
      true if the property can be removed from this property holder, false otherwise.
    • snapshot

      public PropertyContainerSnapshot snapshot()
      Creates a new snapshot of this property container.
      Returns:
      A new snapshot of this property container.
    • forType

      public static PropertyContainer forType(StructureType structureType)
      Creates a new property container for the given structure type.
      Parameters:
      structureType - The structure type to create the property container for.
      Returns:
      A new property container for the given structure type.
    • forProperties

      @VisibleForTesting public static PropertyContainer forProperties(List<Property<?>> properties, boolean required)
      Creates a new property container for the given properties.

      This method is intended for testing purposes only.

      For production code, use forType(StructureType) instead.

      Parameters:
      properties - The properties to create the property container for.
      required - Whether the properties are required.
      Returns:
      A new property container for the given properties.
    • iterator

      public Iterator<PropertyValuePair<?>> iterator()
      Description copied from interface: IPropertyContainerConst
      Returns a read-only iterator over the properties in this container.

      Any properties that are either unmapped or of the wrong type are excluded.

      Specified by:
      iterator in interface IPropertyContainerConst
      Specified by:
      iterator in interface Iterable<PropertyValuePair<?>>
      Returns:
      A read-only iterator over the properties in this container.
    • spliterator

      public Spliterator<PropertyValuePair<?>> spliterator()
      Description copied from interface: IPropertyContainerConst
      Returns a read-only spliterator over the properties in this container.

      Any properties that are either unmapped or of the wrong type are excluded.

      Specified by:
      spliterator in interface IPropertyContainerConst
      Specified by:
      spliterator in interface Iterable<PropertyValuePair<?>>
      Returns:
      A read-only spliterator over the properties in this container.
    • addAll

      public void addAll(IPropertyContainerConst propertyContainer)
      Adds all properties from the given property container to this property container.

      Existing properties will be overwritten.

      A property will be marked as required only if it is already required in this property container.

      Parameters:
      propertyContainer - The property container to add the properties from.
    • propertyCount

      public int propertyCount()
      Description copied from interface: IPropertyHolderConst
      Gets the number of properties in this property container.

      This is the number of entries in the property map.

      Specified by:
      propertyCount in interface IPropertyHolderConst
      Returns:
      The number of properties in this property container.
    • ofAll

      public static PropertyContainer ofAll(Object... properties)
      Creates a new PropertyContainer from the given properties.
      Parameters:
      properties - A group of interleaved property-value-isRequired triplets.
      Returns:
      A new PropertyContainer with the given properties.
      Throws:
      IllegalArgumentException - If the properties are not provided in pairs of 3.

      If an object at an even index is not a Property.

      NullPointerException - If any value is null.
      ClassCastException - If the value of a property cannot be cast to the type of the preceding property using Property.cast(Object).
    • of

      public static <A> PropertyContainer of(Property<A> property0, A value0, boolean required0)
      Type-safe version of ofAll(Object...) for 1 property.
    • of

      public static <A,B> PropertyContainer of(Property<A> property0, A value0, boolean required0, Property<B> property1, B value1, boolean required1)
      Type-safe version of ofAll(Object...) for 2 properties.
    • of

      public static <A,B,C> PropertyContainer of(Property<A> property0, A value0, boolean required0, Property<B> property1, B value1, boolean required1, Property<C> property2, C value2, boolean required2)
      Type-safe version of ofAll(Object...) for 3 properties.
    • of

      public static <A,B,C,D> PropertyContainer of(Property<A> property0, A value0, boolean required0, Property<B> property1, B value1, boolean required1, Property<C> property2, C value2, boolean required2, Property<D> property3, D value3, boolean required3)
      Type-safe version of ofAll(Object...) for 4 properties.
    • of

      public static <A,B,C,D,E> PropertyContainer of(Property<A> property0, A value0, boolean required0, Property<B> property1, B value1, boolean required1, Property<C> property2, C value2, boolean required2, Property<D> property3, D value3, boolean required3, Property<E> property4, E value4, boolean required4)
      Type-safe version of ofAll(Object...) for 5 properties.
    • toString

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

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object