c88c8352161393b088a88d5d58c4ef349930c027
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaNodeDefaults.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.api;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.common.QName;
17
18 @Beta
19 @NonNullByDefault
20 public final class SchemaNodeDefaults {
21     private SchemaNodeDefaults() {
22         // Hidden on purpose
23     }
24
25     /**
26      * Report unsupported {@link SchemaNode#getPath()} implementation. This method is guaranteed to throw an
27      * {@link UnsupportedOperationException}.
28      *
29      * @param impl {@code this} object of invoking implementation
30      * @return Nothing
31      * @throws NullPointerException if {@code impl} is null
32      * @throws UnsupportedOperationException always
33      * @see SchemaNode#getPath()
34      */
35     // FIXME: 8.0.0: consider deprecating this method
36     public static SchemaPath throwUnsupported(final Object impl) {
37         throw new UnsupportedOperationException(impl.getClass() + " does not support SchemaNode.getPath()");
38     }
39
40     /**
41      * Report unsupported {@link SchemaNode#getPath()} implementation if provided path is null.
42      *
43      * @param impl {@code this} object of invoking implementation
44      * @param path A schema path
45      * @return {@code path} if non-null
46      * @throws NullPointerException if {@code impl} is null
47      * @throws UnsupportedOperationException if @{code path} is null
48      * @see SchemaNode#getPath()
49      */
50     // FIXME: 8.0.0: consider deprecating this method
51     public static SchemaPath throwUnsupportedIfNull(final Object impl, final @Nullable SchemaPath path) {
52         return path != null ? path : throwUnsupported(impl);
53     }
54
55     /**
56      * Extract {@link QName} from a path object.
57      *
58      * @param path Path handle
59      * @return Extracted QName
60      * @throws NullPointerException if {@code path} is null
61      * @throws IllegalArgumentException if {@code path} is not supported
62      */
63     // FIXME: 8.0.0: consider deprecating this method
64     public static QName extractQName(final Immutable path) {
65         if (path instanceof QName) {
66             return (QName) path;
67         } else if (path instanceof SchemaPath) {
68             return verifyNotNull(((SchemaPath) path).getLastComponent());
69         } else {
70             throw new IllegalArgumentException("Unhandled object " + path);
71         }
72     }
73
74     /**
75      * Extract {@link SchemaPath} from a path object.
76      *
77      * @param impl Implementation object
78      * @param path Path handle
79      * @return Extracted SchemaPath
80      * @throws UnsupportedOperationException if {@code path} does not hold a SchemaPath
81      */
82     // FIXME: 8.0.0: consider deprecating this method
83     public static SchemaPath extractPath(final Object impl, final Immutable path) {
84         return path instanceof SchemaPath ? (SchemaPath) path : SchemaNodeDefaults.throwUnsupported(impl);
85     }
86 }