Do not leak Optional in QNameWithPredicateImpl.toString()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / QNameWithPredicateImpl.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 com.google.common.collect.ImmutableList;
11 import java.io.Serializable;
12 import java.util.List;
13 import java.util.Objects;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.Revision;
19
20 final class QNameWithPredicateImpl implements Immutable, Serializable, QNameWithPredicate {
21
22     private static final long serialVersionUID = 1L;
23
24     private final List<QNamePredicate> qnamePredicates;
25     private final QNameModule moduleQname;
26     private final String localName;
27
28     QNameWithPredicateImpl(final QNameModule moduleQname, final String localName,
29             final List<QNamePredicate> qnamePredicates) {
30         this.moduleQname = moduleQname;
31         this.localName = localName;
32         this.qnamePredicates = ImmutableList.copyOf(qnamePredicates);
33     }
34
35     @Override
36     public List<QNamePredicate> getQNamePredicates() {
37         return qnamePredicates;
38     }
39
40     @Override
41     public QNameModule getModuleQname() {
42         return moduleQname;
43     }
44
45     @Override
46     public String getLocalName() {
47         return localName;
48     }
49
50     @Override
51     public QName getQName() {
52         return QName.create(moduleQname, localName);
53     }
54
55     // FIXME: check also predicates ...
56     @Override
57     public boolean equals(final Object obj) {
58         if (this == obj) {
59             return true;
60         }
61         if (!(obj instanceof QNameWithPredicateImpl)) {
62             return false;
63         }
64         final QNameWithPredicateImpl other = (QNameWithPredicateImpl) obj;
65         return Objects.equals(localName, other.localName) && moduleQname.equals(other.moduleQname);
66     }
67
68     @Override
69     public int hashCode() {
70         int result = moduleQname != null ? moduleQname.hashCode() : 0;
71         result = 31 * result + (localName != null ? localName.hashCode() : 0);
72         return result;
73     }
74
75     @Override
76     public String toString() {
77         final StringBuilder sb = new StringBuilder();
78
79         if (moduleQname != null) {
80             sb.append('(').append(moduleQname.getNamespace());
81             final Optional<Revision> rev = moduleQname.getRevision();
82             if (rev.isPresent()) {
83                 sb.append("?revision=").append(rev.get());
84             }
85             sb.append(')');
86         }
87
88         sb.append(localName);
89
90         for (final QNamePredicate predicate : qnamePredicates) {
91             sb.append(predicate);
92         }
93
94         return sb.toString();
95     }
96 }