001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing; 018 019import com.google.common.annotations.GwtCompatible; 020import org.checkerframework.checker.nullness.qual.Nullable; 021 022/** 023 * An unhashable object to be used in testing as values in our collections. 024 * 025 * @author Regina O'Dell 026 */ 027@GwtCompatible 028public class UnhashableObject implements Comparable<UnhashableObject> { 029 private final int value; 030 031 public UnhashableObject(int value) { 032 this.value = value; 033 } 034 035 @Override 036 public boolean equals(@Nullable Object object) { 037 if (object instanceof UnhashableObject) { 038 UnhashableObject that = (UnhashableObject) object; 039 return this.value == that.value; 040 } 041 return false; 042 } 043 044 @Override 045 public int hashCode() { 046 throw new UnsupportedOperationException(); 047 } 048 049 // needed because otherwise Object.toString() calls hashCode() 050 @Override 051 public String toString() { 052 return "DontHashMe" + value; 053 } 054 055 @Override 056 public int compareTo(UnhashableObject o) { 057 return (this.value < o.value) ? -1 : (this.value > o.value) ? 1 : 0; 058 } 059}