Fix nullness errors reported by Eclipse
[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 class KeyedInstanceIdentifier<T extends Identifiable<K> & DataObject, K extends Identifier<T>>
20         extends InstanceIdentifier<T> {
21     private static final long serialVersionUID = 2L;
22     private final K key;
23
24     KeyedInstanceIdentifier(final Class<@NonNull T> type, final Iterable<PathArgument> pathArguments,
25             final boolean wildcarded, final int hash, final K key) {
26         super(type, pathArguments, wildcarded, hash);
27         this.key = key;
28     }
29
30     /**
31      * Return the key attached to this identifier. This method is equivalent to calling
32      * {@link InstanceIdentifier#keyOf(InstanceIdentifier)}.
33      *
34      * @return Key associated with this instance identifier.
35      */
36     public final K getKey() {
37         return key;
38     }
39
40     @Override
41     public final InstanceIdentifierBuilder<T> builder() {
42         return new InstanceIdentifierBuilderImpl<>(IdentifiableItem.of(getTargetType(), key), pathArguments,
43                 hashCode(), isWildcarded());
44     }
45
46     @Override
47     protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
48         final KeyedInstanceIdentifier<?, ?> kii = (KeyedInstanceIdentifier<?, ?>) other;
49
50         /*
51          * We could do an equals() here, but that may actually be expensive.
52          * equals() in superclass falls back to a full compare, which will
53          * end up running that equals anyway, so do not bother here.
54          */
55         return key == null != (kii.key == null);
56     }
57
58     private Object writeReplace() throws ObjectStreamException {
59         return new KeyedInstanceIdentifierV2<>(this);
60     }
61 }