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.mdsal.dom.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import java.io.Serializable;
14 import java.util.Iterator;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.concepts.Path;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 * A unique identifier for a particular subtree. It is composed of the logical data store type and the instance
25 * identifier of the root node.
28 public final class DOMDataTreeIdentifier implements Immutable, Path<DOMDataTreeIdentifier>, Serializable,
29 Comparable<DOMDataTreeIdentifier> {
30 private static final long serialVersionUID = 1L;
32 private final YangInstanceIdentifier rootIdentifier;
33 private final LogicalDatastoreType datastoreType;
35 public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType,
36 final YangInstanceIdentifier rootIdentifier) {
37 this.datastoreType = requireNonNull(datastoreType);
38 this.rootIdentifier = requireNonNull(rootIdentifier);
42 * Return the logical data store type.
44 * @return Logical data store type. Guaranteed to be non-null.
46 public LogicalDatastoreType getDatastoreType() {
51 * Return the {@link YangInstanceIdentifier} of the root node.
53 * @return Instance identifier corresponding to the root node.
55 public YangInstanceIdentifier getRootIdentifier() {
56 return rootIdentifier;
60 public boolean contains(final DOMDataTreeIdentifier other) {
61 return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
64 public DOMDataTreeIdentifier toOptimized() {
65 final YangInstanceIdentifier opt = rootIdentifier.toOptimized();
66 return opt == rootIdentifier ? this : new DOMDataTreeIdentifier(datastoreType, opt);
70 public int hashCode() {
71 return datastoreType.hashCode() * 31 + rootIdentifier.hashCode();
75 public boolean equals(final @Nullable Object obj) {
79 if (!(obj instanceof DOMDataTreeIdentifier)) {
82 DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
83 return datastoreType == other.datastoreType && rootIdentifier.equals(other.rootIdentifier);
87 public int compareTo(final DOMDataTreeIdentifier domDataTreeIdentifier) {
88 int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
93 final Iterator<PathArgument> myIter = rootIdentifier.getPathArguments().iterator();
94 final Iterator<PathArgument> otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
96 while (myIter.hasNext()) {
97 if (!otherIter.hasNext()) {
101 final PathArgument myPathArg = myIter.next();
102 final PathArgument otherPathArg = otherIter.next();
103 cmp = myPathArg.compareTo(otherPathArg);
109 return otherIter.hasNext() ? -1 : 0;
113 public String toString() {
114 return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();