Class ObservableProperty<T>

java.lang.Object
io.github.cottonmc.cotton.gui.widget.data.ObservableProperty<T>
Type Parameters:
T - the contained value type
All Implemented Interfaces:
ObservableView<T>, Supplier<T>

public final class ObservableProperty<T> extends Object implements ObservableView<T>
An observable mutable property. Observable properties are containers for values that can be modified and listened to.

The naming convention for ObservableProperty getters follows the convention <property name>Property. For example, the WWidget.hovered property can be retrieved with hoveredProperty().

Since:
4.2.0
  • Method Details

    • empty

      public static <T> ObservableProperty.Builder<T> empty()
      Creates a "late init" property without an initial value. The created property will throw an exception if it has not been initialised yet.
      Type Parameters:
      T - the contained value type
      Returns:
      the created empty property builder
    • of

      public static <T> ObservableProperty.Builder<T> of(T initialValue)
      Creates a property with an initial value.
      Type Parameters:
      T - the contained value type
      Parameters:
      initialValue - the initial value
      Returns:
      the created property
    • hasValue

      public boolean hasValue()
      Description copied from interface: ObservableView
      Returns whether this property has been set to a value.
      Specified by:
      hasValue in interface ObservableView<T>
      Returns:
      whether this property has been set to a value
    • get

      public T get()
      Description copied from interface: ObservableView
      Returns the value of this property.
      Specified by:
      get in interface ObservableView<T>
      Specified by:
      get in interface Supplier<T>
      Returns:
      the value of this property
      See Also:
    • set

      public void set(T value)
      Sets this property to a constant value.
      Parameters:
      value - the new value
      Throws:
      NullPointerException - if the value is null and nulls aren't allowed
    • readOnly

      public ObservableView<T> readOnly()
      Returns a read-only view of this property. The result is not an instance of ObservableProperty, and thus can't be mutated.
      Returns:
      an observable view of this property
    • getName

      public String getName()
      Returns the name of this property.
      Returns:
      the name of this property
    • addListener

      public void addListener(ObservableView.ChangeListener<? super T> listener)
      Description copied from interface: ObservableView
      Adds a change listener to this property view.
      Specified by:
      addListener in interface ObservableView<T>
      Parameters:
      listener - the added listener
    • removeListener

      public void removeListener(ObservableView.ChangeListener<? super T> listener)
      Description copied from interface: ObservableView
      Removes a change listener from this property view if present.
      Specified by:
      removeListener in interface ObservableView<T>
      Parameters:
      listener - the removed listener