Reduce JSR305 proliferation
[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 package org.opendaylight.controller.md.sal.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.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,
27         Path<DOMDataTreeIdentifier>, Serializable, Comparable<DOMDataTreeIdentifier> {
28     private static final long serialVersionUID = 1L;
29     private final @NonNull YangInstanceIdentifier rootIdentifier;
30     private final @NonNull LogicalDatastoreType datastoreType;
31
32     public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType,
33             final YangInstanceIdentifier rootIdentifier) {
34         this.datastoreType = requireNonNull(datastoreType);
35         this.rootIdentifier = requireNonNull(rootIdentifier);
36     }
37
38     /**
39      * Return a counterpart to an MD-SAL data tree identifier.
40      *
41      * @return Controller data tree identifier.
42      */
43     public static DOMDataTreeIdentifier fromMdsal(final org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier mdsal) {
44         return new DOMDataTreeIdentifier(LogicalDatastoreType.fromMdsal(mdsal.getDatastoreType()),
45             mdsal.getRootIdentifier());
46     }
47
48     /**
49      * Return MD-SAL counterpart of this object.
50      *
51      * @return MD-SAL data tree identifier.
52      */
53     public org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier toMdsal() {
54         return new org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier(datastoreType.toMdsal(), rootIdentifier);
55     }
56
57     /**
58      * Return the logical data store type.
59      *
60      * @return Logical data store type. Guaranteed to be non-null.
61      */
62     public @NonNull LogicalDatastoreType getDatastoreType() {
63         return datastoreType;
64     }
65
66     /**
67      * Return the {@link YangInstanceIdentifier} of the root node.
68      *
69      * @return Instance identifier corresponding to the root node.
70      */
71     public @NonNull YangInstanceIdentifier getRootIdentifier() {
72         return rootIdentifier;
73     }
74
75     @Override
76     public boolean contains(final DOMDataTreeIdentifier other) {
77         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
78     }
79
80     @Override
81     public int hashCode() {
82         final int prime = 31;
83         int result = 1;
84         result = prime * result + datastoreType.hashCode();
85         result = prime * result + rootIdentifier.hashCode();
86         return result;
87     }
88
89     @Override
90     public boolean equals(final Object obj) {
91         if (this == obj) {
92             return true;
93         }
94         if (!(obj instanceof DOMDataTreeIdentifier)) {
95             return false;
96         }
97         DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
98         if (datastoreType != other.datastoreType) {
99             return false;
100         }
101         return rootIdentifier.equals(other.rootIdentifier);
102     }
103
104     @Override
105     public int compareTo(final DOMDataTreeIdentifier obj) {
106         int cmp = datastoreType.compareTo(obj.datastoreType);
107         if (cmp != 0) {
108             return cmp;
109         }
110
111         final Iterator<PathArgument> mi = rootIdentifier.getPathArguments().iterator();
112         final Iterator<PathArgument> oi = obj.rootIdentifier.getPathArguments().iterator();
113
114         while (mi.hasNext()) {
115             if (!oi.hasNext()) {
116                 return 1;
117             }
118
119             final PathArgument ma = mi.next();
120             final PathArgument oa = oi.next();
121             cmp = ma.compareTo(oa);
122             if (cmp != 0) {
123                 return cmp;
124             }
125         }
126
127         return oi.hasNext() ? -1 : 0;
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
133     }
134 }