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.impl.leafref;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.Builder;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
17 class QNameWithPredicateBuilder implements Builder<QNameWithPredicate> {
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;
36 public QNameWithPredicate build() {
37 if (qnamePredicates.isEmpty() && moduleQname != null && localName != null) {
38 return new SimpleQNameWithPredicate(QName.create(moduleQname, localName));
41 final QNameWithPredicateImpl qNameWithPredicateImpl = new QNameWithPredicateImpl(moduleQname, localName,
44 // QNameWithPredicateImpl has taken a copy
45 qnamePredicates.clear();
46 return qNameWithPredicateImpl;
49 public List<QNamePredicate> getQNamePredicates() {
50 return qnamePredicates;
53 public void addQNamePredicate(final QNamePredicate qnamePredicate) {
54 qnamePredicates.add(qnamePredicate);
57 public QNameModule getModuleQname() {
61 public void setModuleQname(final QNameModule moduleQname) {
62 this.moduleQname = moduleQname;
65 public String getLocalName() {
69 public void setLocalName(final String localName) {
70 this.localName = localName;
73 // FIXME: check also predicates ...
75 public boolean equals(final Object obj) {
79 if (!(obj instanceof QNameWithPredicateBuilder)) {
82 final QNameWithPredicateBuilder other = (QNameWithPredicateBuilder) obj;
83 return Objects.equals(localName, other.localName) && moduleQname.equals(other.moduleQname);
87 public int hashCode() {
88 int result = moduleQname != null ? moduleQname.hashCode() : 0;
89 result = 31 * result + Objects.hashCode(localName);
94 public String toString() {
95 final StringBuilder sb = new StringBuilder();
97 if (moduleQname != null) {
98 sb.append('(').append(moduleQname.getNamespace());
99 sb.append("?revision=").append(moduleQname.getRevision());
103 sb.append(localName);
105 for (final QNamePredicate predicate : qnamePredicates) {
106 sb.append(predicate);
109 return sb.toString();