Switch (DOM)DataTreeIdentifier to serialization proxy
[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.LogicalDatastoreType;
16 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18
19 /**
20  * A unique identifier for a particular subtree. It is composed of the logical data store type and the instance
21  * identifier of the root node.
22  */
23 @NonNullByDefault
24 public final class DOMDataTreeIdentifier implements HierarchicalIdentifier<DOMDataTreeIdentifier>,
25         Comparable<DOMDataTreeIdentifier> {
26     @java.io.Serial
27     private static final long serialVersionUID = 1L;
28
29     private final YangInstanceIdentifier rootIdentifier;
30     private final LogicalDatastoreType datastoreType;
31
32     public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType,
33             final YangInstanceIdentifier rootIdentifier) {
34         this.datastoreType = requireNonNull(datastoreType);
35         this.rootIdentifier = requireNonNull(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 LogicalDatastoreType getDatastoreType() {
44         return datastoreType;
45     }
46
47     /**
48      * Return the {@link YangInstanceIdentifier} of the root node.
49      *
50      * @return Instance identifier corresponding to the root node.
51      */
52     public YangInstanceIdentifier getRootIdentifier() {
53         return rootIdentifier;
54     }
55
56     @Override
57     public boolean contains(final DOMDataTreeIdentifier other) {
58         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
59     }
60
61     public DOMDataTreeIdentifier toOptimized() {
62         final var opt = rootIdentifier.toOptimized();
63         return opt == rootIdentifier ? this : new DOMDataTreeIdentifier(datastoreType, opt);
64     }
65
66     @Override
67     public int hashCode() {
68         return datastoreType.hashCode() * 31 + rootIdentifier.hashCode();
69     }
70
71     @Override
72     public boolean equals(final @Nullable Object obj) {
73         return this == obj || obj instanceof DOMDataTreeIdentifier other && datastoreType == other.datastoreType
74             && rootIdentifier.equals(other.rootIdentifier);
75     }
76
77     @Override
78     public int compareTo(final DOMDataTreeIdentifier domDataTreeIdentifier) {
79         int cmp = datastoreType.compareTo(domDataTreeIdentifier.datastoreType);
80         if (cmp != 0) {
81             return cmp;
82         }
83
84         final var myIter = rootIdentifier.getPathArguments().iterator();
85         final var otherIter = domDataTreeIdentifier.rootIdentifier.getPathArguments().iterator();
86
87         while (myIter.hasNext()) {
88             if (!otherIter.hasNext()) {
89                 return 1;
90             }
91
92             final var myPathArg = myIter.next();
93             final var otherPathArg = otherIter.next();
94             cmp = myPathArg.compareTo(otherPathArg);
95             if (cmp != 0) {
96                 return cmp;
97             }
98         }
99
100         return otherIter.hasNext() ? -1 : 0;
101     }
102
103     @Override
104     public String toString() {
105         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
106     }
107
108     @java.io.Serial
109     Object writeReplace() {
110         return new DTIv1(this);
111     }
112 }