OpcUaNodeId — Strongly typed NodeId
Overview
OPC UA node IDs are string-encoded by default ("ns=2;s=Foo"). OpcUaNodeId is an immutable value type that explicitly carries namespace / idType / identifier.
Corresponding section of the standard: Part 6 §5.2.2.
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| OpcUaNodeId.NULL | Static | Read | Empty Sentinel |
| OpcUaNodeId.fromNumeric(ns, id) | Factory | — | Digital |
| OpcUaNodeId.fromString(ns, id) | Factory | — | String type |
| OpcUaNodeId.fromGuid(ns, uuid) | Factory | — | GUID Type |
| OpcUaNodeId.fromOpaque(ns, bytes) | Factory | — | Opaque Type |
| OpcUaNodeId.parse(s) | Static | — | Parse, throw an IllegalArgumentException on failure |
| OpcUaNodeId.tryParse(s) | Static | — | Safe evaluation, returns Optional<OpcUaNodeId> |
| nid.toString() | — | — | Serialization |
| nid.namespaceIndex() / idType() / identifier() / isNull() | Properties | Read | Field |
Related Structures:
public enum NodeIdType {
NUMERIC, // Code i=, uint32
STRING, // Code s=, UTF-8
GUID, // Code g=, {xxxx-...}
OPAQUE // Code b=, base64
}
Code Example
import com.darra.opcua.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
// 1) Factory
OpcUaNodeId n1 = OpcUaNodeId.fromNumeric(0, 84); // i=84
OpcUaNodeId n2 = OpcUaNodeId.fromString(2, "Boiler1.Temp");
OpcUaNodeId n3 = OpcUaNodeId.fromGuid(3, UUID.randomUUID());
OpcUaNodeId n4 = OpcUaNodeId.fromOpaque(4, new byte[]{1, 2, 3});
// 2) Analysis
OpcUaNodeId n5 = OpcUaNodeId.parse("ns=2;s=Demo.Tag");
System.out.println("ns=" + n5.namespaceIndex() + ", type=" + n5.idType());
Optional<OpcUaNodeId> n6 = OpcUaNodeId.tryParse("ns=99;i=foo"); // empty
if (n6.isEmpty()) System.out.println("invalid");
// 3) Serialization
String s = n2.toString(); // "ns=2;s=Boiler1.Temp"
// 4) Convert SDK between and
try (OpcUaDataValue dv = ua.read(n2.toString())) { ... }
// 5) Dictionary key (immutable, automatic `equals/hashCode` method)
Map<OpcUaNodeId, String> dict = new HashMap<>();
dict.put(n2, "锅炉一温度");
// 6) Null
System.out.println(OpcUaNodeId.NULL.isNull()); // true
Best Practices
- Store strings in the configuration file; use parse to load them
- Use strong typing within the code
- Numeric is preferred (uint is faster)
- User input is always processed by tryParse first
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | DarraUa_NodeId |
| Parse | parse | parse | Parse | parse | DarraUa_NodeId_Parse |
| FromNumeric | from_numeric | fromNumeric | FromNumeric | from_numeric | DarraUa_NodeId_FromNumeric |