ecbc48aeb63f8c20a9a15bfc2a3894dfb6cd28f4
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / KeyedInstanceIdentifier.java
1 /*
2  * Copyright (c) 2014 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.yangtools.yang.binding;
9
10 import java.io.ObjectStreamException;
11 import java.io.Serial;
12 import org.eclipse.jdt.annotation.NonNull;
13
14 /**
15  * An {@link InstanceIdentifier}, which has a list key attached at its last path element.
16  *
17  * @param <T> Target data type
18  * @param <K> Target key type
19  */
20 public class KeyedInstanceIdentifier<T extends KeyAware<K> & DataObject, K extends Key<T>>
21         extends InstanceIdentifier<T> {
22     @Serial
23     private static final long serialVersionUID = 2L;
24
25     private final K key;
26
27     KeyedInstanceIdentifier(final Class<@NonNull T> type, final Iterable<PathArgument> pathArguments,
28             final boolean wildcarded, final int hash, final K key) {
29         super(type, pathArguments, wildcarded, hash);
30         this.key = key;
31     }
32
33     /**
34      * Return the key attached to this identifier. This method is equivalent to calling
35      * {@link InstanceIdentifier#keyOf(InstanceIdentifier)}.
36      *
37      * @return Key associated with this instance identifier.
38      */
39     public final K getKey() {
40         return key;
41     }
42
43     @Override
44     public final KeyedBuilder<T, K> builder() {
45         return new KeyedBuilder<>(this);
46     }
47
48     @Override
49     protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
50         final KeyedInstanceIdentifier<?, ?> kii = (KeyedInstanceIdentifier<?, ?>) other;
51
52         /*
53          * We could do an equals() here, but that may actually be expensive.
54          * equals() in superclass falls back to a full compare, which will
55          * end up running that equals anyway, so do not bother here.
56          */
57         return key == null != (kii.key == null);
58     }
59
60     @Serial
61     private Object writeReplace() throws ObjectStreamException {
62         return new KeyedInstanceIdentifierV2<>(this);
63     }
64 }