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