Reduce JSR305 proliferation
[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 static java.util.Objects.requireNonNull;
11
12 import java.io.Serializable;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.concepts.Path;
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 Immutable,
25         Path<DataTreeIdentifier<?>>, Serializable {
26     private static final long serialVersionUID = 1L;
27
28     private final @NonNull InstanceIdentifier<T> rootIdentifier;
29     private final @NonNull LogicalDatastoreType datastoreType;
30
31     public DataTreeIdentifier(final LogicalDatastoreType datastoreType, final InstanceIdentifier<T> rootIdentifier) {
32         this.datastoreType = requireNonNull(datastoreType);
33         this.rootIdentifier = requireNonNull(rootIdentifier);
34     }
35
36     /**
37      * Return the logical data store type.
38      *
39      * @return Logical data store type. Guaranteed to be non-null.
40      */
41     public @NonNull LogicalDatastoreType getDatastoreType() {
42         return datastoreType;
43     }
44
45     /**
46      * Return the {@link InstanceIdentifier} of the root node.
47      *
48      * @return Instance identifier corresponding to the root node.
49      */
50     public @NonNull InstanceIdentifier<T> getRootIdentifier() {
51         return rootIdentifier;
52     }
53
54     @Override
55     public boolean contains(final DataTreeIdentifier<?> other) {
56         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
57     }
58
59     @Override
60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63         result = prime * result + datastoreType.hashCode();
64         result = prime * result + rootIdentifier.hashCode();
65         return result;
66     }
67
68     @Override
69     public boolean equals(final Object obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (!(obj instanceof DataTreeIdentifier)) {
74             return false;
75         }
76         final DataTreeIdentifier<?> other = (DataTreeIdentifier<?>) obj;
77         if (datastoreType != other.datastoreType) {
78             return false;
79         }
80         return rootIdentifier.equals(other.rootIdentifier);
81     }
82
83     @Override
84     public String toString() {
85         return getClass().getSimpleName() + "{datastoreType = " + datastoreType + ", rootIdentifier = "
86                 + rootIdentifier + "}";
87     }
88 }