1 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file 2 // for details. All rights reserved. Use of this source code is governed by a 3 // BSD-style license that can be found in the LICENSE file. 4 package com.android.tools.r8.graph; 5 6 /** 7 * DexItems of this kind have to be canonicalized for the whole application. 8 */ 9 public abstract class CanonicalizedDexItem extends DexItem { 10 11 private static final int NOT_COMPUTED_HASH_VALUE = -1; 12 private static final int SENTINEL_HASH_VALUE = 0; 13 private volatile int hash = NOT_COMPUTED_HASH_VALUE; 14 computeHashCode()15 protected abstract int computeHashCode(); 16 computeEquals(Object other)17 protected abstract boolean computeEquals(Object other); 18 19 @Override hashCode()20 public final int hashCode() { 21 int cache = hash; 22 if (cache == NOT_COMPUTED_HASH_VALUE) { 23 cache = computeHashCode(); 24 if (cache == NOT_COMPUTED_HASH_VALUE) { 25 cache = SENTINEL_HASH_VALUE; 26 } 27 hash = cache; 28 } 29 return cache; 30 } 31 32 @Override flushCachedValues()33 public void flushCachedValues() { 34 super.flushCachedValues(); 35 hash = NOT_COMPUTED_HASH_VALUE; 36 } 37 38 @Override equals(Object other)39 public final boolean equals(Object other) { 40 return this == other || computeEquals(other); 41 } 42 } 43