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 org.eclipse.jdt.annotation.Nullable;
16 * An XPath literal expression.
19 * Note that a literal may be required to hold a value of {@code instance-identifier} or {@code identityref} type,
20 * when the corresponding {@link YangXPathExpression} was parsed from reference String specification defined in RFC7950.
21 * When such conversion is required, it should be performed through
22 * {@link YangXPathExpression#interpretAsQName(YangLiteralExpr)} or
23 * {@link YangXPathExpression#interpretAsInstanceIdentifier(YangLiteralExpr)}.
26 * A more type-safe alternative is {@link YangQNameExpr}, which should be preferred and used whenever possible.
28 * @author Robert Varga
31 public final class YangLiteralExpr implements YangExpr {
32 private static final long serialVersionUID = 1L;
33 private static final YangLiteralExpr EMPTY = new YangLiteralExpr("");
35 private final String literal;
37 protected YangLiteralExpr(final String literal) {
38 this.literal = requireNonNull(literal);
41 public static YangLiteralExpr empty() {
45 public static YangLiteralExpr of(final String literal) {
46 return literal.isEmpty() ? EMPTY : new YangLiteralExpr(literal);
49 public String getLiteral() {
54 public int hashCode() {
55 return literal.hashCode();
59 public boolean equals(final @Nullable Object obj) {
60 return this == obj || obj instanceof YangLiteralExpr && literal.equals(((YangLiteralExpr) obj).literal);
64 public String toString() {
68 protected Object readResolve() {
69 return literal.isEmpty() ? EMPTY : this;