Merge branch 'master' of ../controller
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / AbstractIdentifier.java
1 /*
2  * Copyright (c) 2016 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.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.Identifier;
15
16 /**
17  * An abstract {@link Identifier} backed by an immutable object. Subclasses have no control over {@link #hashCode()}
18  * and {@link #equals(Object)}, hence they should not add any fields.
19  *
20  * @author Robert Varga
21  *
22  * @param <T> Object type
23  */
24 public abstract class AbstractIdentifier<T> implements Identifier {
25     private static final long serialVersionUID = 1L;
26
27     private final @NonNull T value;
28
29     public AbstractIdentifier(final @NonNull T value) {
30         this.value = requireNonNull(value);
31     }
32
33     protected final @NonNull T getValue() {
34         return value;
35     }
36
37     @Override
38     public final int hashCode() {
39         return value.hashCode();
40     }
41
42     @Override
43     public final boolean equals(final Object obj) {
44         if (this == obj) {
45             return true;
46         }
47         if (obj == null) {
48             return false;
49         }
50
51         return getClass().equals(obj.getClass()) && value.equals(((AbstractIdentifier<?>)obj).value);
52     }
53
54     @Override
55     public final @NonNull String toString() {
56         return MoreObjects.toStringHelper(this).add("value", value).toString();
57     }
58 }