Class MachineState

java.lang.Object
me.ahoo.cosid.machine.MachineState
Direct Known Subclasses:
MachineStateResponse

@Immutable public class MachineState extends Object
Machine state representing the status of a distributed machine instance.

This immutable class encapsulates the state information for a machine instance in a distributed ID generation system. It contains:

  • Machine ID: The unique identifier for this machine
  • Last Timestamp: The timestamp of the last activity

The machine state is used by MachineIdDistributor implementations to track and manage machine instances, ensuring that each machine has a unique identifier and that stale instances can be detected and cleaned up.

This class provides utility methods for:

  • Creating machine states with factory methods
  • Serializing/deserializing to string format
  • Equality and hash code computation
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final MachineState
    A sentinel value representing a "not found" machine state.
    static final String
    The delimiter used when serializing machine states to string format.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MachineState(int machineId, long lastTimeStamp)
    Create a new MachineState with the specified machine ID and timestamp.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Check if this machine state is equal to another object.
    long
    Get the last activity timestamp.
    int
    Get the machine ID.
    int
    Get the hash code for this machine state.
    of(int machineId)
    Create a new MachineState with the specified machine ID and current timestamp.
    of(int machineId, long lastStamp)
    Create a new MachineState with the specified machine ID and timestamp.
    of(String stateString)
    Create a new MachineState from its string representation.
    Convert this machine state to its string representation.
    Get the string representation of this machine state.

    Methods inherited from class java.lang.Object

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

    • NOT_FOUND

      public static final MachineState NOT_FOUND
      A sentinel value representing a "not found" machine state.

      This is used when no valid machine state can be found for a requested machine ID, allowing the framework to handle missing machine states gracefully without null checks.

    • STATE_DELIMITER

      public static final String STATE_DELIMITER
      The delimiter used when serializing machine states to string format.

      This character separates the machine ID and timestamp components in the string representation.

      See Also:
  • Constructor Details

    • MachineState

      public MachineState(int machineId, long lastTimeStamp)
      Create a new MachineState with the specified machine ID and timestamp.
      Parameters:
      machineId - The machine ID
      lastTimeStamp - The last activity timestamp
  • Method Details

    • getMachineId

      public int getMachineId()
      Get the machine ID.
      Returns:
      The machine ID
    • getLastTimeStamp

      public long getLastTimeStamp()
      Get the last activity timestamp.
      Returns:
      The last activity timestamp
    • equals

      public boolean equals(Object o)
      Check if this machine state is equal to another object.

      Two machine states are considered equal if they have the same machine ID, regardless of their timestamps.

      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare to
      Returns:
      true if the objects are equal, false otherwise
    • hashCode

      public int hashCode()
      Get the hash code for this machine state.

      The hash code is based solely on the machine ID to ensure consistency with the equals(Object) implementation.

      Overrides:
      hashCode in class Object
      Returns:
      The hash code
    • toString

      public String toString()
      Get the string representation of this machine state.
      Overrides:
      toString in class Object
      Returns:
      The string representation
    • toStateString

      public String toStateString()
      Convert this machine state to its string representation.

      The string format is "machineId|lastTimeStamp", using the STATE_DELIMITER to separate the components.

      Returns:
      The string representation
    • of

      public static MachineState of(int machineId, long lastStamp)
      Create a new MachineState with the specified machine ID and timestamp.
      Parameters:
      machineId - The machine ID
      lastStamp - The last activity timestamp
      Returns:
      A new MachineState instance
    • of

      public static MachineState of(int machineId)
      Create a new MachineState with the specified machine ID and current timestamp.
      Parameters:
      machineId - The machine ID
      Returns:
      A new MachineState instance with current timestamp
    • of

      public static MachineState of(String stateString)
      Create a new MachineState from its string representation.

      This method parses a string in the format "machineId|lastTimeStamp" and creates a corresponding MachineState instance.

      Parameters:
      stateString - The string representation
      Returns:
      A new MachineState instance
      Throws:
      IllegalArgumentException - if the string format is invalid