Reformulate LeafRefContext without SchemaPath
[yangtools.git] / parser / 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 com.google.common.annotations.Beta;
11 import java.util.Objects;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16
17 @Beta
18 // FIXME: remove this class once we ditch SchemaPath.getPath()
19 public abstract class SchemaPathSupport implements Immutable {
20     private static final class Enabled extends SchemaPathSupport {
21         @Override
22         SchemaPath effectivePath(final SchemaPath path) {
23             return path;
24         }
25
26         @Override
27         SchemaPath optionalPath(final SchemaPath path) {
28             return path;
29         }
30
31         @Override
32         boolean equalPaths(final SchemaPath first, final SchemaPath second) {
33             return Objects.equals(first, second);
34         }
35     }
36
37     private static final SchemaPathSupport DEFAULT = new Enabled();
38
39     private SchemaPathSupport() {
40         // Hidden on purpose
41     }
42
43     public static @NonNull Immutable toEffectivePath(final @NonNull SchemaPath path) {
44         return DEFAULT.effectivePath(path);
45     }
46
47     public static @Nullable SchemaPath toOptionalPath(final @Nullable SchemaPath path) {
48         return DEFAULT.optionalPath(path);
49     }
50
51     public static boolean effectivelyEqual(@Nullable final SchemaPath first, @Nullable final SchemaPath second) {
52         return DEFAULT.equalPaths(first, second);
53     }
54
55     abstract boolean equalPaths(@Nullable SchemaPath first, @Nullable SchemaPath second);
56
57     abstract @NonNull Immutable effectivePath(@NonNull SchemaPath path);
58
59     abstract @Nullable SchemaPath optionalPath(@Nullable SchemaPath path);
60 }