Class MachineState
- Direct Known Subclasses:
MachineStateResponse
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
FieldsModifier and TypeFieldDescriptionstatic final MachineStateA sentinel value representing a "not found" machine state.static final StringThe delimiter used when serializing machine states to string format. -
Constructor Summary
ConstructorsConstructorDescriptionMachineState(int machineId, long lastTimeStamp) Create a new MachineState with the specified machine ID and timestamp. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCheck if this machine state is equal to another object.longGet the last activity timestamp.intGet the machine ID.inthashCode()Get the hash code for this machine state.static MachineStateof(int machineId) Create a new MachineState with the specified machine ID and current timestamp.static MachineStateof(int machineId, long lastStamp) Create a new MachineState with the specified machine ID and timestamp.static MachineStateCreate a new MachineState from its string representation.Convert this machine state to its string representation.toString()Get the string representation of this machine state.
-
Field Details
-
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
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 IDlastTimeStamp- 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
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.
-
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. -
toString
Get the string representation of this machine state. -
toStateString
Convert this machine state to its string representation.The string format is "machineId|lastTimeStamp", using the
STATE_DELIMITERto separate the components.- Returns:
- The string representation
-
of
Create a new MachineState with the specified machine ID and timestamp.- Parameters:
machineId- The machine IDlastStamp- The last activity timestamp- Returns:
- A new MachineState instance
-
of
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
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
-