b2a92f91036a8cf4b05bacf2e7cf9336b519f229
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / 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.mdsal.binding.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.io.Serial;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 /**
21  * A unique identifier for a particular subtree. It is composed of the logical
22  * data store type and the instance identifier of the root node.
23  */
24 public final class DataTreeIdentifier<T extends DataObject> implements HierarchicalIdentifier<DataTreeIdentifier<?>> {
25     @Serial
26     private static final long serialVersionUID = 1L;
27
28     private final @NonNull InstanceIdentifier<T> rootIdentifier;
29     private final @NonNull LogicalDatastoreType datastoreType;
30
31     private DataTreeIdentifier(final @NonNull LogicalDatastoreType datastoreType,
32             final @NonNull InstanceIdentifier<T> rootIdentifier) {
33         this.datastoreType = requireNonNull(datastoreType);
34         this.rootIdentifier = requireNonNull(rootIdentifier);
35     }
36
37     public static <T extends DataObject> @NonNull DataTreeIdentifier<T> create(
38             final @NonNull LogicalDatastoreType datastoreType, final @NonNull InstanceIdentifier<T> rootIdentifier) {
39         return new DataTreeIdentifier<>(datastoreType, rootIdentifier);
40     }
41
42     /**
43      * Return the logical data store type.
44      *
45      * @return Logical data store type. Guaranteed to be non-null.
46      */
47     public @NonNull LogicalDatastoreType getDatastoreType() {
48         return datastoreType;
49     }
50
51     /**
52      * Return the {@link InstanceIdentifier} of the root node.
53      *
54      * @return Instance identifier corresponding to the root node.
55      */
56     public @NonNull InstanceIdentifier<T> getRootIdentifier() {
57         return rootIdentifier;
58     }
59
60     @Override
61     public boolean contains(final DataTreeIdentifier<?> other) {
62         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
63     }
64
65     @Override
66     public int hashCode() {
67         final int prime = 31;
68         int result = 1;
69         result = prime * result + datastoreType.hashCode();
70         result = prime * result + rootIdentifier.hashCode();
71         return result;
72     }
73
74     @Override
75     public boolean equals(final Object obj) {
76         if (this == obj) {
77             return true;
78         }
79         if (!(obj instanceof DataTreeIdentifier)) {
80             return false;
81         }
82         final DataTreeIdentifier<?> other = (DataTreeIdentifier<?>) obj;
83         if (datastoreType != other.datastoreType) {
84             return false;
85         }
86         return rootIdentifier.equals(other.rootIdentifier);
87     }
88
89     @Override
90     public String toString() {
91         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
92     }
93 }