991063879b5926aff85fbc3a2627193a1e59575b
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / QNameWithPredicateBuilder.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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.data.impl.leafref;
9
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14
15 class QNameWithPredicateBuilder {
16
17     private List<QNamePredicate> qnamePredicates;
18     private QNameModule moduleQname;
19     private String localName;
20
21     public static final QNameWithPredicateBuilder UP_PARENT_BUILDER = new QNameWithPredicateBuilder(
22             null, "..") {
23         @Override
24         public QNameWithPredicate build() {
25             return QNameWithPredicate.UP_PARENT;
26         }
27     };
28
29     public QNameWithPredicateBuilder(final QNameModule moduleQname, final String localName) {
30         this.moduleQname = moduleQname;
31         this.localName = localName;
32         this.qnamePredicates = new LinkedList<>();
33     }
34
35     public QNameWithPredicate build() {
36         final QNameWithPredicateImpl qNameWithPredicateImpl = new QNameWithPredicateImpl(
37                 moduleQname, localName, qnamePredicates);
38
39         this.qnamePredicates = new LinkedList<>();
40
41         return qNameWithPredicateImpl;
42     }
43
44     public List<QNamePredicate> getQNamePredicates() {
45         return qnamePredicates;
46     }
47
48     public void addQNamePredicate(final QNamePredicate qnamePredicate) {
49         qnamePredicates.add(qnamePredicate);
50     }
51
52     public QNameModule getModuleQname() {
53         return moduleQname;
54     }
55
56     public void setModuleQname(final QNameModule moduleQname) {
57         this.moduleQname = moduleQname;
58     }
59
60     public String getLocalName() {
61         return localName;
62     }
63
64     public void setLocalName(final String localName) {
65         this.localName = localName;
66     }
67
68     // FIXME: check also predicates ...
69     @Override
70     public boolean equals(final Object obj) {
71         if (this == obj) {
72             return true;
73         }
74         if (!(obj instanceof QNameWithPredicateBuilder)) {
75             return false;
76         }
77         final QNameWithPredicateBuilder other = (QNameWithPredicateBuilder) obj;
78         return Objects.equals(localName, other.localName) &&  moduleQname.equals(other.moduleQname);
79     }
80
81     @Override
82     public int hashCode() {
83         int result = moduleQname != null ? moduleQname.hashCode() : 0;
84         result = 31 * result + (localName != null ? localName.hashCode() : 0);
85         return result;
86     }
87
88     @Override
89     public String toString() {
90         final StringBuilder sb = new StringBuilder();
91
92         if (moduleQname != null) {
93             sb.append('(').append(moduleQname.getNamespace());
94             sb.append("?revision=").append(moduleQname.getRevision());
95             sb.append(')');
96         }
97
98         sb.append(localName);
99
100         for (final QNamePredicate predicate : qnamePredicates) {
101             sb.append(predicate);
102         }
103
104         return sb.toString();
105     }
106 }