Class Fact<T extends @Nullable Object>

java.lang.Object
io.github.brantunger.unruly.api.Fact<T>
Type Parameters:
T - The object/value type of the fact. A fact's value can be null.
All Implemented Interfaces:
FactReference<T>

public final class Fact<T extends @Nullable Object> extends Object implements FactReference<T>
The built-in, immutable FactReference. A rule sees the fact's value under the fact's name, so a fact named applicant is applicant in a condition or action. The value object isn't copied: a rule or a listener gets the same object the fact holds.

A fact's name and value can't change. To give a name a new value, store a new fact, as FactStore.setValue(String, Object) does. The value object itself isn't copied, so changes to it are seen everywhere it's held.

See Also:
  • Constructor Details

    • Fact

      public Fact(String name, T value)
      Instantiate a Fact from the name and value.
      Parameters:
      name - The name of the fact
      value - The value of the fact
      Throws:
      NullPointerException - if name is null
    • Fact

      public Fact(FactReference<? extends T> fact)
      Instantiate a Fact from another Fact object. The source may hold a narrower type, so a Fact<String> can be copied into a Fact<Object>.
      Parameters:
      fact - The existing Fact object to get the value and name from
      Throws:
      NullPointerException - if fact or its name is null
  • Method Details

    • getName

      public String getName()
      Description copied from interface: FactReference
      Gets the name of the fact.
      Specified by:
      getName in interface FactReference<T extends @Nullable Object>
      Returns:
      The name
    • getValue

      public T getValue()
      Description copied from interface: FactReference
      Gets the value of the fact. A run calls it once, when it starts.

      If it throws, run() or runWithResult() throws that exception unchanged before any listener callback, so no listener hears of the run, and the engine doesn't log it; see Implementing FactStore.

      Specified by:
      getValue in interface FactReference<T extends @Nullable Object>
      Returns:
      The value, which can be null
    • equals

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

      public int hashCode()
      Based on the name and value. A fact whose value is changed in place, such as a list that gains an element, gets a different hash code, so don't change a value while its fact is in a hashed collection such as a HashSet.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code of the name and value
    • toString

      public String toString()
      Overrides:
      toString in class Object