2 * Copyright (c) 2015 Cisco Systems, Inc. and others. 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.data.tree.leafref;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Objects;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.Mutable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
18 class QNameWithPredicateBuilder implements Mutable {
19 private final List<QNamePredicate> qnamePredicates = new ArrayList<>();
20 private QNameModule moduleQname;
21 private String localName;
23 static final QNameWithPredicateBuilder UP_PARENT_BUILDER = new QNameWithPredicateBuilder(null, "..") {
25 public QNameWithPredicate build() {
26 return QNameWithPredicate.UP_PARENT;
30 QNameWithPredicateBuilder(final QNameModule moduleQname, final String localName) {
31 this.moduleQname = moduleQname;
32 this.localName = localName;
35 public @NonNull QNameWithPredicate build() {
36 if (qnamePredicates.isEmpty() && moduleQname != null && localName != null) {
37 return new SimpleQNameWithPredicate(QName.create(moduleQname, localName));
40 final QNameWithPredicateImpl qNameWithPredicateImpl = new QNameWithPredicateImpl(moduleQname, localName,
43 // QNameWithPredicateImpl has taken a copy
44 qnamePredicates.clear();
45 return qNameWithPredicateImpl;
48 public List<QNamePredicate> getQNamePredicates() {
49 return qnamePredicates;
52 public void addQNamePredicate(final QNamePredicate qnamePredicate) {
53 qnamePredicates.add(qnamePredicate);
56 public QNameModule getModuleQname() {
60 public void setModuleQname(final QNameModule moduleQname) {
61 this.moduleQname = moduleQname;
64 public String getLocalName() {
68 public void setLocalName(final String localName) {
69 this.localName = localName;
72 // FIXME: check also predicates ...
74 public boolean equals(final Object obj) {
78 if (!(obj instanceof QNameWithPredicateBuilder)) {
81 final QNameWithPredicateBuilder other = (QNameWithPredicateBuilder) obj;
82 return Objects.equals(localName, other.localName) && moduleQname.equals(other.moduleQname);
86 public int hashCode() {
87 int result = moduleQname != null ? moduleQname.hashCode() : 0;
88 result = 31 * result + Objects.hashCode(localName);
93 public String toString() {
94 final StringBuilder sb = new StringBuilder();
96 if (moduleQname != null) {
97 sb.append('(').append(moduleQname.getNamespace());
98 sb.append("?revision=").append(moduleQname.getRevision());
102 sb.append(localName);
104 for (final QNamePredicate predicate : qnamePredicates) {
105 sb.append(predicate);
108 return sb.toString();