Bug 1459-2 - Re-organize mdsal-binding2-spec
[mdsal.git] / binding2 / mdsal-binding2-spec / src / main / java / org / opendaylight / mdsal / binding2 / spec / base / IdentifiableItem.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.binding2.spec.base;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13
14 /**
15  * An IdentifiableItem represents a object that is usually present in a collection and can be
16  * identified uniquely by a key. In YANG terms this would probably represent an item in a list.
17  *
18  * @param <I> An object that is identifiable by an identifier
19  * @param <T> The identifier of the object
20  */
21 @Beta
22 public final class IdentifiableItem<I extends TreeNode, T> extends TreeArgument<I> {
23     private final Class<I> type;
24     private final T key;
25
26     public IdentifiableItem(final Class<I> type, final T key) {
27         this.type = Preconditions.checkNotNull(type);
28         this.key = Preconditions.checkNotNull(key, "Key may not be null.");
29     }
30
31     @Override
32     public Class<I> getType() {
33         return type;
34     }
35
36     public T getKey() {
37         return this.key;
38     }
39
40     @Override
41     public boolean equals(final Object obj) {
42         return super.equals(obj) && key.equals(((IdentifiableItem<?, ?>) obj).getKey());
43     }
44
45     @Override
46     public int hashCode() {
47         return super.hashCode() * 31 + key.hashCode();
48     }
49
50     @Override
51     public String toString() {
52         return type.getName() + "[key=" + key + "]";
53     }
54 }