Make sure we optimize DOMDataTreeIdentifier
[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,
28         Comparable<DOMDataTreeIdentifier> {
29     private static final long serialVersionUID = 1L;
30
31     private final YangInstanceIdentifier rootIdentifier;
32     private final LogicalDatastoreType datastoreType;
33
34     public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType, final YangInstanceIdentifier rootIdentifier) {
35         this.datastoreType = Preconditions.checkNotNull(datastoreType);
36         this.rootIdentifier = Preconditions.checkNotNull(rootIdentifier);
37     }
38
39     /**
40      * Return the logical data store type.
41      *
42      * @return Logical data store type. Guaranteed to be non-null.
43      */
44     public @Nonnull LogicalDatastoreType getDatastoreType() {
45         return datastoreType;
46     }
47
48     /**
49      * Return the {@link YangInstanceIdentifier} of the root node.
50      *
51      * @return Instance identifier corresponding to the root node.
52      */
53     public @Nonnull YangInstanceIdentifier getRootIdentifier() {
54         return rootIdentifier;
55     }
56
57     @Override
58     public boolean contains(final DOMDataTreeIdentifier other) {
59         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
60     }
61
62     public DOMDataTreeIdentifier toOptimized() {
63         final YangInstanceIdentifier opt = rootIdentifier.toOptimized();
64         return opt == rootIdentifier ? this : new DOMDataTreeIdentifier(datastoreType, opt);
65     }
66
67     @Override
68     public int hashCode() {
69         final int prime = 31;
70         int result = 1;
71         result = prime * result + datastoreType.hashCode();
72         result = prime * result + rootIdentifier.hashCode();
73         return result;
74     }
75
76     @Override
77     public boolean equals(final Object obj) {
78         if (this == obj) {
79             return true;
80         }
81         if (!(obj instanceof DOMDataTreeIdentifier)) {
82             return false;
83         }
84         DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
85         if (datastoreType != other.datastoreType) {
86             return false;
87         }
88         return rootIdentifier.equals(other.rootIdentifier);
89     }
90
91     @Override
92     public int compareTo(final DOMDataTreeIdentifier o) {
93         int i = datastoreType.compareTo(o.datastoreType);
94         if (i != 0) {
95             return i;
96         }
97
98         final Iterator<PathArgument> mi = rootIdentifier.getPathArguments().iterator();
99         final Iterator<PathArgument> oi = o.rootIdentifier.getPathArguments().iterator();
100
101         while (mi.hasNext()) {
102             if (!oi.hasNext()) {
103                 return 1;
104             }
105
106             final PathArgument ma = mi.next();
107             final PathArgument oa = oi.next();
108             i = ma.compareTo(oa);
109             if (i != 0) {
110                 return i;
111             }
112         }
113
114         return oi.hasNext() ? -1 : 0;
115     }
116
117     @Override
118     public String toString() {
119         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
120     }
121 }