Skip to content

Commit

Permalink
~ javadoc, toString() cleanup, DynamicType::getDataTypeDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Jan 26, 2025
1 parent 9d889f0 commit 84752a5
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -53,6 +54,11 @@ public DataType getDataType() {
return dataType;
}

@Override
public EnumDefinition getDataTypeDefinition() {
return (EnumDefinition) requireNonNull(dataType.getDataTypeDefinition());
}

public LinkedHashMap<String, Object> getMembers() {
return members;
}
Expand All @@ -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<String> 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<LocalizedText> 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<LocalizedText> getDescription(int bitIndex) {
EnumField enumField = getFieldMap().get(bitIndex);

return enumField != null ? enumField.getDescription() : null;
return Optional.ofNullable(enumField).map(EnumField::getDescription);
}

private Map<Integer, EnumField> getFieldMap() {
Expand All @@ -112,6 +139,7 @@ private Map<Integer, EnumField> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -36,6 +39,18 @@ public DataType getDataType() {
return dataType;
}

@Override
public StructureDefinition getDataTypeDefinition() {
return (StructureDefinition) requireNonNull(dataType.getDataTypeDefinition());
}

/**
* Get the members of this struct.
*
* <p>The members are a map of member names to their values.
*
* @return the members of this struct.
*/
public LinkedHashMap<String, Object> getMembers() {
return members;
}
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
* <p>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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

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;
import org.eclipse.milo.opcua.sdk.core.typetree.DataType;
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 {
Expand Down Expand Up @@ -61,6 +64,11 @@ public DataType getDataType() {
return dataType;
}

@Override
public StructureDefinition getDataTypeDefinition() {
return (StructureDefinition) requireNonNull(dataType.getDataTypeDefinition());
}

/**
* Get the value of this union.
*
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
}

0 comments on commit 84752a5

Please sign in to comment.