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