CNodeAddress
The node address is the rear half of a target address, consisting of NodeId and SegmentId. The node address can be used to identify a node precisely.
Constructors and static factory methods
The default constructor is used to create the local node address, i.e. the address, which belongs to the own node.
final CNodeAddress n = new CNodeAddress();
An invalid or empty node address is created with the constructor, which takes
a Boolean. The value does not matter. However, it is better to use the
EMPTY public field here.
final CNodeAddress n1 = new CNodeAddress(true);
final CNodeAddress n2 = new CNodeAddress(false);
final CNodeAddress n3 = CNodeAddress.EMPTY;
Another variant accepts a NodeId. If this is invalid, the local NodeId is used. The local ID is also used for the SegmentId.
final CNodeId nodeId = ...
final CNodeAddress n = new CNodeAddress(nodeId);
Once you have the NodeId and SegmentId, you can create the corresponding node address .
final CNodeId nodeId = ...
final CSegmentId segmentId = ...
final CNodeAddress n = new CNodeAddress(nodeId,
segmentId);
For testing purposes, there is a method that provides random node addresses.
@NotNull
public static CNodeAddress random();
Getter
The following getters are available:
@NotNull
public CNodeId getNodeId();
@NotNull
public CSegmentId getSegmentId();
The local address can be obtained via:
@NotNull
public static CNodeAddress getLocal();
Validation
Addresses can be checked to see if they are valid.
public static boolean isValid(@Nullable final CNodeAddress aValue);
The following method checks if the address corresponds to the local
node. Null values and EMPTY values are interpreted as local.
public boolean isLocalNode();
An address is complete if NodeId and SegmentId are not empty:
public boolean isComplete();
String I/O
A node address can be output as a string. The NodeId is separated from the SegmentId with a dot. Invalid components are output as an empty string. A node address in the form of a string can also be read in again:
final CNodeAddress n = CNodeAddress.fromString("Hello.World");
final String s = n.valueToString();
Notice
The toString() method may output a shortened string that cannot be converted
back to a node address. It is better to use the valueToString() method.
Stream I/O
Objects can be written to a DataOutput stream and read from a DataInput
stream. Null values are taken into account.
final DataOutput out = ...
final CNodeAddress address1 = CNodeAddress.fromString("node.segment");
CNodeAddress.toStream(out,
address1);
final DataInput in = ...
final CNodeAddress address2 = CNodeAddress.fromStream(in);
Comparison
CNodeAddress supports equals(), hashCode() and implements the Comparable
interface.
There are two other methods that compare the NodeId and the SegmentId respectively.
public boolean matchNodeId(@Nullable final CNodeId aNodeId);
public boolean matchSegmentId(@Nullable final CSegmentId aSegmentId);
Another method realizes a comparison of two addresses considering null values:
public static boolean isEqual(@Nullable final CNodeAddress aValue1,
@Nullable final CNodeAddress aValue2);
There is a special method that compares two addresses, taking into account, that null or EMPTY values correspond to the local address.
public static boolean logicalEquals(@Nullable final CNodeAddress aNodeId1,
@Nullable final CNodeAddress aNodeId2);