Populate xpath/ hierarchy
[yangtools.git] / xpath / yang-xpath-api / src / main / java / org / opendaylight / yangtools / yang / xpath / api / YangBooleanConstantExpr.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.Optional;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.QName;
16
17 /**
18  * Expressions which evaluate to a logical {@code true} or {@code false}. These expressions are equivalent to the result
19  * returned by {@code true()} and {@code false()} functions defined in XPath 1.0.
20  *
21  * <p>
22  * They also map these functions' names to the constant pool under their {@link YangFunctionCallExpr#getName()}
23  * identity. All users should use these constants in favor of their equivalent function calls.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public enum YangBooleanConstantExpr implements YangConstantExpr<Boolean> {
29     /**
30      * A constant {@code false} expression.
31      */
32     FALSE(Boolean.FALSE, YangFunction.FALSE, "false"),
33     /**
34      * A constant {@code true} expression.
35      */
36     TRUE(Boolean.TRUE, YangFunction.TRUE, "true");
37
38     private final YangFunctionCallExpr function;
39     private final YangLiteralExpr literal;
40     private final Boolean value;
41
42     @SuppressWarnings("null")
43     YangBooleanConstantExpr(final @Nullable Boolean value, final YangFunction function, final String literal) {
44         this.value = requireNonNull(value);
45         this.function = YangFunctionCallExpr.of(function.getIdentifier());
46         this.literal = YangLiteralExpr.of(literal);
47     }
48
49     @Override
50     public QName getIdentifier() {
51         return function.getName();
52     }
53
54     @Override
55     public Boolean getValue() {
56         return value;
57     }
58
59     /**
60      * Convert this constant into the equivalent function. This function is provided for bridging purposes only.
61      *
62      * @return Equivalent function invocation.
63      */
64     public YangFunctionCallExpr asFunction() {
65         return function;
66     }
67
68     /**
69      * Convert this constant into a string literal, i.e. the result of calling {@code string(boolean)} function on this
70      * constant.
71      *
72      * @return Literal expression.
73      */
74     public YangLiteralExpr asStringLiteral() {
75         return literal;
76     }
77
78     public static YangBooleanConstantExpr of(final boolean bool) {
79         return bool ? TRUE : FALSE;
80     }
81
82     public static Optional<YangFunctionCallExpr> forFunctionName(final String functionName) {
83         switch (functionName) {
84             case "false":
85                 return Optional.of(FALSE.function);
86             case "true":
87                 return Optional.of(TRUE.function);
88             default:
89                 return Optional.empty();
90         }
91     }
92 }