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