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 org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 * A unique identifier for a particular subtree. It is composed of the logical data store type and the instance
21 * identifier of the root node.
24 public final class DOMDataTreeIdentifier implements HierarchicalIdentifier<DOMDataTreeIdentifier>,
25 Comparable<DOMDataTreeIdentifier> {
27 private static final long serialVersionUID = 1L;
29 private final YangInstanceIdentifier rootIdentifier;
30 private final LogicalDatastoreType datastoreType;
32 public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType,
33 final YangInstanceIdentifier rootIdentifier) {
34 this.datastoreType = requireNonNull(datastoreType);
35 this.rootIdentifier = requireNonNull(rootIdentifier);
39 * Return the logical data store type.
41 * @return Logical data store type. Guaranteed to be non-null.
43 public LogicalDatastoreType getDatastoreType() {
48 * Return the {@link YangInstanceIdentifier} of the root node.
50 * @return Instance identifier corresponding to the root node.
52 public YangInstanceIdentifier getRootIdentifier() {
53 return rootIdentifier;
57 public boolean contains(final DOMDataTreeIdentifier other) {
58 return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
61 public DOMDataTreeIdentifier toOptimized() {
62 final var opt = rootIdentifier.toOptimized();
63 return opt == rootIdentifier ? this : new DOMDataTreeIdentifier(datastoreType, opt);
67 public int hashCode() {
68 return datastoreType.hashCode() * 31 + rootIdentifier.hashCode();
72 public boolean equals(final @Nullable Object obj) {
73 return this == obj || obj instanceof DOMDataTreeIdentifier other && datastoreType == other.datastoreType
74 && rootIdentifier.equals(other.rootIdentifier);
78 public int compareTo(final DOMDataTreeIdentifier domDataTreeIdentifier) {
79 int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
84 final var myIter = rootIdentifier.getPathArguments().iterator();
85 final var otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
87 while (myIter.hasNext()) {
88 if (!otherIter.hasNext()) {
92 final var myPathArg = myIter.next();
93 final var otherPathArg = otherIter.next();
94 cmp = myPathArg.compareTo(otherPathArg);
100 return otherIter.hasNext() ? -1 : 0;
104 public String toString() {
105 return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
109 Object writeReplace() {
110 return new DTIv1(this);