Class Mutable<T>

java.lang.Object
org.team1126.lib.util.Mutable<T>
All Implemented Interfaces:
Consumer<T>, Supplier<T>

public class Mutable<T> extends Object implements Supplier<T>, Consumer<T>
A simple mutable object that stores a value.

Useful for declaring primitives in an enclosing scope to be mutated inside a lambda, as lambdas prohibit capturing non final variables, with the exception of instance variables. This use case however should be executed with caution, as a race condition may occur if the lambda escapes its capturing thread.

Fortunately for our purposes, using this class in a command factory to provide stateful behavior does not suffer from aforementioned race conditions as commands are invoked synchronously.

  • Field Details

    • value

      public T value
  • Constructor Details

    • Mutable

      public Mutable(T value)
      Create the mutable object.
      Parameters:
      value - The initial value to store.
  • Method Details

    • get

      public T get()
      Gets the current value.
      Specified by:
      get in interface Supplier<T>
      Returns:
      The current value.
    • accept

      public void accept(T value)
      Sets a new value.
      Specified by:
      accept in interface Consumer<T>
      Parameters:
      value - The new value.