Refactor DOM{Action,Rpc}Implementation
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMDataTreeIdentifier.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.dom.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.common.api.LogicalDatastorePath;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18
19 /**
20  * A DOM version of {@link LogicalDatastorePath}. Uses {@link YangInstanceIdentifier} for path addressing.
21  */
22 @NonNullByDefault
23 public final class DOMDataTreeIdentifier implements LogicalDatastorePath<DOMDataTreeIdentifier, YangInstanceIdentifier>,
24         Comparable<DOMDataTreeIdentifier> {
25     @java.io.Serial
26     private static final long serialVersionUID = 1L;
27
28     private final YangInstanceIdentifier rootIdentifier;
29     private final LogicalDatastoreType datastoreType;
30
31     /**
32      * Default constructor.
33      *
34      * @param datastore {@link LogicalDatastoreType} of this identifier
35      * @param path {@link YangInstanceIdentifier} path of this identifier
36      * @throws NullPointerException if any argument is {@code null}
37      * @deprecated Use {@link #of(LogicalDatastoreType, YangInstanceIdentifier)} instead
38      */
39     @Deprecated(since = "13.0.0", forRemoval = true)
40     public DOMDataTreeIdentifier(final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
41         datastoreType = requireNonNull(datastore);
42         rootIdentifier = requireNonNull(path);
43     }
44
45     /**
46      * Create a new {@link DOMDataTreeIdentifier} with specified datastore and path.
47      *
48      * @param datastore {@link LogicalDatastoreType} of this identifier
49      * @param path {@link YangInstanceIdentifier} path of this identifier
50      * @throws NullPointerException if any argument is {@code null}
51      */
52     public static DOMDataTreeIdentifier of(final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
53         return new DOMDataTreeIdentifier(datastore, path);
54     }
55
56     @Override
57     public LogicalDatastoreType datastore() {
58         return datastoreType;
59     }
60
61     /**
62      * Return the logical data store type.
63      *
64      * @return Logical data store type. Guaranteed to be non-null.
65      * @deprecated Use {@link #datastore()} instead
66      */
67     @Deprecated(since = "13.0.0", forRemoval = true)
68     public LogicalDatastoreType getDatastoreType() {
69         return datastore();
70     }
71
72     @Override
73     public YangInstanceIdentifier path() {
74         return rootIdentifier;
75     }
76
77     /**
78      * Return the {@link YangInstanceIdentifier} of the root node.
79      *
80      * @return Instance identifier corresponding to the root node.
81      * @deprecated Use {@link #path()} instead
82      */
83     @Deprecated(since = "13.0.0", forRemoval = true)
84     public YangInstanceIdentifier getRootIdentifier() {
85         return path();
86     }
87
88     public DOMDataTreeIdentifier toOptimized() {
89         final var opt = rootIdentifier.toOptimized();
90         return opt == rootIdentifier ? this : DOMDataTreeIdentifier.of(datastoreType, opt);
91     }
92
93     @Override
94     public int compareTo(final DOMDataTreeIdentifier domDataTreeIdentifier) {
95         int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
96         if (cmp != 0) {
97             return cmp;
98         }
99
100         final var myIter = rootIdentifier.getPathArguments().iterator();
101         final var otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
102
103         while (myIter.hasNext()) {
104             if (!otherIter.hasNext()) {
105                 return 1;
106             }
107
108             final var myPathArg = myIter.next();
109             final var otherPathArg = otherIter.next();
110             cmp = myPathArg.compareTo(otherPathArg);
111             if (cmp != 0) {
112                 return cmp;
113             }
114         }
115
116         return otherIter.hasNext() ? -1 : 0;
117     }
118
119     @Override
120     public int hashCode() {
121         return datastoreType.hashCode() * 31 + rootIdentifier.hashCode();
122     }
123
124     @Override
125     public boolean equals(final @Nullable Object obj) {
126         return this == obj || obj instanceof DOMDataTreeIdentifier other && datastoreType == other.datastoreType
127             && rootIdentifier.equals(other.rootIdentifier);
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
133     }
134
135     @java.io.Serial
136     Object writeReplace() {
137         return new DTIv1(this);
138     }
139 }