2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.binding;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import org.eclipse.jdt.annotation.NonNull;
16 * Utility base class for {@link OpaqueObject} implementations. This class provides baseline implementation of
17 * {@link #hashCode()} and {@link #equals(Object)} as specified by {@link OpaqueObject}.
19 * @param <T> Implemented OpaqueObject type
22 public abstract class AbstractOpaqueObject<T extends OpaqueObject<T>> implements OpaqueObject<T> {
24 public final int hashCode() {
25 return 31 * implementedInterface().hashCode() + valueHashCode();
29 public final boolean equals(final Object obj) {
30 return this == obj || obj instanceof OpaqueObject<?> other
31 && implementedInterface().equals(other.implementedInterface()) && valueEquals(other.getValue());
35 public final String toString() {
36 return addToStringAttributes(MoreObjects.toStringHelper(this)
37 .add("implementedInterface", implementedInterface())).toString();
40 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
41 return helper.add("value", getValue());
44 protected boolean valueEquals(final @NonNull OpaqueData<?> otherValue) {
45 return getValue().equals(otherValue);
48 protected int valueHashCode() {
49 return getValue().hashCode();