2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
10 import static com.google.common.base.Verify.verifyNotNull;
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;
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 {
26 SchemaPath effectivePath(final SchemaPath path) {
31 SchemaPath optionalPath(final SchemaPath path) {
36 boolean equalPaths(final SchemaPath first, final SchemaPath second) {
37 return Objects.equals(first, second);
41 private static final SchemaPathSupport DEFAULT = new Enabled();
43 private SchemaPathSupport() {
47 public static @NonNull Immutable toEffectivePath(final @NonNull SchemaPath path) {
48 return DEFAULT.effectivePath(path);
51 public static @Nullable SchemaPath toOptionalPath(final @Nullable SchemaPath path) {
52 return DEFAULT.optionalPath(path);
55 public static boolean effectivelyEqual(@Nullable final SchemaPath first, @Nullable final SchemaPath second) {
56 return DEFAULT.equalPaths(first, second);
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) {
65 throw new IllegalArgumentException("Unhandled object " + path);
69 public static @NonNull SchemaPath extractPath(final @NonNull Object impl, final @NonNull Immutable path) {
70 return path instanceof SchemaPath ? (SchemaPath) path : SchemaNodeDefaults.throwUnsupported(impl);
73 abstract boolean equalPaths(@Nullable SchemaPath first, @Nullable SchemaPath second);
75 abstract @NonNull Immutable effectivePath(@NonNull SchemaPath path);
77 abstract @Nullable SchemaPath optionalPath(@Nullable SchemaPath path);