Expose Relative/Absolute YangLocationPath 32/81032/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 20 Mar 2019 12:20:38 +0000 (13:20 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 20 Mar 2019 14:29:00 +0000 (15:29 +0100)
This patch exposes the two concretizations of YangLocationPath,
so they can be used for constructing type-safe APIs.

Change-Id: I9f0678141d7f6df668020531250a6a10fab67512
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-xpath-api/src/main/java/org/opendaylight/yangtools/yang/xpath/api/YangLocationPath.java
yang/yang-xpath-api/src/main/java/org/opendaylight/yangtools/yang/xpath/api/YangPathExpr.java
yang/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/AntlrXPathParser.java
yang/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/InstanceIdentifierParser.java

index b3e687e966155c920256253a93490b22feb07a19..9563b18d7af9ecaa5de6407aa334915d3c321771 100644 (file)
@@ -25,7 +25,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 
 @Beta
-public class YangLocationPath implements YangExpr {
+public abstract class YangLocationPath implements YangExpr {
     public abstract static class Step implements Serializable, YangPredicateAware {
         private static final long serialVersionUID = 1L;
 
@@ -317,9 +317,22 @@ public class YangLocationPath implements YangExpr {
         }
     }
 
+    public static final class Relative extends YangLocationPath {
+        private static final long serialVersionUID = 1L;
+
+        Relative(final ImmutableList<Step> steps) {
+            super(steps);
+        }
+
+        @Override
+        public boolean isAbsolute() {
+            return false;
+        }
+    }
+
     private static final long serialVersionUID = 1L;
-    private static final YangLocationPath ROOT = new Absolute(ImmutableList.of());
-    private static final YangLocationPath SELF = new YangLocationPath(ImmutableList.of());
+    private static final Absolute ROOT = new Absolute(ImmutableList.of());
+    private static final Relative SELF = new Relative(ImmutableList.of());
 
     private final ImmutableList<Step> steps;
 
@@ -327,21 +340,20 @@ public class YangLocationPath implements YangExpr {
         this.steps = requireNonNull(steps);
     }
 
-    public static final YangLocationPath of(final boolean absolute) {
-        return absolute ? ROOT : SELF;
+    public static final Absolute absolute(final Step... steps) {
+        return absolute(Arrays.asList(steps));
     }
 
-    public static final YangLocationPath of(final boolean absolute, final Step... steps) {
-        return of(absolute, Arrays.asList(steps));
+    public static final Absolute absolute(final Collection<Step> steps) {
+        return steps.isEmpty() ? ROOT : new Absolute(ImmutableList.copyOf(steps));
     }
 
-    public static final YangLocationPath of(final boolean absolute, final Collection<Step> steps) {
-        if (steps.isEmpty()) {
-            return of(absolute);
-        }
+    public static final Relative relative(final Step... steps) {
+        return relative(Arrays.asList(steps));
+    }
 
-        final ImmutableList<Step> copy = ImmutableList.copyOf(steps);
-        return absolute ? new Absolute(copy) : new YangLocationPath(copy);
+    public static final Relative relative(final Collection<Step> steps) {
+        return steps.isEmpty() ? SELF : new Relative(ImmutableList.copyOf(steps));
     }
 
     /**
@@ -349,7 +361,7 @@ public class YangLocationPath implements YangExpr {
      *
      * @return Empty absolute {@link YangLocationPath}
      */
-    public static final YangLocationPath root() {
+    public static final Absolute root() {
         return ROOT;
     }
 
@@ -359,18 +371,16 @@ public class YangLocationPath implements YangExpr {
      *
      * @return Empty relative {@link YangLocationPath}
      */
-    public static YangLocationPath self() {
+    public static final Relative self() {
         return SELF;
     }
 
-    public boolean isAbsolute() {
-        return false;
-    }
-
     public final ImmutableList<Step> getSteps() {
         return steps;
     }
 
+    public abstract boolean isAbsolute();
+
     @Override
     public final int hashCode() {
         return Boolean.hashCode(isAbsolute()) * 31 + steps.hashCode();
@@ -398,6 +408,6 @@ public class YangLocationPath implements YangExpr {
     }
 
     final Object readSolve() {
-        return steps.isEmpty() ? of(isAbsolute()) : this;
+        return steps.isEmpty() ? isAbsolute() ? ROOT : SELF : this;
     }
 }
index 47577c8b6938ae8e76ef3f6a9723d942d4a7c8c7..dd7886e5cce7b29346b0e540e07b5a85472b16a7 100644 (file)
@@ -13,21 +13,22 @@ import com.google.common.annotations.Beta;
 import java.util.Objects;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Relative;
 
 @Beta
 public class YangPathExpr implements YangExpr {
     private static final class WithLocation extends YangPathExpr {
         private static final long serialVersionUID = 1L;
 
-        private final YangLocationPath locationPath;
+        private final Relative locationPath;
 
-        WithLocation(final YangExpr filterExpr, final YangLocationPath locationPath) {
+        WithLocation(final YangExpr filterExpr, final Relative locationPath) {
             super(filterExpr);
             this.locationPath = requireNonNull(locationPath);
         }
 
         @Override
-        public Optional<YangLocationPath> getLocationPath() {
+        public Optional<Relative> getLocationPath() {
             return Optional.of(locationPath);
         }
     }
@@ -44,7 +45,7 @@ public class YangPathExpr implements YangExpr {
         return new YangPathExpr(filterExpr);
     }
 
-    public static YangPathExpr of(final YangExpr expr, final YangLocationPath locationPath) {
+    public static YangPathExpr of(final YangExpr expr, final Relative locationPath) {
         return new WithLocation(expr, locationPath);
     }
 
@@ -52,7 +53,7 @@ public class YangPathExpr implements YangExpr {
         return filterExpr;
     }
 
-    public Optional<YangLocationPath> getLocationPath() {
+    public Optional<Relative> getLocationPath() {
         return Optional.empty();
     }
 
index 2c7f506cc1aff934c256a727104cb917e0e45cc8..82134f64dc64dd3aed5a7144277ecfeccb729f9b 100644 (file)
@@ -225,7 +225,7 @@ final class AntlrXPathParser implements YangXPathParser {
         verifyChildCount(expr, 1);
         final ParseTree first = expr.getChild(0);
         if (first instanceof RelativeLocationPathContext) {
-            return parseRelativeLocationPath((RelativeLocationPathContext) first);
+            return YangLocationPath.relative(parseLocationPathSteps((RelativeLocationPathContext) first));
         }
 
         final AbsoluteLocationPathNorootContext abs = verifyTree(AbsoluteLocationPathNorootContext.class, first);
@@ -234,7 +234,7 @@ final class AntlrXPathParser implements YangXPathParser {
         final Deque<Step> steps = parseLocationPathSteps(getChild(abs, RelativeLocationPathContext.class, 1));
         parseStepShorthand(abs.getChild(0)).ifPresent(steps::addFirst);
 
-        return YangLocationPath.of(true, steps);
+        return YangLocationPath.absolute(steps);
     }
 
     private YangExpr parseMultiplicative(final MultiplicativeExprContext expr) {
@@ -270,7 +270,7 @@ final class AntlrXPathParser implements YangXPathParser {
         verifyChildCount(expr, 3);
         final Deque<Step> steps = parseLocationPathSteps(getChild(expr, RelativeLocationPathContext.class, 2));
         parseStepShorthand(expr.getChild(1)).ifPresent(steps::addFirst);
-        return YangPathExpr.of(filter, YangLocationPath.of(false, steps));
+        return YangPathExpr.of(filter, YangLocationPath.relative(steps));
     }
 
     private YangExpr parsePredicate(final PredicateContext expr) {
@@ -321,10 +321,6 @@ final class AntlrXPathParser implements YangXPathParser {
         return steps;
     }
 
-    private YangLocationPath parseRelativeLocationPath(final RelativeLocationPathContext expr) {
-        return YangLocationPath.of(false, parseLocationPathSteps(expr));
-    }
-
     private YangExpr parseTerminal(final TerminalNode term) {
         final String text = term.getText();
         switch (term.getSymbol().getType()) {
index e03b2471b2262ae4724f91ecc4539f894f04d1f2..ce2944ee8d44c87229f1fd360d784d3df5bda8b8 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.yangtools.yang.xpath.api.YangBinaryOperator;
 import org.opendaylight.yangtools.yang.xpath.api.YangExpr;
 import org.opendaylight.yangtools.yang.xpath.api.YangLiteralExpr;
 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
+import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Absolute;
 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Step;
 import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr;
 import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
@@ -52,7 +53,7 @@ final class InstanceIdentifierParser {
         this.mathSupport = mathMode.getSupport();
     }
 
-    YangLocationPath interpretAsInstanceIdentifier(final YangLiteralExpr expr) throws XPathExpressionException {
+    Absolute interpretAsInstanceIdentifier(final YangLiteralExpr expr) throws XPathExpressionException {
         final xpathLexer lexer = new xpathLexer(CharStreams.fromString(expr.getLiteral()));
         final instanceIdentifierParser parser = new instanceIdentifierParser(new CommonTokenStream(lexer));
         final CapturingErrorListener listener = new CapturingErrorListener();
@@ -70,7 +71,7 @@ final class InstanceIdentifierParser {
             steps.add(parsePathArgument(getChild(id, PathArgumentContext.class, i)));
         }
 
-        return YangLocationPath.of(true, steps);
+        return YangLocationPath.absolute(steps);
     }
 
     private Step parsePathArgument(final PathArgumentContext expr) {