Binding v2 runtime - adapters - impl - operations invoker
[mdsal.git] / binding2 / mdsal-binding2-spec / src / main / java / org / opendaylight / mdsal / binding / javav2 / spec / base / KeyedInstanceIdentifier.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.mdsal.binding.javav2.spec.base;
10
11 import com.google.common.annotations.Beta;
12
13 /**
14  * An {@link InstanceIdentifier}, which has a list key attached at its last path
15  * element.
16  *
17  * @param <T> Target data type
18  * @param <K> Target key type
19  */
20
21 @Beta
22 public class KeyedInstanceIdentifier<T extends TreeNode, K> extends InstanceIdentifier<T> {
23     private static final long serialVersionUID = 1L;
24     private final K key;
25
26     KeyedInstanceIdentifier(final Class<T> type, final Iterable<TreeArgument> pathArguments, final boolean wildcarded, final int hash, final K key) {
27         super(type, pathArguments, wildcarded, hash);
28         this.key = key;
29     }
30
31     /**
32      * Return the key attached to this identifier. This method is equivalent to
33      * calling {@link InstanceIdentifier#keyOf(InstanceIdentifier)}.
34      *
35      * @return Key associated with this instance identifier.
36      */
37     public final K getKey() {
38         return key;
39     }
40
41     @Override
42     public final InstanceIdentifierBuilder<T> builder() {
43         return new InstanceIdentifierBuilderImpl<T>(new IdentifiableItem<T, K>(getTargetType(), key), pathArguments,
44                 hashCode(), isWildcarded());
45     }
46
47     @Override
48     protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
49         final KeyedInstanceIdentifier<?, ?> kii = (KeyedInstanceIdentifier<?, ?>) other;
50
51         /*
52          * We could do an equals() here, but that may actually be expensive.
53          * equals() in superclass falls back to a full compare, which will
54          * end up running that equals anyway, so do not bother here.
55          */
56         return (key == null) != (kii.key == null);
57     }
58 }