Class RangeAttribute<T extends Number & Comparable<T>>

java.lang.Object
de.gurkenlabs.litiengine.attributes.Attribute<T>
de.gurkenlabs.litiengine.attributes.RangeAttribute<T>
Type Parameters:
T - the type of the attribute value, which must be a Number and Comparable
All Implemented Interfaces:
IAttribute<T>, Serializable

public class RangeAttribute<T extends Number & Comparable<T>> extends Attribute<T> implements Serializable

Represents an attribute with a defined range, extending the base Attribute class. The attribute value is constrained between a minimum and maximum value.

This class is XML-serializable via JAXB. The base value, min and max are persisted as XML attributes; modifiers and property change support are runtime-only state and are not serialized.

See Also:
  • Constructor Details

    • RangeAttribute

      public RangeAttribute()
    • RangeAttribute

      public RangeAttribute(T initialValue, T min, T max)
      Constructs a new RangeAttribute with the specified initial value, minimum value, and maximum value. Ensures that the minimum value is not greater than the maximum value.
      Parameters:
      initialValue - the initial value of the attribute
      min - the minimum value of the attribute
      max - the maximum value of the attribute
      Throws:
      IllegalArgumentException - if the minimum value is greater than the maximum value
    • RangeAttribute

      public RangeAttribute(T min, T max)
      Constructs a new RangeAttribute with the specified minimum and maximum values, using min as the initial base value.
      Parameters:
      min - the minimum value of the attribute
      max - the maximum value of the attribute
      Throws:
      IllegalArgumentException - if the minimum value is greater than the maximum value
  • Method Details

    • getModifiedValue

      public T getModifiedValue()
      Gets the current value of the attribute, ensuring it is within the defined range.
      Specified by:
      getModifiedValue in interface IAttribute<T extends Number & Comparable<T>>
      Overrides:
      getModifiedValue in class Attribute<T extends Number & Comparable<T>>
      Returns:
      the current value of the attribute
    • getValue

      public T getValue()

      Gets the base value of the attribute.

      The base value is intentionally not serialized for a RangeAttribute. Range attributes describe a value range via min and max (for example particle emitter parameters); persisting a single base value alongside the range is not meaningful, since consumers such as the particle system always draw a fresh random sample between min and max (see getRandomNumber()). Suppression of the inherited value XML attribute is implemented via the JAXB beforeMarshal / afterMarshal callbacks below.

      Specified by:
      getValue in interface IAttribute<T extends Number & Comparable<T>>
      Overrides:
      getValue in class Attribute<T extends Number & Comparable<T>>
      Returns:
      the base value of the attribute
    • getMin

      public T getMin()
      Gets the base minimum value of the attribute.
      Returns:
      the minimum value of the attribute
    • getModifiedMin

      public T getModifiedMin()
      Computes the modified minimum value by applying all active modifiers to the base minimum value.
      Returns:
      the computed modified minimum value
    • setMin

      public void setMin(T min)
      Sets the minimum value of the attribute and notifies listeners of the change.
      Parameters:
      min - the new minimum value
    • getMax

      public T getMax()
      Gets the base maximum value of the attribute.
      Returns:
      the maximum value of the attribute
    • getModifiedMax

      public T getModifiedMax()
      Computes the modified maximum value by applying all active modifiers to the base maximum value.
      Returns:
      the computed modified maximum value
    • setMax

      public void setMax(T max)
      Sets the maximum value of the attribute and notifies listeners of the change.
      Parameters:
      max - the new maximum value
    • addMinListener

      public void addMinListener(PropertyChangeListener listener)
      Adds a PropertyChangeListener for the minimum value.
      Parameters:
      listener - the listener to be added
    • removeMinListener

      public void removeMinListener(PropertyChangeListener listener)
      Removes a PropertyChangeListener for the minimum value.
      Parameters:
      listener - the listener to be removed
    • addMaxListener

      public void addMaxListener(PropertyChangeListener listener)
      Adds a PropertyChangeListener for the maximum value.
      Parameters:
      listener - the listener to be added
    • removeMaxListener

      public void removeMaxListener(PropertyChangeListener listener)
      Removes a PropertyChangeListener for the maximum value.
      Parameters:
      listener - the listener to be removed
    • getMinModifiers

      public List<AttributeModifier<T>> getMinModifiers()
      Gets the list of minimum value modifiers.
      Returns:
      the list of minimum value modifiers
    • getMaxModifiers

      public List<AttributeModifier<T>> getMaxModifiers()
      Gets the list of maximum value modifiers.
      Returns:
      the list of maximum value modifiers
    • addMinModifier

      public void addMinModifier(AttributeModifier<T> modifier)
      Adds a modifier to the list of minimum value modifiers and notifies listeners of the change.
      Parameters:
      modifier - the modifier to add
    • removeMinModifier

      public void removeMinModifier(AttributeModifier<T> modifier)
      Removes a modifier from the list of minimum value modifiers and notifies listeners of the change.
      Parameters:
      modifier - the modifier to remove
    • addMaxModifier

      public void addMaxModifier(AttributeModifier<T> modifier)
      Adds a modifier to the list of maximum value modifiers and notifies listeners of the change.
      Parameters:
      modifier - the modifier to add
    • removeMaxModifier

      public void removeMaxModifier(AttributeModifier<T> modifier)
      Removes a modifier from the list of maximum value modifiers and notifies listeners of the change.
      Parameters:
      modifier - the modifier to remove
    • getRatio

      public float getRatio()
      Gets the ratio of the current value to the maximum value as a float. This is calculated as the current value divided by the maximum value.
      Returns:
      The ratio of the current value to the maximum value.
    • getRandomNumber

      public Number getRandomNumber()

      Returns a uniformly distributed pseudo-random number between this attribute's modified minimum and modified maximum values.

      This is intended for use-cases that need to draw a random sample from a configured range (e.g. particle parameters used by an emitter). If modifiedMin >= modifiedMax, the modified minimum is returned.

      Returns:
      a Number between getModifiedMin() (inclusive) and getModifiedMax() (exclusive), or getModifiedMin() if the range is empty
    • setValue

      public void setValue(T newValue)
      Description copied from class: Attribute
      Sets the base value of the attribute and fires a property change event.
      Specified by:
      setValue in interface IAttribute<T extends Number & Comparable<T>>
      Overrides:
      setValue in class Attribute<T extends Number & Comparable<T>>
      Parameters:
      newValue - the new base value of the attribute