1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
2090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copyright (C) 2007 Google Inc.
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.base;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.lang.ref.PhantomReference;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Phantom reference with a {@code finalizeReferent()} method which a
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * background thread invokes after the garbage collector reclaims the
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * referent. This is a simpler alternative to using a {@link
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * java.lang.ref.ReferenceQueue}.
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Unlike a normal phantom reference, this reference will be cleared
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * automatically.
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Bob Lee
31bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library)
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic abstract class FinalizablePhantomReference<T>
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    extends PhantomReference<T> implements FinalizableReference {
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Constructs a new finalizable phantom reference.
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param referent to phantom reference
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param queue that should finalize the referent
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  protected FinalizablePhantomReference(T referent,
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      FinalizableReferenceQueue queue) {
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    super(referent, queue.queue);
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    queue.cleanUp();
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
48