From 84752a575492cccbf978f611047bc8c82aef00a4 Mon Sep 17 00:00:00 2001 From: Kevin Herron Date: Sun, 26 Jan 2025 14:08:31 -0800 Subject: [PATCH] ~ javadoc, toString() cleanup, DynamicType::getDataTypeDefinition --- .../opcua/sdk/core/types/DynamicEnumType.java | 27 ++++++++++-- .../sdk/core/types/DynamicOptionSetType.java | 42 +++++++++++++++---- .../sdk/core/types/DynamicStructType.java | 17 +++++++- .../opcua/sdk/core/types/DynamicType.java | 12 ++++++ .../sdk/core/types/DynamicUnionType.java | 14 +++++-- 5 files changed, 98 insertions(+), 14 deletions(-) diff --git a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicEnumType.java b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicEnumType.java index 26f75fc18..ac83b0eb6 100644 --- a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicEnumType.java +++ b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicEnumType.java @@ -10,6 +10,7 @@ package org.eclipse.milo.opcua.sdk.core.types; +import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNullElse; import java.util.Objects; @@ -33,6 +34,7 @@ public final class DynamicEnumType extends DynamicType implements UaEnumeratedTy public DynamicEnumType(DataType dataType, int value) { this.dataType = dataType; + this.value = value; EnumDefinition definition = (EnumDefinition) dataType.getDataTypeDefinition(); assert definition != null; @@ -41,8 +43,6 @@ public DynamicEnumType(DataType dataType, int value) { for (EnumField field : fields) { if (field.getValue() == value) { - this.value = field.getValue().intValue(); - this.name = field.getName() != null ? field.getName() : ""; this.displayName = field.getDisplayName() != null ? field.getDisplayName() : LocalizedText.NULL_VALUE; @@ -71,14 +71,34 @@ public DataType getDataType() { return dataType; } + @Override + public EnumDefinition getDataTypeDefinition() { + return (EnumDefinition) requireNonNull(dataType.getDataTypeDefinition()); + } + + /** + * Get the name associated with this enum value. + * + * @return the name associated with this enum value. + */ public String getName() { return name; } + /** + * Get the display name associated with this enum value. + * + * @return the display name associated with this enum value. + */ public LocalizedText getDisplayName() { return displayName; } + /** + * Get the description associated with this enum value. + * + * @return the description associated with this enum value. + */ public LocalizedText getDescription() { return description; } @@ -100,7 +120,8 @@ public int hashCode() { @Override public String toString() { return new StringJoiner(", ", DynamicEnumType.class.getSimpleName() + "[", "]") - .add("name='" + name + "'") + .add("dataType=" + dataType.getNodeId().toParseableString()) + .add("name=" + name) .add("value=" + value) .toString(); } diff --git a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicOptionSetType.java b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicOptionSetType.java index 0bd42d000..906d877c3 100644 --- a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicOptionSetType.java +++ b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicOptionSetType.java @@ -10,12 +10,14 @@ package org.eclipse.milo.opcua.sdk.core.types; +import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNullElse; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; import java.util.StringJoiner; import org.eclipse.milo.opcua.sdk.core.typetree.DataType; import org.eclipse.milo.opcua.stack.core.types.UaStructuredType; @@ -25,7 +27,6 @@ import org.eclipse.milo.opcua.stack.core.types.structured.EnumDefinition; import org.eclipse.milo.opcua.stack.core.types.structured.EnumField; import org.eclipse.milo.opcua.stack.core.util.Lazy; -import org.jspecify.annotations.Nullable; public final class DynamicOptionSetType extends DynamicType implements UaStructuredType { @@ -53,6 +54,11 @@ public DataType getDataType() { return dataType; } + @Override + public EnumDefinition getDataTypeDefinition() { + return (EnumDefinition) requireNonNull(dataType.getDataTypeDefinition()); + } + public LinkedHashMap getMembers() { return members; } @@ -73,22 +79,43 @@ public void setValidBits(ByteString validBits) { getMembers().put("ValidBits", validBits); } - public @Nullable String getName(int bitIndex) { + /** + * Get the name of the field at the given bit index. + * + * @param bitIndex the bit index. + * @return the name of the field at the given bit index, or {@link Optional#empty()} if no field + * exists at the given bit index. + */ + public Optional getName(int bitIndex) { EnumField enumField = getFieldMap().get(bitIndex); - return enumField != null ? enumField.getName() : null; + return Optional.ofNullable(enumField).map(EnumField::getName); } - public @Nullable LocalizedText getDisplayName(int bitIndex) { + /** + * Get the display name of the field at the given bit index. + * + * @param bitIndex the bit index. + * @return the display name of the field at the given bit index, or {@link Optional#empty()} if no + * field exists at the given bit index. + */ + public Optional getDisplayName(int bitIndex) { EnumField enumField = getFieldMap().get(bitIndex); - return enumField != null ? enumField.getDisplayName() : null; + return Optional.ofNullable(enumField).map(EnumField::getDisplayName); } - public @Nullable LocalizedText getDescription(int bitIndex) { + /** + * Get the description of the field at the given bit index. + * + * @param bitIndex the bit index. + * @return the description of the field at the given bit index, or {@link Optional#empty()} if no + * field exists at the given bit index. + */ + public Optional getDescription(int bitIndex) { EnumField enumField = getFieldMap().get(bitIndex); - return enumField != null ? enumField.getDescription() : null; + return Optional.ofNullable(enumField).map(EnumField::getDescription); } private Map getFieldMap() { @@ -112,6 +139,7 @@ private Map getFieldMap() { @Override public String toString() { return new StringJoiner(", ", DynamicOptionSetType.class.getSimpleName() + "[", "]") + .add("dataType=" + dataType.getNodeId().toParseableString()) .add("value=" + toBitString(getValue())) .add("validBits=" + toBitString(getValidBits())) .toString(); diff --git a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicStructType.java b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicStructType.java index 1153f0e13..bc803226b 100644 --- a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicStructType.java +++ b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicStructType.java @@ -10,6 +10,8 @@ package org.eclipse.milo.opcua.sdk.core.types; +import static java.util.Objects.requireNonNull; + import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Objects; @@ -20,6 +22,7 @@ import org.eclipse.milo.opcua.stack.core.types.UaStructuredType; import org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId; import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; +import org.eclipse.milo.opcua.stack.core.types.structured.StructureDefinition; public final class DynamicStructType extends DynamicType implements UaStructuredType { @@ -36,6 +39,18 @@ public DataType getDataType() { return dataType; } + @Override + public StructureDefinition getDataTypeDefinition() { + return (StructureDefinition) requireNonNull(dataType.getDataTypeDefinition()); + } + + /** + * Get the members of this struct. + * + *

The members are a map of member names to their values. + * + * @return the members of this struct. + */ public LinkedHashMap getMembers() { return members; } @@ -79,7 +94,7 @@ public int hashCode() { @Override public String toString() { var joiner = new StringJoiner(", ", DynamicStructType.class.getSimpleName() + "[", "]"); - joiner.add("dataType=" + dataType.getNodeId()); + joiner.add("dataType=" + dataType.getNodeId().toParseableString()); joiner.add("members=" + joinMembers(members)); return joiner.toString(); } diff --git a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicType.java b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicType.java index 6a4bc2c20..3d9b7763b 100644 --- a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicType.java +++ b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicType.java @@ -12,6 +12,7 @@ import org.eclipse.milo.opcua.sdk.core.typetree.DataType; import org.eclipse.milo.opcua.stack.core.types.UaDataType; +import org.eclipse.milo.opcua.stack.core.types.structured.DataTypeDefinition; /** * Base class for an instance of a "dynamic" type, i.e. one that is defined dynamically at runtime @@ -26,4 +27,15 @@ public abstract sealed class DynamicType implements UaDataType * @return the {@link DataType} that defines this type. */ public abstract DataType getDataType(); + + /** + * Get the {@link DataTypeDefinition} that defines this type. + * + *

Subclasses may refine the return type to either {@link + * org.eclipse.milo.opcua.stack.core.types.structured.EnumDefinition} or {@link + * org.eclipse.milo.opcua.stack.core.types.structured.StructureDefinition}. + * + * @return the {@link DataTypeDefinition} that defines this type. + */ + public abstract DataTypeDefinition getDataTypeDefinition(); } diff --git a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicUnionType.java b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicUnionType.java index dfa7cf56b..c125e396c 100644 --- a/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicUnionType.java +++ b/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/types/DynamicUnionType.java @@ -10,6 +10,8 @@ package org.eclipse.milo.opcua.sdk.core.types; +import static java.util.Objects.requireNonNull; + import java.util.Objects; import java.util.Optional; import java.util.StringJoiner; @@ -17,6 +19,7 @@ import org.eclipse.milo.opcua.stack.core.types.UaStructuredType; import org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId; import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; +import org.eclipse.milo.opcua.stack.core.types.structured.StructureDefinition; import org.jspecify.annotations.Nullable; public final class DynamicUnionType extends DynamicType implements UaStructuredType { @@ -61,6 +64,11 @@ public DataType getDataType() { return dataType; } + @Override + public StructureDefinition getDataTypeDefinition() { + return (StructureDefinition) requireNonNull(dataType.getDataTypeDefinition()); + } + /** * Get the value of this union. * @@ -104,7 +112,7 @@ public int hashCode() { @Override public String toString() { var joiner = new StringJoiner(", ", DynamicUnionType.class.getSimpleName() + "[", "]"); - joiner.add("dataType=" + dataType.getNodeId()); + joiner.add("dataType=" + dataType.getNodeId().toParseableString()); if (value != null) { joiner.add("%s=%s".formatted(value.fieldName(), value.fieldValue())); } else { @@ -129,8 +137,8 @@ public static DynamicUnionType ofNull(DataType dataType) { */ public record UnionValue(String fieldName, Object fieldValue) { public UnionValue { - Objects.requireNonNull(fieldName); - Objects.requireNonNull(fieldValue); + requireNonNull(fieldName); + requireNonNull(fieldValue); } } }