private void streamNormalizedNode(final NormalizedNodeStreamWriter writer, final byte nodeType) throws IOException {
switch (nodeType) {
- case LithiumNode.ANY_XML_NODE:
- streamAnyxml(writer);
- break;
- case LithiumNode.AUGMENTATION_NODE:
- streamAugmentation(writer);
- break;
- case LithiumNode.CHOICE_NODE:
- streamChoice(writer);
- break;
- case LithiumNode.CONTAINER_NODE:
- streamContainer(writer);
- break;
- case LithiumNode.LEAF_NODE:
- streamLeaf(writer);
- break;
- case LithiumNode.LEAF_SET:
- streamLeafSet(writer);
- break;
- case LithiumNode.ORDERED_LEAF_SET:
- streamOrderedLeafSet(writer);
- break;
- case LithiumNode.LEAF_SET_ENTRY_NODE:
- streamLeafSetEntry(writer);
- break;
- case LithiumNode.MAP_ENTRY_NODE:
- streamMapEntry(writer);
- break;
- case LithiumNode.MAP_NODE:
- streamMap(writer);
- break;
- case LithiumNode.ORDERED_MAP_NODE:
- streamOrderedMap(writer);
- break;
- case LithiumNode.UNKEYED_LIST:
- streamUnkeyedList(writer);
- break;
- case LithiumNode.UNKEYED_LIST_ITEM:
- streamUnkeyedListItem(writer);
- break;
- default:
- throw new InvalidNormalizedNodeStreamException("Unexpected node " + nodeType);
+ case LithiumNode.ANY_XML_NODE -> streamAnyxml(writer);
+ case LithiumNode.AUGMENTATION_NODE -> streamAugmentation(writer);
+ case LithiumNode.CHOICE_NODE -> streamChoice(writer);
+ case LithiumNode.CONTAINER_NODE -> streamContainer(writer);
+ case LithiumNode.LEAF_NODE -> streamLeaf(writer);
+ case LithiumNode.LEAF_SET -> streamLeafSet(writer);
+ case LithiumNode.ORDERED_LEAF_SET -> streamOrderedLeafSet(writer);
+ case LithiumNode.LEAF_SET_ENTRY_NODE -> streamLeafSetEntry(writer);
+ case LithiumNode.MAP_ENTRY_NODE -> streamMapEntry(writer);
+ case LithiumNode.MAP_NODE -> streamMap(writer);
+ case LithiumNode.ORDERED_MAP_NODE -> streamOrderedMap(writer);
+ case LithiumNode.UNKEYED_LIST -> streamUnkeyedList(writer);
+ case LithiumNode.UNKEYED_LIST_ITEM -> streamUnkeyedListItem(writer);
+ default -> throw new InvalidNormalizedNodeStreamException("Unexpected node " + nodeType);
}
}
final String readCodedString() throws IOException {
final byte valueType = input.readByte();
- switch (valueType) {
- case LithiumTokens.IS_NULL_VALUE:
- return null;
- case LithiumTokens.IS_CODE_VALUE:
+ return switch (valueType) {
+ case LithiumTokens.IS_NULL_VALUE -> null;
+ case LithiumTokens.IS_CODE_VALUE -> {
final int code = input.readInt();
try {
- return codedStringMap.get(code);
+ yield codedStringMap.get(code);
} catch (IndexOutOfBoundsException e) {
throw new IOException("String code " + code + " was not found", e);
}
- case LithiumTokens.IS_STRING_VALUE:
+ }
+ case LithiumTokens.IS_STRING_VALUE -> {
final String value = input.readUTF().intern();
codedStringMap.add(value);
- return value;
- default:
- throw new IOException("Unhandled string value type " + valueType);
- }
+ yield value;
+ }
+ default -> throw new IOException("Unhandled string value type " + valueType);
+ };
}
private Set<QName> readQNameSet() throws IOException {
private Object readObject() throws IOException {
byte objectType = input.readByte();
- switch (objectType) {
- case LithiumValue.BITS_TYPE:
- return readObjSet();
-
- case LithiumValue.BOOL_TYPE:
- return input.readBoolean();
-
- case LithiumValue.BYTE_TYPE:
- return input.readByte();
-
- case LithiumValue.INT_TYPE:
- return input.readInt();
-
- case LithiumValue.LONG_TYPE:
- return input.readLong();
-
- case LithiumValue.QNAME_TYPE:
- return readQName();
-
- case LithiumValue.SHORT_TYPE:
- return input.readShort();
-
- case LithiumValue.STRING_TYPE:
- return input.readUTF();
-
- case LithiumValue.STRING_BYTES_TYPE:
- return readStringBytes();
-
- case LithiumValue.BIG_DECIMAL_TYPE:
- return Decimal64.valueOf(input.readUTF());
-
- case LithiumValue.BIG_INTEGER_TYPE:
- return new BigInteger(input.readUTF());
-
- case LithiumValue.BINARY_TYPE:
+ return switch (objectType) {
+ case LithiumValue.BITS_TYPE -> readObjSet();
+ case LithiumValue.BOOL_TYPE -> input.readBoolean();
+ case LithiumValue.BYTE_TYPE -> input.readByte();
+ case LithiumValue.INT_TYPE -> input.readInt();
+ case LithiumValue.LONG_TYPE -> input.readLong();
+ case LithiumValue.QNAME_TYPE -> readQName();
+ case LithiumValue.SHORT_TYPE -> input.readShort();
+ case LithiumValue.STRING_TYPE -> input.readUTF();
+ case LithiumValue.STRING_BYTES_TYPE -> readStringBytes();
+ case LithiumValue.BIG_DECIMAL_TYPE -> Decimal64.valueOf(input.readUTF());
+ case LithiumValue.BIG_INTEGER_TYPE -> new BigInteger(input.readUTF());
+ case LithiumValue.BINARY_TYPE -> {
byte[] bytes = new byte[input.readInt()];
input.readFully(bytes);
- return bytes;
-
- case LithiumValue.YANG_IDENTIFIER_TYPE:
- return readYangInstanceIdentifierInternal();
-
- case LithiumValue.EMPTY_TYPE:
+ yield bytes;
+ }
+ case LithiumValue.YANG_IDENTIFIER_TYPE -> readYangInstanceIdentifierInternal();
+ case LithiumValue.EMPTY_TYPE, LithiumValue.NULL_TYPE ->
// Leaf nodes no longer allow null values and thus we no longer emit null values. Previously, the "empty"
// yang type was represented as null so we translate an incoming null value to Empty. It was possible for
// a BI user to set a string leaf to null and we're rolling the dice here but the chances for that are
// very low. We'd have to know the yang type but, even if we did, we can't let a null value pass upstream
// so we'd have to drop the leaf which might cause other issues.
- case LithiumValue.NULL_TYPE:
- return Empty.value();
-
- default:
- return null;
- }
+ Empty.value();
+ default -> null;
+ };
}
private String readStringBytes() throws IOException {
public final PathArgument readPathArgument() throws IOException {
// read Type
int type = input.readByte();
-
- switch (type) {
- case LithiumPathArgument.AUGMENTATION_IDENTIFIER:
- return readAugmentationIdentifier();
- case LithiumPathArgument.NODE_IDENTIFIER:
- return readNodeIdentifier();
- case LithiumPathArgument.NODE_IDENTIFIER_WITH_PREDICATES:
- return readNormalizedNodeWithPredicates();
- case LithiumPathArgument.NODE_IDENTIFIER_WITH_VALUE:
- return new NodeWithValue<>(readQName(), readObject());
- default:
+ return switch (type) {
+ case LithiumPathArgument.AUGMENTATION_IDENTIFIER -> readAugmentationIdentifier();
+ case LithiumPathArgument.NODE_IDENTIFIER -> readNodeIdentifier();
+ case LithiumPathArgument.NODE_IDENTIFIER_WITH_PREDICATES -> readNormalizedNodeWithPredicates();
+ case LithiumPathArgument.NODE_IDENTIFIER_WITH_VALUE -> new NodeWithValue<>(readQName(), readObject());
+ default ->
// FIXME: throw hard error
- return null;
- }
+ null;
+ };
}
}
final Object value;
if ((nodeHeader & MagnesiumNode.PREDICATE_ONE) == MagnesiumNode.PREDICATE_ONE) {
- if (!(parent instanceof NodeIdentifierWithPredicates)) {
+ if (!(parent instanceof NodeIdentifierWithPredicates nip)) {
throw new InvalidNormalizedNodeStreamException("Invalid predicate leaf " + identifier + " in parent "
+ parent);
}
- value = ((NodeIdentifierWithPredicates) parent).getValue(identifier.getNodeType());
+ value = nip.getValue(identifier.getNodeType());
if (value == null) {
throw new InvalidNormalizedNodeStreamException("Failed to find predicate leaf " + identifier
+ " in parent " + parent);
final byte nodeHeader) throws IOException {
final NodeIdentifier nodeId = decodeNodeIdentifier(nodeHeader, parent);
- final int size;
- switch (mask(nodeHeader, MagnesiumNode.PREDICATE_MASK)) {
- case MagnesiumNode.PREDICATE_ZERO:
- size = 0;
- break;
- case MagnesiumNode.PREDICATE_ONE:
- size = 1;
- break;
- case MagnesiumNode.PREDICATE_1B:
- size = input.readUnsignedByte();
- break;
- case MagnesiumNode.PREDICATE_4B:
- size = input.readInt();
- break;
- default:
+ final int size = switch (mask(nodeHeader, MagnesiumNode.PREDICATE_MASK)) {
+ case MagnesiumNode.PREDICATE_ZERO -> 0;
+ case MagnesiumNode.PREDICATE_ONE -> 1;
+ case MagnesiumNode.PREDICATE_1B -> input.readUnsignedByte();
+ case MagnesiumNode.PREDICATE_4B -> input.readInt();
+ default ->
// ISE on purpose: this should never ever happen
throw new IllegalStateException("Failed to decode NodeIdentifierWithPredicates size from header "
- + nodeHeader);
- }
-
+ + nodeHeader);
+ };
final NodeIdentifierWithPredicates identifier = readNodeIdentifierWithPredicates(nodeId.getNodeType(), size);
LOG.trace("Streaming map entry node {}", identifier);
writer.startMapEntryNode(identifier, UNKNOWN_SIZE);
index = input.readInt();
break;
case MagnesiumNode.ADDR_PARENT:
- if (parent instanceof NodeIdentifier) {
- return (NodeIdentifier) parent;
+ if (parent instanceof NodeIdentifier nid) {
+ return nid;
}
throw new InvalidNormalizedNodeStreamException("Invalid node identifier reference to parent " + parent);
default:
@Override
public final void startLeafNode(final NodeIdentifier name) throws IOException {
final Object current = stack.peek();
- if (current instanceof NodeIdentifierWithPredicates) {
+ if (current instanceof NodeIdentifierWithPredicates nip) {
final QName qname = name.getNodeType();
- if (((NodeIdentifierWithPredicates) current).containsKey(qname)) {
+ if (nip.containsKey(qname)) {
writeQNameNode(MagnesiumNode.NODE_LEAF | MagnesiumNode.PREDICATE_ONE, qname);
stack.push(KEY_LEAF_STATE);
return;
@Override
final void writePathArgumentInternal(final PathArgument pathArgument) throws IOException {
- if (pathArgument instanceof NodeIdentifier) {
- writeNodeIdentifier((NodeIdentifier) pathArgument);
- } else if (pathArgument instanceof NodeIdentifierWithPredicates) {
- writeNodeIdentifierWithPredicates((NodeIdentifierWithPredicates) pathArgument);
- } else if (pathArgument instanceof AugmentationIdentifier) {
- writeAugmentationIdentifier((AugmentationIdentifier) pathArgument);
- } else if (pathArgument instanceof NodeWithValue) {
- writeNodeWithValue((NodeWithValue<?>) pathArgument);
- } else if (pathArgument instanceof MountPointIdentifier) {
- writeMountPointIdentifier((MountPointIdentifier) pathArgument);
+ if (pathArgument instanceof NodeIdentifier nid) {
+ writeNodeIdentifier(nid);
+ } else if (pathArgument instanceof NodeIdentifierWithPredicates nip) {
+ writeNodeIdentifierWithPredicates(nip);
+ } else if (pathArgument instanceof AugmentationIdentifier augid) {
+ writeAugmentationIdentifier(augid);
+ } else if (pathArgument instanceof NodeWithValue<?> niv) {
+ writeNodeWithValue(niv);
+ } else if (pathArgument instanceof MountPointIdentifier mpid) {
+ writeMountPointIdentifier(mpid);
} else {
throw new IOException("Unhandled PathArgument " + pathArgument);
}
}
private void writeObject(final @NonNull Object value) throws IOException {
- if (value instanceof String) {
- writeValue((String) value);
- } else if (value instanceof Boolean) {
- writeValue((Boolean) value);
- } else if (value instanceof Byte) {
- writeValue((Byte) value);
- } else if (value instanceof Short) {
- writeValue((Short) value);
- } else if (value instanceof Integer) {
- writeValue((Integer) value);
- } else if (value instanceof Long) {
- writeValue((Long) value);
- } else if (value instanceof Uint8) {
- writeValue((Uint8) value);
- } else if (value instanceof Uint16) {
- writeValue((Uint16) value);
- } else if (value instanceof Uint32) {
- writeValue((Uint32) value);
- } else if (value instanceof Uint64) {
- writeValue((Uint64) value);
- } else if (value instanceof QName) {
- writeQNameInternal((QName) value);
- } else if (value instanceof YangInstanceIdentifier) {
- writeValue((YangInstanceIdentifier) value);
- } else if (value instanceof byte[]) {
- writeValue((byte[]) value);
+ if (value instanceof String str) {
+ writeValue(str);
+ } else if (value instanceof Boolean bool) {
+ writeValue(bool);
+ } else if (value instanceof Byte byteVal) {
+ writeValue(byteVal);
+ } else if (value instanceof Short shortVal) {
+ writeValue(shortVal);
+ } else if (value instanceof Integer intVal) {
+ writeValue(intVal);
+ } else if (value instanceof Long longVal) {
+ writeValue(longVal);
+ } else if (value instanceof Uint8 uint8) {
+ writeValue(uint8);
+ } else if (value instanceof Uint16 uint16) {
+ writeValue(uint16);
+ } else if (value instanceof Uint32 uint32) {
+ writeValue(uint32);
+ } else if (value instanceof Uint64 uint64) {
+ writeValue(uint64);
+ } else if (value instanceof QName qname) {
+ writeQNameInternal(qname);
+ } else if (value instanceof YangInstanceIdentifier id) {
+ writeValue(id);
+ } else if (value instanceof byte[] bytes) {
+ writeValue(bytes);
} else if (value instanceof Empty) {
output.writeByte(MagnesiumValue.EMPTY);
- } else if (value instanceof Set) {
- writeValue((Set<?>) value);
+ } else if (value instanceof Set<?> set) {
+ writeValue(set);
} else if (value instanceof BigDecimal || value instanceof Decimal64) {
output.writeByte(MagnesiumValue.BIGDECIMAL);
output.writeUTF(value.toString());
- } else if (value instanceof BigInteger) {
- writeValue((BigInteger) value);
+ } else if (value instanceof BigInteger val) {
+ writeValue(val);
} else {
throw new IOException("Unhandled value type " + value.getClass());
}
// NodeIdentifier -- which is typically true
private boolean matchesParentQName(final QName qname) {
final Object current = stack.peek();
- return current instanceof NodeIdentifier && qname.equals(((NodeIdentifier) current).getNodeType());
+ return current instanceof NodeIdentifier nid && qname.equals(nid.getNodeType());
}
// Start an END_NODE-terminated node, which typically has a QName matching the parent. If that is the case we emit
@Override
public void flush() throws IOException {
- if (output instanceof OutputStream) {
- ((OutputStream)output).flush();
+ if (output instanceof OutputStream out) {
+ out.flush();
}
}
@Override
public QName readQName() throws IOException {
final byte valueType = readByte();
- switch (valueType) {
- case NeonSR2Tokens.IS_QNAME_CODE:
- return codedQName(readInt());
- case NeonSR2Tokens.IS_QNAME_VALUE:
- return rawQName();
- default:
- throw new IOException("Unhandled QName value type " + valueType);
- }
+ return switch (valueType) {
+ case NeonSR2Tokens.IS_QNAME_CODE -> codedQName(readInt());
+ case NeonSR2Tokens.IS_QNAME_VALUE -> rawQName();
+ default -> throw new IOException("Unhandled QName value type " + valueType);
+ };
}
@Override
AugmentationIdentifier readAugmentationIdentifier() throws IOException {
final byte valueType = readByte();
- switch (valueType) {
- case NeonSR2Tokens.IS_AUGMENT_CODE:
- return codedAugmentId(readInt());
- case NeonSR2Tokens.IS_AUGMENT_VALUE:
- return rawAugmentId();
- default:
- throw new IOException("Unhandled AugmentationIdentifier value type " + valueType);
- }
+ return switch (valueType) {
+ case NeonSR2Tokens.IS_AUGMENT_CODE -> codedAugmentId(readInt());
+ case NeonSR2Tokens.IS_AUGMENT_VALUE -> rawAugmentId();
+ default -> throw new IOException("Unhandled AugmentationIdentifier value type " + valueType);
+ };
}
@Override
// NodeIdentifier rides on top of QName, with this method really saying 'interpret next QName as NodeIdentifier'
// to do that we inter-mingle with readQName()
final byte valueType = readByte();
- switch (valueType) {
- case NeonSR2Tokens.IS_QNAME_CODE:
- return codedNodeIdentifier(readInt());
- case NeonSR2Tokens.IS_QNAME_VALUE:
- return rawNodeIdentifier();
- default:
- throw new IOException("Unhandled NodeIdentifier value type " + valueType);
- }
+ return switch (valueType) {
+ case NeonSR2Tokens.IS_QNAME_CODE -> codedNodeIdentifier(readInt());
+ case NeonSR2Tokens.IS_QNAME_VALUE -> rawNodeIdentifier();
+ default -> throw new IOException("Unhandled NodeIdentifier value type " + valueType);
+ };
}
private QNameModule readModule() throws IOException {
final byte valueType = readByte();
- switch (valueType) {
- case NeonSR2Tokens.IS_MODULE_CODE:
- return codedModule(readInt());
- case NeonSR2Tokens.IS_MODULE_VALUE:
- return rawModule();
- default:
- throw new IOException("Unhandled QNameModule value type " + valueType);
- }
+ return switch (valueType) {
+ case NeonSR2Tokens.IS_MODULE_CODE -> codedModule(readInt());
+ case NeonSR2Tokens.IS_MODULE_VALUE -> rawModule();
+ default -> throw new IOException("Unhandled QNameModule value type " + valueType);
+ };
}
private NodeIdentifier codedNodeIdentifier(final int code) throws IOException {
@Override
public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof StringQName)) {
- return false;
- }
- final StringQName other = (StringQName) obj;
- return localName.equals(other.localName) && namespace.equals(other.namespace)
- && Objects.equals(revision, other.revision);
+ return this == obj || obj instanceof StringQName other && localName.equals(other.localName)
+ && namespace.equals(other.namespace) && Objects.equals(revision, other.revision);
}
QName toQName() {
@Override
public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof ModuleQName)) {
- return false;
- }
- final ModuleQName other = (ModuleQName) obj;
- return localName.equals(other.localName) && module.equals(other.module);
+ return this == obj || obj instanceof ModuleQName other && localName.equals(other.localName)
+ && module.equals(other.module);
}
QName toQName() {
@Override
public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof StringModule)) {
- return false;
- }
- final StringModule other = (StringModule) obj;
- return namespace.equals(other.namespace) && Objects.equals(revision, other.revision);
+ return this == obj || obj instanceof StringModule other && namespace.equals(other.namespace)
+ && Objects.equals(revision, other.revision);
}
QNameModule toQNameModule() {
}
final short version = input.readShort();
- final NormalizedNodeDataInput ret;
- switch (version) {
- case TokenTypes.LITHIUM_VERSION:
- ret = new LithiumNormalizedNodeInputStreamReader(input);
- break;
- case TokenTypes.NEON_SR2_VERSION:
- ret = new NeonSR2NormalizedNodeInputStreamReader(input);
- break;
- case TokenTypes.SODIUM_SR1_VERSION:
- ret = new SodiumSR1DataInput(input);
- break;
- case TokenTypes.MAGNESIUM_VERSION:
- ret = new MagnesiumDataInput(input);
- break;
- default:
- throw defunct("Unhandled stream version %s", version);
- }
+ final NormalizedNodeDataInput ret = switch (version) {
+ case TokenTypes.LITHIUM_VERSION -> new LithiumNormalizedNodeInputStreamReader(input);
+ case TokenTypes.NEON_SR2_VERSION -> new NeonSR2NormalizedNodeInputStreamReader(input);
+ case TokenTypes.SODIUM_SR1_VERSION -> new SodiumSR1DataInput(input);
+ case TokenTypes.MAGNESIUM_VERSION -> new MagnesiumDataInput(input);
+ default -> throw defunct("Unhandled stream version %s", version);
+ };
setDelegate(ret);
return ret;