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