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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.io.Serializable;
13 import java.util.Iterator;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.concepts.Path;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 * A unique identifier for a particular subtree. It is composed of the logical
23 * data store type and the instance identifier of the root node.
25 public final class DOMDataTreeIdentifier implements Immutable, Path<DOMDataTreeIdentifier>, Serializable,
26 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 = Preconditions.checkNotNull(datastoreType);
35 this.rootIdentifier = Preconditions.checkNotNull(rootIdentifier);
39 * Return the logical data store type.
41 * @return Logical data store type. Guaranteed to be non-null.
44 public LogicalDatastoreType getDatastoreType() {
49 * Return the {@link YangInstanceIdentifier} of the root node.
51 * @return Instance identifier corresponding to the root node.
54 public YangInstanceIdentifier getRootIdentifier() {
55 return rootIdentifier;
59 public boolean contains(final DOMDataTreeIdentifier other) {
60 return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
63 public DOMDataTreeIdentifier toOptimized() {
64 final YangInstanceIdentifier opt = rootIdentifier.toOptimized();
65 return opt == rootIdentifier ? this : new DOMDataTreeIdentifier(datastoreType, opt);
69 public int hashCode() {
72 result = prime * result + datastoreType.hashCode();
73 result = prime * result + rootIdentifier.hashCode();
78 public boolean equals(final Object obj) {
82 if (!(obj instanceof DOMDataTreeIdentifier)) {
85 DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
86 if (datastoreType != other.datastoreType) {
89 return rootIdentifier.equals(other.rootIdentifier);
93 public int compareTo(final DOMDataTreeIdentifier domDataTreeIdentifier) {
94 int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
99 final Iterator<PathArgument> myIter = rootIdentifier.getPathArguments().iterator();
100 final Iterator<PathArgument> otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
102 while (myIter.hasNext()) {
103 if (!otherIter.hasNext()) {
107 final PathArgument myPathArg = myIter.next();
108 final PathArgument otherPathArg = otherIter.next();
109 cmp = myPathArg.compareTo(otherPathArg);
115 return otherIter.hasNext() ? -1 : 0;
119 public String toString() {
120 return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();