1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.base; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import com.google.common.annotations.J2ktIncompatible; 19 import java.lang.ref.ReferenceQueue; 20 import java.lang.ref.WeakReference; 21 import javax.annotation.CheckForNull; 22 23 /** 24 * Weak reference with a {@code finalizeReferent()} method which a background thread invokes after 25 * the garbage collector reclaims the referent. This is a simpler alternative to using a {@link 26 * ReferenceQueue}. 27 * 28 * @author Bob Lee 29 * @since 2.0 30 */ 31 @J2ktIncompatible 32 @GwtIncompatible 33 @ElementTypesAreNonnullByDefault 34 public abstract class FinalizableWeakReference<T> extends WeakReference<T> 35 implements FinalizableReference { 36 /** 37 * Constructs a new finalizable weak reference. 38 * 39 * @param referent to weakly reference 40 * @param queue that should finalize the referent 41 */ FinalizableWeakReference(@heckForNull T referent, FinalizableReferenceQueue queue)42 protected FinalizableWeakReference(@CheckForNull T referent, FinalizableReferenceQueue queue) { 43 super(referent, queue.queue); 44 queue.cleanUp(); 45 } 46 } 47