Class Signal<T>

java.lang.Object
me.tamkungz.signals.Signal<T>
Type Parameters:
T - event payload type

public final class Signal<T> extends Object
Lightweight signal facade for a single payload type.

A signal is a view of one channel on a SignalBus: every Signal created for the same SignalKey shares the same listeners.

  • Method Details

    • bus

      public SignalBus bus()
      Returns the bus this signal lives on.
      Returns:
      the signal bus
    • key

      public SignalKey<T> key()
      Returns the key of this signal's channel.
      Returns:
      the signal key
    • eventType

      public Class<T> eventType()
      Returns the payload type of this signal.
      Returns:
      payload class
    • connect

      public Connection connect(Consumer<T> listener)
      Connects a listener.
      Parameters:
      listener - listener to call for every value
      Returns:
      the connection for this listener
    • connect

      public Connection connect(ConnectionGroup group, Consumer<T> listener)
      Connects a listener and tracks it in a group.
      Parameters:
      group - group that will disconnect the listener
      listener - listener to call for every value
      Returns:
      the connection for this listener
    • connectIf

      public Connection connectIf(Predicate<T> predicate, Consumer<T> listener)
      Connects a listener that only receives the values a predicate accepts.
      Parameters:
      predicate - decides which values reach the listener
      listener - listener to call for accepted values
      Returns:
      the connection for this listener
    • connectIf

      public Connection connectIf(ConnectionGroup group, Predicate<T> predicate, Consumer<T> listener)
      Connects a filtered listener and tracks it in a group.
      Parameters:
      group - group that will disconnect the listener
      predicate - decides which values reach the listener
      listener - listener to call for accepted values
      Returns:
      the connection for this listener
    • once

      public Connection once(Consumer<T> listener)
      Connects a listener that is disconnected after its first value.
      Parameters:
      listener - listener to call at most once
      Returns:
      the connection for this listener
    • once

      public Connection once(ConnectionGroup group, Consumer<T> listener)
      Connects a one-shot listener and tracks it in a group.
      Parameters:
      group - group that will disconnect the listener
      listener - listener to call at most once
      Returns:
      the connection for this listener
    • filter

      public Signal<T> filter(Predicate<T> predicate)
      Returns a new signal that only carries the values a predicate accepts.

      The new signal has its own channel; firing on it does not loop back into this signal. Clearing it also detaches it from this signal.

      Parameters:
      predicate - decides which values pass
      Returns:
      a derived signal of the same payload type
    • map

      public <R> Signal<R> map(Class<R> resultType, Function<T,R> mapper)
      Returns a new signal that carries every value of this signal converted by a function.

      The new signal has its own channel. Clearing it also detaches it from this signal.

      Type Parameters:
      R - payload type of the new signal
      Parameters:
      resultType - payload type of the new signal
      mapper - converts a value of this signal
      Returns:
      a derived signal of the result type
    • fire

      public void fire(T value)
      Delivers a value to every listener of this signal, on the calling thread.
      Parameters:
      value - value to deliver
    • listenerCount

      public int listenerCount()
      Counts the listeners connected to this signal's channel.
      Returns:
      number of connected listeners
    • clear

      public void clear()
      Disconnects every listener of this signal's channel, including ones connected through other Signal instances for the same key. For a signal made by filter(Predicate) or map(Class, Function) it also detaches the signal from its source.