Switch to Objects.requireNonNull
[mdsal.git] / binding2 / mdsal-binding2-api / src / main / java / org / opendaylight / mdsal / binding / javav2 / api / DataTreeIdentifier.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.api;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.io.Serializable;
15 import java.util.Objects;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.concepts.Path;
22
23 /**
24  * A unique identifier for a particular subtree. It's composed of the logical
25  * data store type and the instance identifier of the root node.
26  */
27 @Beta
28 public final class DataTreeIdentifier<T extends TreeNode> implements Immutable,
29         Path<DataTreeIdentifier<?>>, Serializable {
30
31     private static final long serialVersionUID = 1L;
32     private final InstanceIdentifier<T> rootIdentifier;
33     private final LogicalDatastoreType datastoreType;
34
35     private DataTreeIdentifier(final LogicalDatastoreType datastoreType, final InstanceIdentifier<T> rootIdentifier) {
36         this.datastoreType = requireNonNull(datastoreType);
37         this.rootIdentifier = requireNonNull(rootIdentifier);
38     }
39
40     public static <T extends TreeNode> DataTreeIdentifier<T> create(final LogicalDatastoreType datastoreType,
41         final InstanceIdentifier<T> rootIdentifier) {
42         return new DataTreeIdentifier<>(datastoreType, rootIdentifier);
43     }
44
45     /**
46      * Return the logical data store type.
47      *
48      * @return Logical data store type. Guaranteed to be non-null.
49      */
50     @Nonnull
51     public LogicalDatastoreType getDatastoreType() {
52         return datastoreType;
53     }
54
55     /**
56      * Return the {@link InstanceIdentifier} of the root node.
57      *
58      * @return Instance identifier corresponding to the root node.
59      */
60     @Nonnull
61     public InstanceIdentifier<T> getRootIdentifier() {
62         return rootIdentifier;
63     }
64
65     /**
66      * Checks whether this identifier contains some other.
67      * @param other Other path, may not be null.
68      * @return true/false
69      */
70     @Override
71     public boolean contains(@Nonnull final DataTreeIdentifier<?> other) {
72         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
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 int hashCode() {
92         return Objects.hash(rootIdentifier, datastoreType);
93     }
94 }