3690909ef0ddd7780252d060b937e7d293fce4db
[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 java.util.Objects;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNodeDefaults;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20
21 @Beta
22 // FIXME: remove this class once we ditch SchemaPath.getPath()
23 public abstract class SchemaPathSupport implements Immutable {
24     private static final class Enabled extends SchemaPathSupport {
25         @Override
26         SchemaPath effectivePath(final SchemaPath path) {
27             return path;
28         }
29
30         @Override
31         SchemaPath optionalPath(final SchemaPath path) {
32             return path;
33         }
34
35         @Override
36         boolean equalPaths(final SchemaPath first, final SchemaPath second) {
37             return Objects.equals(first, second);
38         }
39     }
40
41     private static final SchemaPathSupport DEFAULT = new Enabled();
42
43     private SchemaPathSupport() {
44         // Hidden on purpose
45     }
46
47     public static @NonNull Immutable toEffectivePath(final @NonNull SchemaPath path) {
48         return DEFAULT.effectivePath(path);
49     }
50
51     public static @Nullable SchemaPath toOptionalPath(final @Nullable SchemaPath path) {
52         return DEFAULT.optionalPath(path);
53     }
54
55     public static boolean effectivelyEqual(@Nullable final SchemaPath first, @Nullable final SchemaPath second) {
56         return DEFAULT.equalPaths(first, second);
57     }
58
59     public static @NonNull QName extractQName(final @NonNull Immutable path) {
60         if (path instanceof SchemaPath) {
61             return verifyNotNull(((SchemaPath) path).getLastComponent());
62         } else if (path instanceof QName) {
63             return (QName) path;
64         } else {
65             throw new IllegalArgumentException("Unhandled object " + path);
66         }
67     }
68
69     public static @NonNull SchemaPath extractPath(final @NonNull Object impl, final @NonNull Immutable path) {
70         return path instanceof SchemaPath ? (SchemaPath) path : SchemaNodeDefaults.throwUnsupported(impl);
71     }
72
73     abstract boolean equalPaths(@Nullable SchemaPath first, @Nullable SchemaPath second);
74
75     abstract @NonNull Immutable effectivePath(@NonNull SchemaPath path);
76
77     abstract @Nullable SchemaPath optionalPath(@Nullable SchemaPath path);
78 }