Merge "Bug 3051: Fixed pattern checks in generated DTOs"
[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 org.opendaylight.yangtools.yang.common.QNameModule;
12
13 class QNameWithPredicateBuilder {
14
15     private LinkedList<QNamePredicate> qnamePredicates;
16     private QNameModule moduleQname;
17     private String localName;
18
19     public static QNameWithPredicateBuilder UP_PARENT_BUILDER = new QNameWithPredicateBuilder(
20             null, "..") {
21         @Override
22         public QNameWithPredicate build() {
23             return QNameWithPredicate.UP_PARENT;
24         }
25     };
26
27     public QNameWithPredicateBuilder(final QNameModule moduleQname, final String localName) {
28         this.moduleQname = moduleQname;
29         this.localName = localName;
30         this.qnamePredicates = new LinkedList<QNamePredicate>();
31     }
32
33     public QNameWithPredicate build() {
34         final QNameWithPredicateImpl qNameWithPredicateImpl = new QNameWithPredicateImpl(
35                 moduleQname, localName, qnamePredicates);
36
37         this.qnamePredicates = new LinkedList<QNamePredicate>();
38
39         return qNameWithPredicateImpl;
40     }
41
42     public LinkedList<QNamePredicate> getQNamePredicates() {
43         return qnamePredicates;
44     }
45
46     public void addQNamePredicate(final QNamePredicate qnamePredicate) {
47         qnamePredicates.add(qnamePredicate);
48     }
49
50     public QNameModule getModuleQname() {
51         return moduleQname;
52     }
53
54     public void setModuleQname(final QNameModule moduleQname) {
55         this.moduleQname = moduleQname;
56     }
57
58     public String getLocalName() {
59         return localName;
60     }
61
62     public void setLocalName(final String localName) {
63         this.localName = localName;
64     }
65
66     // FIXME: check also predicates ...
67     @Override
68     public boolean equals(final Object obj) {
69         if (this == obj) {
70             return true;
71         }
72         if (!(obj instanceof QNameWithPredicateBuilder)) {
73             return false;
74         }
75         final QNameWithPredicateBuilder other = (QNameWithPredicateBuilder) obj;
76         if (localName == null) {
77             if (other.localName != null) {
78                 return false;
79             }
80         } else if (!localName.equals(other.localName)) {
81             return false;
82         }
83         return moduleQname.equals(other.moduleQname);
84     }
85
86     @Override
87     public String toString() {
88         final StringBuilder sb = new StringBuilder();
89
90         if (moduleQname != null) {
91             sb.append("(" + moduleQname.getNamespace());
92             sb.append("?revision=" + moduleQname.getRevision());
93             sb.append(")");
94         }
95
96         sb.append(localName);
97
98         for (final QNamePredicate predicate : qnamePredicates) {
99             sb.append(predicate);
100         }
101
102         return sb.toString();
103     }
104 }