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