Class ConnectionGroup

java.lang.Object
me.tamkungz.signals.connection.ConnectionGroup
All Implemented Interfaces:
Connection

public final class ConnectionGroup extends Object implements Connection
Tracks many connections and disconnects them all at once, for example when a plugin disables.

Connections added after the group was disconnected are disconnected immediately.

  • Constructor Details

    • ConnectionGroup

      public ConnectionGroup()
  • Method Details

    • create

      public static ConnectionGroup create()
      Creates an empty group.
      Returns:
      a new, connected group
    • of

      public static ConnectionGroup of(Connection... connections)
      Creates a group that tracks the given connections.
      Parameters:
      connections - connections to track, none of them may be null
      Returns:
      a new, connected group
    • add

      public <T extends Connection> T add(T connection)
      Tracks a connection. If the group is already disconnected, the connection is disconnected right away.
      Type Parameters:
      T - connection type
      Parameters:
      connection - connection to track
      Returns:
      the same connection
    • size

      public int size()
      Returns how many connections the group currently tracks.

      Connections that were disconnected on their own are dropped as the group grows, so this can include a few that are no longer connected.

      Returns:
      number of tracked connections
    • disconnect

      public void disconnect()
      Disconnects every tracked connection and stops accepting new ones.

      A connection that fails to disconnect does not stop the rest; the first failure is rethrown afterwards with the others attached as suppressed exceptions.

      Specified by:
      disconnect in interface Connection
    • isConnected

      public boolean isConnected()
      Description copied from interface: Connection
      Tells whether this connection is still active.
      Specified by:
      isConnected in interface Connection
      Returns:
      true until Connection.disconnect() has been called