Seal YangExpr hierarchy
[yangtools.git] / xpath / yang-xpath-api / src / main / java / org / opendaylight / yangtools / yang / xpath / api / YangPathExpr.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.xpath.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.Objects;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Relative;
17
18 @Beta
19 public sealed class YangPathExpr implements YangExpr {
20     private static final class WithLocation extends YangPathExpr {
21         private static final long serialVersionUID = 1L;
22
23         private final Relative locationPath;
24
25         WithLocation(final YangExpr filterExpr, final Relative locationPath) {
26             super(filterExpr);
27             this.locationPath = requireNonNull(locationPath);
28         }
29
30         @Override
31         public Optional<Relative> getLocationPath() {
32             return Optional.of(locationPath);
33         }
34     }
35
36     private static final long serialVersionUID = 1L;
37
38     private final YangExpr filterExpr;
39
40     private YangPathExpr(final YangExpr filterExpr) {
41         this.filterExpr = requireNonNull(filterExpr);
42     }
43
44     public static YangPathExpr of(final YangExpr filterExpr) {
45         return new YangPathExpr(filterExpr);
46     }
47
48     public static YangPathExpr of(final YangExpr expr, final Relative locationPath) {
49         return new WithLocation(expr, locationPath);
50     }
51
52     public final YangExpr getFilterExpr() {
53         return filterExpr;
54     }
55
56     public Optional<Relative> getLocationPath() {
57         return Optional.empty();
58     }
59
60     @Override
61     public final int hashCode() {
62         return Objects.hash(filterExpr, getLocationPath());
63     }
64
65     @Override
66     public final boolean equals(final @Nullable Object obj) {
67         if (this == obj) {
68             return true;
69         }
70         if (!(obj instanceof YangPathExpr)) {
71             return false;
72         }
73         final YangPathExpr other = (YangPathExpr) obj;
74         return filterExpr.equals(((YangPathExpr) obj).filterExpr) && getLocationPath().equals(other.getLocationPath());
75     }
76
77     @Override
78     public final String toString() {
79         // FIXME: this is not right
80         return "-(" + filterExpr + ")";
81     }
82 }