Mark DataTreeChangeListener for evolution
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMDataTreeIdentifier.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.mdsal.dom.api;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
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.
26  */
27 @NonNullByDefault
28 public final class DOMDataTreeIdentifier implements Immutable, Path<DOMDataTreeIdentifier>, Serializable,
29         Comparable<DOMDataTreeIdentifier> {
30     private static final long serialVersionUID = 1L;
31
32     private final YangInstanceIdentifier rootIdentifier;
33     private final LogicalDatastoreType datastoreType;
34
35     public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType,
36             final YangInstanceIdentifier rootIdentifier) {
37         this.datastoreType = requireNonNull(datastoreType);
38         this.rootIdentifier = requireNonNull(rootIdentifier);
39     }
40
41     /**
42      * Return the logical data store type.
43      *
44      * @return Logical data store type. Guaranteed to be non-null.
45      */
46     public LogicalDatastoreType getDatastoreType() {
47         return datastoreType;
48     }
49
50     /**
51      * Return the {@link YangInstanceIdentifier} of the root node.
52      *
53      * @return Instance identifier corresponding to the root node.
54      */
55     public YangInstanceIdentifier getRootIdentifier() {
56         return rootIdentifier;
57     }
58
59     @Override
60     public boolean contains(final DOMDataTreeIdentifier other) {
61         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
62     }
63
64     public DOMDataTreeIdentifier toOptimized() {
65         final YangInstanceIdentifier opt = rootIdentifier.toOptimized();
66         return opt == rootIdentifier ? this : new DOMDataTreeIdentifier(datastoreType, opt);
67     }
68
69     @Override
70     public int hashCode() {
71         return datastoreType.hashCode() * 31 + rootIdentifier.hashCode();
72     }
73
74     @Override
75     public boolean equals(final @Nullable Object obj) {
76         if (this == obj) {
77             return true;
78         }
79         if (!(obj instanceof DOMDataTreeIdentifier)) {
80             return false;
81         }
82         DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
83         return datastoreType == other.datastoreType && rootIdentifier.equals(other.rootIdentifier);
84     }
85
86     @Override
87     public int compareTo(final DOMDataTreeIdentifier domDataTreeIdentifier) {
88         int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
89         if (cmp != 0) {
90             return cmp;
91         }
92
93         final Iterator<PathArgument> myIter = rootIdentifier.getPathArguments().iterator();
94         final Iterator<PathArgument> otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
95
96         while (myIter.hasNext()) {
97             if (!otherIter.hasNext()) {
98                 return 1;
99             }
100
101             final PathArgument myPathArg = myIter.next();
102             final PathArgument otherPathArg = otherIter.next();
103             cmp = myPathArg.compareTo(otherPathArg);
104             if (cmp != 0) {
105                 return cmp;
106             }
107         }
108
109         return otherIter.hasNext() ? -1 : 0;
110     }
111
112     @Override
113     public String toString() {
114         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
115     }
116 }