1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/lang/ref/PhantomReference.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,83 @@
1.4 +/*
1.5 + * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang.ref;
1.30 +
1.31 +
1.32 +/**
1.33 + * Phantom reference objects, which are enqueued after the collector
1.34 + * determines that their referents may otherwise be reclaimed. Phantom
1.35 + * references are most often used for scheduling pre-mortem cleanup actions in
1.36 + * a more flexible way than is possible with the Java finalization mechanism.
1.37 + *
1.38 + * <p> If the garbage collector determines at a certain point in time that the
1.39 + * referent of a phantom reference is <a
1.40 + * href="package-summary.html#reachability">phantom reachable</a>, then at that
1.41 + * time or at some later time it will enqueue the reference.
1.42 + *
1.43 + * <p> In order to ensure that a reclaimable object remains so, the referent of
1.44 + * a phantom reference may not be retrieved: The <code>get</code> method of a
1.45 + * phantom reference always returns <code>null</code>.
1.46 + *
1.47 + * <p> Unlike soft and weak references, phantom references are not
1.48 + * automatically cleared by the garbage collector as they are enqueued. An
1.49 + * object that is reachable via phantom references will remain so until all
1.50 + * such references are cleared or themselves become unreachable.
1.51 + *
1.52 + * @author Mark Reinhold
1.53 + * @since 1.2
1.54 + */
1.55 +
1.56 +public class PhantomReference<T> extends Reference<T> {
1.57 +
1.58 + /**
1.59 + * Returns this reference object's referent. Because the referent of a
1.60 + * phantom reference is always inaccessible, this method always returns
1.61 + * <code>null</code>.
1.62 + *
1.63 + * @return <code>null</code>
1.64 + */
1.65 + public T get() {
1.66 + return null;
1.67 + }
1.68 +
1.69 + /**
1.70 + * Creates a new phantom reference that refers to the given object and
1.71 + * is registered with the given queue.
1.72 + *
1.73 + * <p> It is possible to create a phantom reference with a <tt>null</tt>
1.74 + * queue, but such a reference is completely useless: Its <tt>get</tt>
1.75 + * method will always return null and, since it does not have a queue, it
1.76 + * will never be enqueued.
1.77 + *
1.78 + * @param referent the object the new phantom reference will refer to
1.79 + * @param q the queue with which the reference is to be registered,
1.80 + * or <tt>null</tt> if registration is not required
1.81 + */
1.82 + public PhantomReference(T referent, ReferenceQueue<? super T> q) {
1.83 + super(referent, q);
1.84 + }
1.85 +
1.86 +}