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