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