Fix eclipse/checkstyle warnings
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / EvenMoreObjects.java
1 /*
2  * Copyright (c) 2016 Red Hat, 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.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import java.util.function.BiFunction;
13
14 /**
15  * Utility helping to implement readable equals() methods.
16  *
17  * <p>Usage:
18  * <pre>
19  *{@literal @}Override
20  * public boolean equals(Object obj) {
21  *     return EvenMoreObjects.equalsHelper(this, obj,
22  *        (one, another) -&gt; Objects.equals(one.name, another.name) &amp;&amp; Objects.equals(one.age, another.age));
23  * }
24  * </pre>
25  *
26  * <p>See <a href="https://github.com/google/guava/issues/2521">Guava issue proposing contributing this</a>.
27  *
28  * @see MoreObjects
29  *
30  * @author Michael Vorburger, Red Hat
31  */
32 @Beta
33 public final class EvenMoreObjects {
34
35     @SuppressWarnings("unchecked")
36     public static <T> boolean equalsHelper(final T self, final Object other, final BooleanEqualsFunction<T> equals) {
37         if (other == self) {
38             return true;
39         }
40         if (other == null) {
41             return false;
42         }
43         if (self.getClass() != other.getClass()) {
44             return false;
45         }
46         return equals.apply(self, (T) other).booleanValue();
47     }
48
49     @FunctionalInterface
50     public interface BooleanEqualsFunction<T> extends BiFunction<T, T, Boolean> { }
51
52     private EvenMoreObjects() { }
53 }