ebd5cd258b0ed20ddcbd806995ac9e5e232aa092
[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 org.eclipse.jdt.annotation.NonNull;
12
13 /**
14  * An {@link InstanceIdentifier}, which has a list key attached at its last path element.
15  *
16  * @param <T> Target data type
17  * @param <K> Target key type
18  */
19 public final class KeyedInstanceIdentifier<T extends KeyAware<K> & DataObject, K extends Key<T>>
20         extends InstanceIdentifier<T> {
21     @java.io.Serial
22     private static final long serialVersionUID = 2L;
23
24     private final @NonNull KeyStep<K, T> lastStep;
25
26     KeyedInstanceIdentifier(final KeyStep<K, T> lastStep, final Iterable<DataObjectStep<?>> pathArguments,
27             final boolean wildcarded, final int hash) {
28         super(lastStep.type(), pathArguments, wildcarded, hash);
29         this.lastStep = lastStep;
30     }
31
32     @NonNull KeyStep<K, T> lastStep() {
33         return lastStep;
34     }
35
36     /**
37      * Return the key attached to this identifier. This method is equivalent to calling
38      * {@link InstanceIdentifier#keyOf(InstanceIdentifier)}.
39      *
40      * @return Key associated with this instance identifier.
41      */
42     public @NonNull K getKey() {
43         return lastStep.key();
44     }
45
46     @Override
47     public KeyedBuilder<T, K> builder() {
48         return new KeyedBuilder<>(this);
49     }
50
51     @Override
52     boolean keyEquals(final InstanceIdentifier<?> other) {
53         return getKey().equals(((KeyedInstanceIdentifier<?, ?>) other).getKey());
54     }
55
56     @Override
57     Object writeReplace() throws ObjectStreamException {
58         return new KIIv4<>(this);
59     }
60 }