1 /* 2 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package jdk.internal.vm.compiler.collections; 26 27 /** 28 * Strategy for comparing two objects. Default predefined strategies are {@link #DEFAULT}, 29 * {@link #IDENTITY}, and {@link #IDENTITY_WITH_SYSTEM_HASHCODE}. 30 * 31 * @since 1.0 32 */ 33 public abstract class Equivalence { 34 35 /** 36 * Default equivalence calling {@link #equals(Object)} to check equality and {@link #hashCode()} 37 * for obtaining hash values. Do not change the logic of this class as it may be inlined in 38 * other places. 39 * 40 * @since 1.0 41 */ 42 public static final Equivalence DEFAULT = new Equivalence() { 43 44 @Override 45 public boolean equals(Object a, Object b) { 46 return a.equals(b); 47 } 48 49 @Override 50 public int hashCode(Object o) { 51 return o.hashCode(); 52 } 53 }; 54 55 /** 56 * Identity equivalence using {@code ==} to check equality and {@link #hashCode()} for obtaining 57 * hash values. Do not change the logic of this class as it may be inlined in other places. 58 * 59 * @since 1.0 60 */ 61 public static final Equivalence IDENTITY = new Equivalence() { 62 63 @Override 64 public boolean equals(Object a, Object b) { 65 return a == b; 66 } 67 68 @Override 69 public int hashCode(Object o) { 70 return o.hashCode(); 71 } 72 }; 73 74 /** 75 * Identity equivalence using {@code ==} to check equality and 76 * {@link System#identityHashCode(Object)} for obtaining hash values. Do not change the logic of 77 * this class as it may be inlined in other places. 78 * 79 * @since 1.0 80 */ 81 public static final Equivalence IDENTITY_WITH_SYSTEM_HASHCODE = new Equivalence() { 82 83 @Override 84 public boolean equals(Object a, Object b) { 85 return a == b; 86 } 87 88 @Override 89 public int hashCode(Object o) { 90 return System.identityHashCode(o); 91 } 92 }; 93 94 /** 95 * Subclass for creating custom equivalence definitions. 96 * 97 * @since 1.0 98 */ 99 protected Equivalence() { 100 } 101 102 /** 103 * Returns {@code true} if the non-{@code null} arguments are equal to each other and 104 * {@code false} otherwise. 105 * 106 * @since 1.0 107 */ 108 public abstract boolean equals(Object a, Object b); 109 110 /** 111 * Returns the hash code of a non-{@code null} argument {@code o}. 112 * 113 * @since 1.0 114 */ 115 public abstract int hashCode(Object o); 116 }