58bfc5ac26ca23b484789cc47c8e8cfeeeaf9456
[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 org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.common.api.LogicalDatastorePath;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18
19 /**
20  * A Binding version of {@link LogicalDatastorePath}. Uses {@link InstanceIdentifier} for path addressing.
21  */
22 public final class DataTreeIdentifier<T extends DataObject>
23         implements LogicalDatastorePath<@NonNull DataTreeIdentifier<?>, @NonNull InstanceIdentifier<?>> {
24     @java.io.Serial
25     private static final long serialVersionUID = 1L;
26
27     private final @NonNull InstanceIdentifier<T> rootIdentifier;
28     private final @NonNull LogicalDatastoreType datastoreType;
29
30     private DataTreeIdentifier(final @NonNull LogicalDatastoreType datastore,
31             final @NonNull InstanceIdentifier<T> path) {
32         datastoreType = requireNonNull(datastore);
33         rootIdentifier = requireNonNull(path);
34     }
35
36     /**
37      * Create a new {@link DataTreeIdentifier} with specified datastore and path.
38      *
39      * @param <T> target {@link DataObject} type
40      * @param datastore {@link LogicalDatastoreType} of this identifier
41      * @param path {@link InstanceIdentifier} path of this identifier
42      * @throws NullPointerException if any argument is {@code null}
43      */
44     public static <T extends DataObject> @NonNull DataTreeIdentifier<T> of(
45             final @NonNull LogicalDatastoreType datastore, final @NonNull InstanceIdentifier<T> path) {
46         return new DataTreeIdentifier<>(datastore, path);
47     }
48
49     /**
50      * Create a new {@link DataTreeIdentifier} with specified datastore and path.
51      *
52      * @param <T> target {@link DataObject} type
53      * @param datastore {@link LogicalDatastoreType} of this identifier
54      * @param path {@link InstanceIdentifier} path of this identifier
55      * @throws NullPointerException if any argument is {@code null}
56      * @deprecated Use {@link #of(LogicalDatastoreType, InstanceIdentifier)} instead
57      */
58     @Deprecated(since = "13.0.0", forRemoval = true)
59     public static <T extends DataObject> @NonNull DataTreeIdentifier<T> create(
60             final @NonNull LogicalDatastoreType datastore, final @NonNull InstanceIdentifier<T> path) {
61         return of(datastore, path);
62     }
63
64     @Override
65     public LogicalDatastoreType datastore() {
66         return datastoreType;
67     }
68
69     /**
70      * Return the logical data store type.
71      *
72      * @return Logical data store type. Guaranteed to be non-null.
73      * @deprecated Use {@link #datastore()} instead
74      */
75     @Deprecated(since = "13.0.0", forRemoval = true)
76     public @NonNull LogicalDatastoreType getDatastoreType() {
77         return datastore();
78     }
79
80     @Override
81     public InstanceIdentifier<T> path() {
82         return rootIdentifier;
83     }
84
85     /**
86      * Return the {@link InstanceIdentifier} of the root node.
87      *
88      * @return Instance identifier corresponding to the root node.
89      * @deprecated Use {@link #path()} instead
90      */
91     @Deprecated(since = "13.0.0", forRemoval = true)
92     public @NonNull InstanceIdentifier<T> getRootIdentifier() {
93         return path();
94     }
95
96     @Override
97     public int hashCode() {
98         return datastoreType.hashCode() * 31 + rootIdentifier.hashCode();
99     }
100
101     @Override
102     public boolean equals(final Object obj) {
103         return this == obj || obj instanceof DataTreeIdentifier<?> other && datastoreType == other.datastoreType
104             && rootIdentifier.equals(other.rootIdentifier);
105     }
106
107     @Override
108     public String toString() {
109         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
110     }
111
112     @java.io.Serial
113     Object writeReplace() {
114         return new DTIv1(this);
115     }
116 }