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