Move AbstractPathExpression to yang-model-spi
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / PathExpressionImpl.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o.  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.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.model.spi.AbstractPathExpression;
17
18 /**
19  * A simple XPathExpression implementation.
20  *
21  * @deprecated Users are advised to supply their own implementation of PathExpression.
22  */
23 @Deprecated(forRemoval = true)
24 @NonNullByDefault
25 public final class PathExpressionImpl extends AbstractPathExpression {
26     private final @Nullable Steps steps;
27     private final boolean absolute;
28
29     @SuppressFBWarnings(value = "NP_STORE_INTO_NONNULL_FIELD", justification = "Non-grok on SpotBugs part")
30     public PathExpressionImpl(final String xpath, final boolean absolute) {
31         super(xpath);
32         this.absolute = absolute;
33         this.steps = null;
34     }
35
36     public PathExpressionImpl(final String xpath, final Steps steps) {
37         super(xpath);
38         this.steps = requireNonNull(steps);
39         this.absolute = steps instanceof LocationPathSteps
40                 && ((LocationPathSteps) steps).getLocationPath().isAbsolute();
41     }
42
43     @Override
44     public boolean isAbsolute() {
45         return absolute;
46     }
47
48     @Override
49     public Steps getSteps() {
50         final Steps loc = steps;
51         if (loc == null) {
52             throw new UnsupportedOperationException("Steps have not been provided");
53         }
54         return loc;
55     }
56
57     @Override
58     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
59         return super.addToStringAttributes(helper.add("absolute", absolute).add("steps", steps));
60     }
61 }