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