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