Update SchemaPath.getPath() implementation
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / SchemaPathSupport.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.parser.spi.meta;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.SchemaNodeDefaults;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19
20 @Beta
21 // FIXME: remove this class once we ditch SchemaPath.getPath()
22 public abstract class SchemaPathSupport implements Immutable {
23     private static final class Enabled extends SchemaPathSupport {
24         @Override
25         SchemaPath effectivePath(final SchemaPath path) {
26             return path;
27         }
28
29         @Override
30         SchemaPath optionalPath(final SchemaPath path) {
31             return path;
32         }
33     }
34
35     private static final SchemaPathSupport DEFAULT = new Enabled();
36
37     private SchemaPathSupport() {
38         // Hidden on purpose
39     }
40
41     public static @NonNull Object toEffectivePath(final @NonNull SchemaPath path) {
42         return DEFAULT.effectivePath(path);
43     }
44
45     public static @Nullable SchemaPath toOptionalPath(final @Nullable SchemaPath path) {
46         return DEFAULT.optionalPath(path);
47     }
48
49     public static @NonNull QName extractQName(final @NonNull Object path) {
50         return path instanceof QName ? (QName) path : verifyNotNull(((SchemaPath) path).getLastComponent());
51     }
52
53     public static @NonNull SchemaPath extractPath(final @NonNull Object impl, final @NonNull Object path) {
54         return path instanceof SchemaPath ? (SchemaPath) path : SchemaNodeDefaults.throwUnsupported(impl);
55     }
56
57     abstract @NonNull Object effectivePath(@NonNull SchemaPath path);
58
59     abstract @Nullable SchemaPath optionalPath(@Nullable SchemaPath path);
60 }