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
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
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();
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();
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();
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();
toString() method may output a shortened string that cannot be converted back to a node
It is better to use the
Objects can be written to a
DataOutput stream and read from a
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);
hashCode() and implements the
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);