2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.xpath.api;
10 import static java.util.Objects.requireNonNull;
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;
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.
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.
25 * @author Robert Varga
28 public enum YangBooleanConstantExpr implements YangConstantExpr<Boolean> {
30 * A constant {@code false} expression.
32 FALSE(Boolean.FALSE, YangFunction.FALSE, "false"),
34 * A constant {@code true} expression.
36 TRUE(Boolean.TRUE, YangFunction.TRUE, "true");
38 private final YangFunctionCallExpr function;
39 private final YangLiteralExpr literal;
40 private final Boolean value;
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);
50 public QName getIdentifier() {
51 return function.getName();
55 public Boolean getValue() {
60 * Convert this constant into the equivalent function. This function is provided for bridging purposes only.
62 * @return Equivalent function invocation.
64 public YangFunctionCallExpr asFunction() {
69 * Convert this constant into a string literal, i.e. the result of calling {@code string(boolean)} function on this
72 * @return Literal expression.
74 public YangLiteralExpr asStringLiteral() {
78 public static YangBooleanConstantExpr of(final boolean bool) {
79 return bool ? TRUE : FALSE;
82 public static Optional<YangFunctionCallExpr> forFunctionName(final String functionName) {
83 switch (functionName) {
85 return Optional.of(FALSE.function);
87 return Optional.of(TRUE.function);
89 return Optional.empty();