Make SchemaNode.getPath() a default method 10/94010/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 2 Dec 2020 22:26:30 +0000 (23:26 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 3 Dec 2020 03:27:12 +0000 (03:27 +0000)
Make sure implementations do not have to implement getPath() as we
can just have a default method throwing UnsupportedOperationException.

Change-Id: I3490d6d706113ea14ee3f4181a7ba827380c8181
JIRA: YANGTOOLS-1071
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaNode.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaNodeDefaults.java [new file with mode: 0644]

index a0773f031a6f289493b58f350f60568112647c10..441bc793a5b8e281a534d13e5cecc089e78d43d3 100644 (file)
@@ -24,6 +24,9 @@ public interface SchemaNode extends DocumentedNode.WithStatus {
     /**
      * Returns the schema path of the instance of the type {@code SchemaNode}.
      *
+     * <p>
+     * The default implementation throws an {@link UnsupportedOperationException}.
+     *
      * @return schema path of the schema node
      * @throws UnsupportedOperationException when the implementation does not support per-node unique paths
      * @deprecated The idea of identifying SchemaNodes through a global path does not work. There are two problems:
@@ -38,5 +41,7 @@ public interface SchemaNode extends DocumentedNode.WithStatus {
      *             </ul>
      */
     @Deprecated
-    @NonNull SchemaPath getPath();
+    default @NonNull SchemaPath getPath() {
+        return SchemaNodeDefaults.throwUnsupported(this);
+    }
 }
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaNodeDefaults.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaNodeDefaults.java
new file mode 100644 (file)
index 0000000..63b1a50
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2020 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.model.api;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
+@Beta
+@NonNullByDefault
+public final class SchemaNodeDefaults {
+    private SchemaNodeDefaults() {
+        // Hidden on purpose
+    }
+
+    /**
+     * Report unsupported {@link SchemaNode#getPath()} implementation. This method is guaranteed to throw a
+     * RuntimeException.
+     *
+     * @param impl {@code this} object of invoking implementation
+     * @throws NullPointerException if impl is null
+     * @throws UnsupportedOperationException if impl is not null
+     * @see SchemaNode#getPath()
+     */
+    // FIXME: 8.0.0: consider deprecating this method
+    public static SchemaPath throwUnsupported(final Object impl) {
+        throw new UnsupportedOperationException(impl.getClass() + " does not support SchemaNode.getPath()");
+    }
+
+    /**
+     * Report unsupported {@link SchemaNode#getPath()} implementation if provided path is null.
+     *
+     * @param impl {@code this} object of invoking implementation
+     * @throws NullPointerException if impl is null
+     * @throws UnsupportedOperationException if path is null
+     * @see SchemaNode#getPath()
+     */
+    // FIXME: 8.0.0: consider deprecating this method
+    public static SchemaPath throwUnsupportedIfNull(final Object impl, final @Nullable SchemaPath path) {
+        return path != null ? path : throwUnsupported(impl);
+    }
+}