Fix checkstyle violations in sal-dom-api
[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,
27         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,
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     public @Nonnull LogicalDatastoreType getDatastoreType() {
44         return datastoreType;
45     }
46
47     /**
48      * Return the {@link YangInstanceIdentifier} of the root node.
49      *
50      * @return Instance identifier corresponding to the root node.
51      */
52     public @Nonnull YangInstanceIdentifier getRootIdentifier() {
53         return rootIdentifier;
54     }
55
56     @Override
57     public boolean contains(final DOMDataTreeIdentifier other) {
58         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
59     }
60
61     @Override
62     public int hashCode() {
63         final int prime = 31;
64         int result = 1;
65         result = prime * result + datastoreType.hashCode();
66         result = prime * result + rootIdentifier.hashCode();
67         return result;
68     }
69
70     @Override
71     public boolean equals(final Object obj) {
72         if (this == obj) {
73             return true;
74         }
75         if (!(obj instanceof DOMDataTreeIdentifier)) {
76             return false;
77         }
78         DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
79         if (datastoreType != other.datastoreType) {
80             return false;
81         }
82         return rootIdentifier.equals(other.rootIdentifier);
83     }
84
85     @Override
86     public int compareTo(final DOMDataTreeIdentifier obj) {
87         int cmp = datastoreType.compareTo(obj.datastoreType);
88         if (cmp != 0) {
89             return cmp;
90         }
91
92         final Iterator<PathArgument> mi = rootIdentifier.getPathArguments().iterator();
93         final Iterator<PathArgument> oi = obj.rootIdentifier.getPathArguments().iterator();
94
95         while (mi.hasNext()) {
96             if (!oi.hasNext()) {
97                 return 1;
98             }
99
100             final PathArgument ma = mi.next();
101             final PathArgument oa = oi.next();
102             cmp = ma.compareTo(oa);
103             if (cmp != 0) {
104                 return cmp;
105             }
106         }
107
108         return oi.hasNext() ? -1 : 0;
109     }
110
111     @Override
112     public String toString() {
113         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
114     }
115 }