java.lang.Object
net.i2p.data.TunnelId

public class TunnelIdextends Object
Tunnel identifier: a 32-bit unsigned value, held in a long, that names a tunnel within the network database and in lease sets.

Value:

  • Valid range is 1 to MAX_ID_VALUE (0xffffffff). 0 is reserved for a direct reply in a DatabaseStoreMessage, and the no-argument constructor leaves the invalid value -1 in place until the ID is set by a constructor argument, setTunnelId(long) or readBytes(InputStream).
  • Uniqueness is only local: each router picks and enforces its own IDs, nothing here generates or checks them.

Comparison:

Mutability and thread safety:

  • Not immutable: the ID is a plain non-volatile long that setTunnelId(long), readBytes(InputStream) and the constructors can replace, even after the object has been shared. Publish or synchronize the object, or replace it, rather than mutating a TunnelId another thread may already hold.
  • A TunnelId whose ID is set before publication and never changed afterwards can be shared freely between threads.
  • Field Summary

    Modifier and Type
    Field
    Description
    private long
    LOCKING: this, see setTunnelId(long).
    static final long
    Maximum tunnel ID value (2^32 - 1).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a TunnelId with the invalid value -1, for reading from a stream or for setTunnelId().
    TunnelId(long id)
    1 to 0xffffffff.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    boolean
    Two TunnelIds are equal when their IDs are equal.
    long
    Tunnel ID, or -1 if not set or not yet read.
    int
    The low 32 bits of the ID, which is the whole valid range, or -1 if the ID is not set.
    void
    Read the tunnel ID from a stream.
    final void
    setTunnelId(long id)
    Sets the tunnel ID for this lease, replacing any previous value.
    The ID in decimal.
    void
    Write the tunnel ID to a stream.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • TunnelId

      public TunnelId()
      Create a TunnelId with the invalid value -1, for reading from a stream or for setTunnelId().
    • TunnelId

      public TunnelId(long id)
      1 to 0xffffffff.
      Parameters:
      id - 1 to 0xffffffff
      Throws:
      IllegalArgumentException - if less than or equal to zero or greater than max value
  • Method Details

    • getTunnelId

      public long getTunnelId()
      Tunnel ID, or -1 if not set or not yet read.
      Returns:
      the tunnel ID
    • setTunnelId

      public final void setTunnelId(long id)
      Sets the tunnel ID for this lease, replacing any previous value.
      Parameters:
      id - 1 to 0xffffffff
      Throws:
      IllegalArgumentException - if less than or equal to zero or greater than max value
    • readBytes

      public void readBytes(InputStream in) throws DataFormatException, IOException
      Read the tunnel ID from a stream.
      Parameters:
      in - the input stream to read from
      Throws:
      DataFormatException - if the data is invalid
      IOException - if there is an error reading
    • writeBytes

      public void writeBytes(OutputStream out) throws DataFormatException, IOException
      Write the tunnel ID to a stream.
      Parameters:
      out - the output stream to write to
      Throws:
      DataFormatException - if the data is invalid
      IOException - if there is an error writing
    • equals

      public boolean equals(Object obj)
      Two TunnelIds are equal when their IDs are equal. Two unset TunnelIds (both -1) are equal to each other.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare
      Returns:
      true if the IDs match
    • hashCode

      public int hashCode()
      The low 32 bits of the ID, which is the whole valid range, or -1 if the ID is not set.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • toString

      public String toString()
      The ID in decimal.
      Overrides:
      toString in class Object
      Returns:
      a string representation