2 * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.opendaylight.yangtools.triemap;
18 import java.io.Serializable;
19 import javax.annotation.Nonnull;
22 * Internal equivalence class, similar to {@link com.google.common.base.Equivalence}, but explicitly not handling
23 * nulls. We use equivalence only for keys, which are guaranteed to be non-null.
25 * @author Robert Varga
27 abstract class Equivalence<T> implements Serializable {
28 private static final long serialVersionUID = 1L;
30 private static final class Equals extends Equivalence<Object> {
31 private static final long serialVersionUID = 1L;
33 static final Equals INSTANCE = new Equals();
36 boolean equivalent(final Object a, final Object b) {
41 Object readResolve() {
46 private static final class Identity extends Equivalence<Object> {
47 private static final long serialVersionUID = 1L;
49 static final Identity INSTANCE = new Identity();
52 boolean equivalent(final Object a, final Object b) {
57 Object readResolve() {
62 static Equivalence<Object> equals() {
63 return Equals.INSTANCE;
66 static Equivalence<Object> identity() {
67 return Identity.INSTANCE;
70 final int hash(@Nonnull final T t) {
73 // This function ensures that hashCodes that differ only by
74 // constant multiples at each bit position have a bounded
75 // number of collisions (approximately 8 at default load factor).
76 h ^= (h >>> 20) ^ (h >>> 12);
77 h ^= (h >>> 7) ^ (h >>> 4);
81 abstract boolean equivalent(@Nonnull T a, @Nonnull T b);
83 abstract Object readResolve();