Merge "Bug 2731: Discard changes only when transaction exist."
[controller.git] / opendaylight / md-sal / sal-binding-api / src / main / java / org / opendaylight / controller / md / sal / binding / api / DataTreeIdentifier.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.controller.md.sal.binding.api;
9
10 import com.google.common.base.Preconditions;
11 import java.io.Serializable;
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.binding.InstanceIdentifier;
17
18 /**
19  * A unique identifier for a particular subtree. It is composed of the logical
20  * data store type and the instance identifier of the root node.
21  */
22 public final class DataTreeIdentifier implements Immutable, Path<DataTreeIdentifier>, Serializable {
23     private static final long serialVersionUID = 1L;
24     private final InstanceIdentifier<?> rootIdentifier;
25     private final LogicalDatastoreType datastoreType;
26
27     public DataTreeIdentifier(final LogicalDatastoreType datastoreType, final InstanceIdentifier<?> rootIdentifier) {
28         this.datastoreType = Preconditions.checkNotNull(datastoreType);
29         this.rootIdentifier = Preconditions.checkNotNull(rootIdentifier);
30     }
31
32     /**
33      * Return the logical data store type.
34      *
35      * @return Logical data store type. Guaranteed to be non-null.
36      */
37     public @Nonnull LogicalDatastoreType getDatastoreType() {
38         return datastoreType;
39     }
40
41     /**
42      * Return the {@link YangInstanceIdentifier} of the root node.
43      *
44      * @return Instance identifier corresponding to the root node.
45      */
46     public @Nonnull InstanceIdentifier<?> getRootIdentifier() {
47         return rootIdentifier;
48     }
49
50     @Override
51     public boolean contains(final DataTreeIdentifier other) {
52         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
53     }
54
55     @Override
56     public int hashCode() {
57         final int prime = 31;
58         int result = 1;
59         result = prime * result + datastoreType.hashCode();
60         result = prime * result + rootIdentifier.hashCode();
61         return result;
62     }
63
64     @Override
65     public boolean equals(final Object obj) {
66         if (this == obj) {
67             return true;
68         }
69         if (!(obj instanceof DataTreeIdentifier)) {
70             return false;
71         }
72         DataTreeIdentifier other = (DataTreeIdentifier) obj;
73         if (datastoreType != other.datastoreType) {
74             return false;
75         }
76         return rootIdentifier.equals(other.rootIdentifier);
77     }
78 }