Split out NormalizedContainer 31/106431/8
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Jun 2023 14:49:49 +0000 (16:49 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 13 Jun 2023 09:16:51 +0000 (09:16 +0000)
In order to support a consistent surface between ContainerNode and
NormalizedYangData (which really serve a similar purpose), we need to
make sure we have a common concept capturing NormalizedNodeContainer
without implying addressability by PathArgument.

This introduces DataContainer as a superclass of DataContainerNode,
along with NormalizedContainer, DistinctContainer and OrderedContainer
to support the attributes.

JIRA: YANGTOOLS-1472
Change-Id: Ida94334c0e8b7a776befffce62096a659ff3110b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DataContainer.java [new file with mode: 0644]
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DataContainerNode.java
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DistinctContainer.java [new file with mode: 0644]
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DistinctNodeContainer.java
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/NormalizedContainer.java [new file with mode: 0644]
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/NormalizedNodeContainer.java
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/NormalizedYangData.java
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/OrderedContainer.java [new file with mode: 0644]
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/OrderedNodeContainer.java
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/OrderingAware.java

diff --git a/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DataContainer.java b/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DataContainer.java
new file mode 100644 (file)
index 0000000..ed6c33d
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api.schema;
+
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+
+/**
+ * A {@link DistinctContainer} containing {@link DataContainerChild} children.
+ *
+ * <p>
+ * <b>NOTE:</b>
+ * All implementations of this interface are assumed to be {@link OrderingAware.System}, i.e. order-independent.
+ */
+public sealed interface DataContainer
+        extends DistinctContainer<NodeIdentifier, DataContainerChild>, OrderingAware.System
+        permits DataContainerNode, NormalizedYangData {
+    @Override
+    int hashCode();
+
+    @Override
+    boolean equals(Object obj);
+}
index df50de51db135a49e85eb4654fdbc4999cc19a29..722358c685f4cf8195ba972ccb3d545ce01ef1a1 100644 (file)
@@ -13,10 +13,6 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent
  * Abstract node which does not have value but contains valid {@link DataContainerChild} nodes. Schema of this node is
  * described by instance of {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer}.
  *
- * <p>
- * <b>NOTE:</b>
- * All implementations of this interface are assumed to be {@link OrderingAware.System}, i.e. order-independent.
- *
  * <h2>Implementation notes</h2>
  * This interface should not be implemented directly, but rather implementing one of it's subclasses
  * <ul>
@@ -26,11 +22,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent
  *   <li>{@link UnkeyedListEntryNode}</li>
  * </ul>
  */
-public interface DataContainerNode
-        extends DistinctNodeContainer<NodeIdentifier, DataContainerChild>, OrderingAware.System {
-    @Override
-    int hashCode();
-
-    @Override
-    boolean equals(Object obj);
+public non-sealed interface DataContainerNode
+        extends DataContainer, DistinctNodeContainer<NodeIdentifier, DataContainerChild> {
+    // Just a composition of DataContainer and DistingNodeContainer
 }
diff --git a/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DistinctContainer.java b/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/DistinctContainer.java
new file mode 100644 (file)
index 0000000..d744319
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api.schema;
+
+import static com.google.common.base.Verify.verifyNotNull;
+
+import com.google.common.base.VerifyException;
+import java.util.Collection;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
+/**
+ * A {@link NormalizedContainer} which contains distinctly-addressable children.
+ *
+ * @param <K> Child path argument type
+ * @param <V> Child Node type
+ */
+public sealed interface DistinctContainer<K extends PathArgument, V extends NormalizedNode>
+        extends NormalizedContainer<V> permits DistinctNodeContainer, DataContainer {
+    /**
+     * {@inheritDoc}
+     *
+     * <p>
+     * All nodes returned in this iterable, MUST also be accessible via {@link #childByArg(PathArgument)} using their
+     * associated identifier.
+     *
+     * @return Iteration of all child nodes
+     */
+    @Override
+    Collection<@NonNull V> body();
+
+    /**
+     * Returns a child node identified by provided key.
+     *
+     * @param key Path argument identifying child node
+     * @return Matching child node, or null if no matching child exists
+     * @throws NullPointerException if {@code key} is {@code null}
+     */
+    @Nullable V childByArg(K key);
+
+    /**
+     * Attempts to find a child node identified by provided key.
+     *
+     * @param key Path argument identifying child node
+     * @return Optional with child node if child exists. {@link Optional#empty()} if child does not exist
+     * @throws NullPointerException if {@code key} is {@code null}
+     */
+    default Optional<V> findChildByArg(final K key) {
+        return Optional.ofNullable(childByArg(key));
+    }
+
+    /**
+     * Returns a child node identified by provided key, asserting its presence.
+     *
+     * @param key Path argument identifying child node
+     * @return Matching child node
+     * @throws NullPointerException if {@code key} is {@code null}
+     * @throws VerifyException if the child does not exist
+     */
+    default @NonNull V getChildByArg(final K key) {
+        return verifyNotNull(childByArg(key), "No child matching %s", key);
+    }
+}
index ae82f698a37e4aa31855ccce3a60075308e15c01..cafeb01d83bb4067ab744139db1ce7c92e5553d8 100644 (file)
@@ -7,13 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.data.api.schema;
 
-import static com.google.common.base.Verify.verifyNotNull;
-
-import com.google.common.base.VerifyException;
-import java.util.Collection;
-import java.util.Optional;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 
 /**
@@ -31,48 +24,6 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
  * @param <V> Child Node type
  */
 public non-sealed interface DistinctNodeContainer<K extends PathArgument, V extends NormalizedNode>
-        extends NormalizedNodeContainer<V> {
-    /**
-     * {@inheritDoc}
-     *
-     * <p>
-     * All nodes returned in this iterable, MUST also be accessible via {@link #childByArg(PathArgument)} using their
-     * associated identifier.
-     *
-     * @return Iteration of all child nodes
-     */
-    @Override
-    Collection<@NonNull V> body();
-
-    /**
-     * Returns a child node identified by provided key.
-     *
-     * @param key Path argument identifying child node
-     * @return Matching child node, or null if no matching child exists
-     * @throws NullPointerException if {@code key} is {@code null}
-     */
-    @Nullable V childByArg(K key);
-
-    /**
-     * Attempts to find a child node identified by provided key.
-     *
-     * @param key Path argument identifying child node
-     * @return Optional with child node if child exists. {@link Optional#empty()} if child does not exist
-     * @throws NullPointerException if {@code key} is {@code null}
-     */
-    default Optional<V> findChildByArg(final K key) {
-        return Optional.ofNullable(childByArg(key));
-    }
-
-    /**
-     * Returns a child node identified by provided key, asserting its presence.
-     *
-     * @param key Path argument identifying child node
-     * @return Matching child node
-     * @throws NullPointerException if {@code key} is {@code null}
-     * @throws VerifyException if the child does not exist
-     */
-    default @NonNull V getChildByArg(final K key) {
-        return verifyNotNull(childByArg(key), "No child matching %s", key);
-    }
+        extends DistinctContainer<K, V>, NormalizedNodeContainer<V> {
+    // Composition of DistinctContainer and NormalizedNodeContainer
 }
diff --git a/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/NormalizedContainer.java b/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/NormalizedContainer.java
new file mode 100644 (file)
index 0000000..ee0fecb
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api.schema;
+
+import java.util.Collection;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * A piece of {@link NormalizedData} which holds some child {@link NormalizedNode}s. This interface should not be used
+ * directly, but rather through its specializations.
+ *
+ * @param <V> Child Node type
+ */
+public sealed interface NormalizedContainer<V extends NormalizedNode> extends NormalizedData, OrderingAware
+        permits NormalizedNodeContainer, DistinctContainer, OrderedContainer {
+    /**
+     * {@inheritDoc}
+     *
+     * <p>
+     * Returns iteration of all child nodes. Order of returned child nodes may be defined by subinterfaces.
+     */
+    @Override
+    Collection<@NonNull V> body();
+
+    /**
+     * Return the logical size of this container body. The default implementation defers to {@code body().size()}.
+     *
+     * @return Size of this container's body.
+     */
+    default int size() {
+        return body().size();
+    }
+
+    /**
+     * Determine whether this container body is empty. The default implementation defers to {@code body().isEmpty()}.
+     *
+     * @return True if this container has an empty body.
+     */
+    default boolean isEmpty() {
+        return body().isEmpty();
+    }
+}
index 3a8d5c5dc2cab6cd58c0b0c43734b13dfa837946..0cc8047090d7895dbfdc2adc06e023a5461f4372 100644 (file)
@@ -7,9 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.data.api.schema;
 
-import java.util.Collection;
-import org.eclipse.jdt.annotation.NonNull;
-
 /**
  * Node which is not leaf, but has child {@link NormalizedNode}s as its value. It provides iteration over its child
  * nodes via {@link #body()}. More convenient access to child nodes are provided by {@link DistinctNodeContainer} and
@@ -17,32 +14,7 @@ import org.eclipse.jdt.annotation.NonNull;
  *
  * @param <V> Child Node type
  */
-public sealed interface NormalizedNodeContainer<V extends NormalizedNode> extends NormalizedNode, OrderingAware
+public sealed interface NormalizedNodeContainer<V extends NormalizedNode> extends NormalizedContainer<V>, NormalizedNode
         permits DistinctNodeContainer, OrderedNodeContainer {
-    /**
-     * {@inheritDoc}
-     *
-     * <p>
-     * Returns iteration of all child nodes. Order of returned child nodes may be defined by subinterfaces.
-     */
-    @Override
-    Collection<@NonNull V> body();
-
-    /**
-     * Return the logical size of this container body. The default implementation defers to {@code body().size()}.
-     *
-     * @return Size of this container's body.
-     */
-    default int size() {
-        return body().size();
-    }
-
-    /**
-     * Determine whether this container body is empty. The default implementation defers to {@code body().isEmpty()}.
-     *
-     * @return True if this container has an empty body.
-     */
-    default boolean isEmpty() {
-        return body().isEmpty();
-    }
+    // Just a composite of NormalizedContainer and NormalizedNode
 }
index 85b96d5963227c2e0168e9ac4cb34d0f5527f5a7..7b2d9c5ec35be023eb4385d3f7f6bddabb0d6fc8 100644 (file)
@@ -13,7 +13,7 @@ import org.opendaylight.yangtools.yang.common.YangDataName;
  * The contents of a {@code yang-data} template instance, as defined in
  * <a href="https://www.rfc-editor.org/rfc/rfc8040#page-80">RFC8040</a>'s {@code ietf-restconf} module.
  */
-public interface NormalizedYangData extends NormalizedData {
+public non-sealed interface NormalizedYangData extends DataContainer {
     @Override
     default Class<NormalizedYangData> contract() {
         return NormalizedYangData.class;
@@ -21,7 +21,4 @@ public interface NormalizedYangData extends NormalizedData {
 
     @Override
     YangDataName name();
-
-    @Override
-    DataContainerChild body();
 }
diff --git a/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/OrderedContainer.java b/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/OrderedContainer.java
new file mode 100644 (file)
index 0000000..cbfd5b4
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api.schema;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * A {@link NormalizedContainer} which preserves user supplied ordering and allows addressing of child elements by
+ * position. All implementations of this interface must also implement {@link OrderingAware.User}.
+ *
+ * @param <V> child type
+ */
+public sealed interface OrderedContainer<V extends NormalizedNode> extends NormalizedContainer<V>, OrderingAware.User
+        permits OrderedNodeContainer {
+    /**
+     * Returns child node by position.
+     *
+     * @param position Position of child node
+     * @return Child Node
+     * @throws IndexOutOfBoundsException Out of bound Exception
+     */
+    @NonNull V childAt(int position);
+
+    @Override
+    int hashCode();
+
+    @Override
+    boolean equals(Object obj);
+}
index 71e137831ec9746e522472e5ee3a69ee1c0676f1..171aa70e932096f74dc141916e782b428a556197 100644 (file)
@@ -7,12 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.data.api.schema;
 
-import org.eclipse.jdt.annotation.NonNull;
-
 /**
  * A {@link NormalizedNodeContainer} which preserves user supplied ordering and allows addressing of child elements by
- * position. All implementations of this interface must also implement {@link OrderingAware.User}. This interface should
- * not be implemented directly, but rather implementing one of it's subclasses
+ * position. This interface should not be implemented directly, but rather implementing one of it's subclasses
  * <ul>
  *   <li>{@link UnkeyedListNode}</li>
  *   <li>{@link UserLeafSetNode}</li>
@@ -24,20 +21,7 @@ import org.eclipse.jdt.annotation.NonNull;
 // FIXME: 9.0.0: we really want to do a List<@NonNull V> body(), but need to reconcile that with key-based lookup in
 //               implementations -- and those are using only a Map internally.
 public sealed interface OrderedNodeContainer<V extends NormalizedNode>
-        extends NormalizedNodeContainer<V>, OrderingAware.User
+        extends OrderedContainer<V>, NormalizedNodeContainer<V>
         permits UnkeyedListNode, UserLeafSetNode, UserMapNode {
-    /**
-     * Returns child node by position.
-     *
-     * @param position Position of child node
-     * @return Child Node
-     * @throws IndexOutOfBoundsException Out of bound Exception
-     */
-    @NonNull V childAt(int position);
-
-    @Override
-    int hashCode();
-
-    @Override
-    boolean equals(Object obj);
+    // Composition of OrderedContainer and NormalizedNodeContainer
 }
index d302673463bbfb5fa7b212a3bb5974605dd2d9eb..ce0ff683d48abd1e249b398592a3259656a4940f 100644 (file)
@@ -18,7 +18,7 @@ import org.opendaylight.yangtools.yang.common.Ordering;
  */
 @Beta
 @NonNullByDefault
-public sealed interface OrderingAware permits NormalizedNodeContainer, OrderingAware.System, OrderingAware.User {
+public sealed interface OrderingAware permits NormalizedContainer, OrderingAware.System, OrderingAware.User {
     /**
      * Marker interface for NormalizedNodeContainer implementations which correspond to {@code ordered-by system}. These
      * follow the {@link Unordered} contract.