001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.functors; 018 019import java.io.Serializable; 020import java.util.Objects; 021 022import org.apache.commons.collections4.Equator; 023 024/** 025 * Default {@link Equator} implementation. 026 * 027 * @param <T> the types of object this {@link Equator} can evaluate. 028 * @since 4.0 029 */ 030public class DefaultEquator<T> implements Equator<T>, Serializable { 031 032 /** Serial version UID */ 033 private static final long serialVersionUID = 825802648423525485L; 034 035 /** Static instance */ 036 @SuppressWarnings("rawtypes") // the static instance works for all types 037 public static final DefaultEquator INSTANCE = new DefaultEquator<>(); 038 039 /** 040 * Hashcode used for {@code null} objects. 041 */ 042 public static final int HASHCODE_NULL = -1; 043 044 /** 045 * Factory returning the typed singleton instance. 046 * 047 * @param <T> the object type 048 * @return the singleton instance 049 */ 050 public static <T> DefaultEquator<T> defaultEquator() { 051 return INSTANCE; 052 } 053 054 /** 055 * Restricted constructor. 056 */ 057 private DefaultEquator() { 058 } 059 060 /** 061 * {@inheritDoc} Delegates to {@link Objects#equals(Object, Object)}. 062 */ 063 @Override 064 public boolean equate(final T o1, final T o2) { 065 return Objects.equals(o1, o2); 066 } 067 068 /** 069 * {@inheritDoc} 070 * 071 * @return {@code o.hashCode()} if {@code o} is non- 072 * {@code null}, else {@link #HASHCODE_NULL}. 073 */ 074 @Override 075 public int hash(final T o) { 076 return o == null ? HASHCODE_NULL : o.hashCode(); 077 } 078 079 private Object readResolve() { 080 return INSTANCE; 081 } 082 083}