Create a dedicated QName-only implementation, saving some space.
Change-Id: I0218763aaf4e809f50b580677fc0b634b7c918fa
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.leafref;
+
+import java.io.Serializable;
+import java.util.Objects;
+import java.util.Optional;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
+
+abstract class AbstractQNameWithPredicate implements Immutable, Serializable, QNameWithPredicate {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public final boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof QNameWithPredicate)) {
+ return false;
+ }
+ final QNameWithPredicate other = (QNameWithPredicate) obj;
+ // FIXME: check also predicates ...
+ return Objects.equals(getLocalName(), other.getLocalName())
+ && Objects.equals(getModuleQname(), other.getModuleQname());
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(getModuleQname(), getLocalName());
+ }
+
+ @Override
+ public final String toString() {
+ final StringBuilder sb = new StringBuilder();
+
+ final QNameModule moduleQname = getModuleQname();
+ if (moduleQname != null) {
+ sb.append('(').append(moduleQname.getNamespace());
+ final Optional<Revision> rev = moduleQname.getRevision();
+ if (rev.isPresent()) {
+ sb.append("?revision=").append(rev.get());
+ }
+ sb.append(')');
+ }
+
+ sb.append(getLocalName());
+ getQNamePredicates().forEach(sb::append);
+ return sb.toString();
+ }
+}
if (child instanceof DataNodeContainer) {
if (!(child instanceof CaseSchemaNode)) {
- xpath.add(new QNameWithPredicateBuilder(qname).build());
+ xpath.add(new SimpleQNameWithPredicate(qname));
}
currenDataNodeContainer = (DataNodeContainer) child;
} else if (child instanceof ChoiceSchemaNode) {
break;
}
} else if (child instanceof LeafSchemaNode || child instanceof LeafListSchemaNode) {
- xpath.add(new QNameWithPredicateBuilder(qname).build());
+ xpath.add(new SimpleQNameWithPredicate(qname));
break;
} else if (child == null) {
throw new IllegalArgumentException("No child " + qname + " found in node container "
}
};
- QNameWithPredicateBuilder(final QName qname) {
- this(qname.getModule(), qname.getLocalName());
- }
-
QNameWithPredicateBuilder(final QNameModule moduleQname, final String localName) {
this.moduleQname = moduleQname;
this.localName = localName;
@Override
public QNameWithPredicate build() {
- final QNameWithPredicateImpl qNameWithPredicateImpl = new QNameWithPredicateImpl(
- moduleQname, localName, qnamePredicates);
+ if (qnamePredicates.isEmpty() && moduleQname != null && localName != null) {
+ return new SimpleQNameWithPredicate(QName.create(moduleQname, localName));
+ }
+
+ final QNameWithPredicateImpl qNameWithPredicateImpl = new QNameWithPredicateImpl(moduleQname, localName,
+ qnamePredicates);
// QNameWithPredicateImpl has taken a copy
qnamePredicates.clear();
package org.opendaylight.yangtools.yang.data.impl.leafref;
import com.google.common.collect.ImmutableList;
-import java.io.Serializable;
import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
-import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.common.Revision;
-final class QNameWithPredicateImpl implements Immutable, Serializable, QNameWithPredicate {
+final class QNameWithPredicateImpl extends AbstractQNameWithPredicate {
private static final long serialVersionUID = 1L;
public QName getQName() {
return QName.create(moduleQname, localName);
}
-
- // FIXME: check also predicates ...
- @Override
- public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof QNameWithPredicateImpl)) {
- return false;
- }
- final QNameWithPredicateImpl other = (QNameWithPredicateImpl) obj;
- return Objects.equals(localName, other.localName) && moduleQname.equals(other.moduleQname);
- }
-
- @Override
- public int hashCode() {
- int result = moduleQname != null ? moduleQname.hashCode() : 0;
- result = 31 * result + (localName != null ? localName.hashCode() : 0);
- return result;
- }
-
- @Override
- public String toString() {
- final StringBuilder sb = new StringBuilder();
-
- if (moduleQname != null) {
- sb.append('(').append(moduleQname.getNamespace());
- final Optional<Revision> rev = moduleQname.getRevision();
- if (rev.isPresent()) {
- sb.append("?revision=").append(rev.get());
- }
- sb.append(')');
- }
-
- sb.append(localName);
-
- for (final QNamePredicate predicate : qnamePredicates) {
- sb.append(predicate);
- }
-
- return sb.toString();
- }
}
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.leafref;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+
+public class SimpleQNameWithPredicate extends AbstractQNameWithPredicate {
+ private static final long serialVersionUID = 1L;
+
+ private final QName qname;
+
+ SimpleQNameWithPredicate(final QName qname) {
+ this.qname = requireNonNull(qname);
+ }
+
+ @Override
+ public List<QNamePredicate> getQNamePredicates() {
+ return ImmutableList.of();
+ }
+
+ @Override
+ public QNameModule getModuleQname() {
+ return qname.getModule();
+ }
+
+ @Override
+ public String getLocalName() {
+ return qname.getLocalName();
+ }
+
+ @Override
+ public QName getQName() {
+ return qname;
+ }
+}