Fixed Checkstyle violation errors in mdsal-dom-api module
[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,
26     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,
32             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 domDataTreeIdentifier) {
86         int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
87         if (cmp != 0) {
88             return cmp;
89         }
90
91         final Iterator<PathArgument> myIter = rootIdentifier.getPathArguments().iterator();
92         final Iterator<PathArgument> otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
93
94         while (myIter.hasNext()) {
95             if (!otherIter.hasNext()) {
96                 return 1;
97             }
98
99             final PathArgument myPathArg = myIter.next();
100             final PathArgument otherPathArg = otherIter.next();
101             cmp = myPathArg.compareTo(otherPathArg);
102             if (cmp != 0) {
103                 return cmp;
104             }
105         }
106
107         return otherIter.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 }