407c466c3998a168c6a83cf55556e62c5245c8d8
[controller.git] / opendaylight / md-sal / sal-dom-api / src / main / java / org / opendaylight / controller / md / sal / 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
9 package org.opendaylight.controller.md.sal.dom.api;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.io.Serializable;
14 import java.util.Iterator;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.concepts.Path;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21
22 /**
23  * A unique identifier for a particular subtree. It is composed of the logical
24  * data store type and the instance identifier of the root node.
25  */
26 public final class DOMDataTreeIdentifier implements Immutable, Path<DOMDataTreeIdentifier>, Serializable, Comparable<DOMDataTreeIdentifier> {
27     private static final long serialVersionUID = 1L;
28     private final YangInstanceIdentifier rootIdentifier;
29     private final LogicalDatastoreType datastoreType;
30
31     public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType, final YangInstanceIdentifier rootIdentifier) {
32         this.datastoreType = Preconditions.checkNotNull(datastoreType);
33         this.rootIdentifier = Preconditions.checkNotNull(rootIdentifier);
34     }
35
36     /**
37      * Return the logical data store type.
38      *
39      * @return Logical data store type. Guaranteed to be non-null.
40      */
41     public @Nonnull LogicalDatastoreType getDatastoreType() {
42         return datastoreType;
43     }
44
45     /**
46      * Return the {@link YangInstanceIdentifier} of the root node.
47      *
48      * @return Instance identifier corresponding to the root node.
49      */
50     public @Nonnull YangInstanceIdentifier getRootIdentifier() {
51         return rootIdentifier;
52     }
53
54     @Override
55     public boolean contains(final DOMDataTreeIdentifier other) {
56         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
57     }
58
59     @Override
60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63         result = prime * result + datastoreType.hashCode();
64         result = prime * result + rootIdentifier.hashCode();
65         return result;
66     }
67
68     @Override
69     public boolean equals(final Object obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (!(obj instanceof DOMDataTreeIdentifier)) {
74             return false;
75         }
76         DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
77         if (datastoreType != other.datastoreType) {
78             return false;
79         }
80         return rootIdentifier.equals(other.rootIdentifier);
81     }
82
83     @Override
84     public int compareTo(final DOMDataTreeIdentifier o) {
85         int i = datastoreType.compareTo(o.datastoreType);
86         if (i != 0) {
87             return i;
88         }
89
90         final Iterator<PathArgument> mi = rootIdentifier.getPathArguments().iterator();
91         final Iterator<PathArgument> oi = o.rootIdentifier.getPathArguments().iterator();
92
93         while (mi.hasNext()) {
94             if (!oi.hasNext()) {
95                 return 1;
96             }
97
98             final PathArgument ma = mi.next();
99             final PathArgument oa = oi.next();
100             i = ma.compareTo(oa);
101             if (i != 0) {
102                 return i;
103             }
104         }
105
106         return oi.hasNext() ? -1 : 0;
107     }
108
109     @Override
110     public String toString() {
111         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
112     }
113 }