rt/emul/compact/src/main/java/java/lang/ref/SoftReference.java
changeset 772 d382dacfd73f
parent 601 5198affdb915
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/ref/SoftReference.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,118 @@
     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 + * Soft reference objects, which are cleared at the discretion of the garbage
    1.34 + * collector in response to memory demand.  Soft references are most often used
    1.35 + * to implement memory-sensitive caches.
    1.36 + *
    1.37 + * <p> Suppose that the garbage collector determines at a certain point in time
    1.38 + * that an object is <a href="package-summary.html#reachability">softly
    1.39 + * reachable</a>.  At that time it may choose to clear atomically all soft
    1.40 + * references to that object and all soft references to any other
    1.41 + * softly-reachable objects from which that object is reachable through a chain
    1.42 + * of strong references.  At the same time or at some later time it will
    1.43 + * enqueue those newly-cleared soft references that are registered with
    1.44 + * reference queues.
    1.45 + *
    1.46 + * <p> All soft references to softly-reachable objects are guaranteed to have
    1.47 + * been cleared before the virtual machine throws an
    1.48 + * <code>OutOfMemoryError</code>.  Otherwise no constraints are placed upon the
    1.49 + * time at which a soft reference will be cleared or the order in which a set
    1.50 + * of such references to different objects will be cleared.  Virtual machine
    1.51 + * implementations are, however, encouraged to bias against clearing
    1.52 + * recently-created or recently-used soft references.
    1.53 + *
    1.54 + * <p> Direct instances of this class may be used to implement simple caches;
    1.55 + * this class or derived subclasses may also be used in larger data structures
    1.56 + * to implement more sophisticated caches.  As long as the referent of a soft
    1.57 + * reference is strongly reachable, that is, is actually in use, the soft
    1.58 + * reference will not be cleared.  Thus a sophisticated cache can, for example,
    1.59 + * prevent its most recently used entries from being discarded by keeping
    1.60 + * strong referents to those entries, leaving the remaining entries to be
    1.61 + * discarded at the discretion of the garbage collector.
    1.62 + *
    1.63 + * @author   Mark Reinhold
    1.64 + * @since    1.2
    1.65 + */
    1.66 +
    1.67 +public class SoftReference<T> extends Reference<T> {
    1.68 +
    1.69 +    /**
    1.70 +     * Timestamp clock, updated by the garbage collector
    1.71 +     */
    1.72 +    static private long clock;
    1.73 +
    1.74 +    /**
    1.75 +     * Timestamp updated by each invocation of the get method.  The VM may use
    1.76 +     * this field when selecting soft references to be cleared, but it is not
    1.77 +     * required to do so.
    1.78 +     */
    1.79 +    private long timestamp;
    1.80 +
    1.81 +    /**
    1.82 +     * Creates a new soft reference that refers to the given object.  The new
    1.83 +     * reference is not registered with any queue.
    1.84 +     *
    1.85 +     * @param referent object the new soft reference will refer to
    1.86 +     */
    1.87 +    public SoftReference(T referent) {
    1.88 +        super(referent);
    1.89 +        this.timestamp = clock;
    1.90 +    }
    1.91 +
    1.92 +    /**
    1.93 +     * Creates a new soft reference that refers to the given object and is
    1.94 +     * registered with the given queue.
    1.95 +     *
    1.96 +     * @param referent object the new soft reference will refer to
    1.97 +     * @param q the queue with which the reference is to be registered,
    1.98 +     *          or <tt>null</tt> if registration is not required
    1.99 +     *
   1.100 +     */
   1.101 +    public SoftReference(T referent, ReferenceQueue<? super T> q) {
   1.102 +        super(referent, q);
   1.103 +        this.timestamp = clock;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Returns this reference object's referent.  If this reference object has
   1.108 +     * been cleared, either by the program or by the garbage collector, then
   1.109 +     * this method returns <code>null</code>.
   1.110 +     *
   1.111 +     * @return   The object to which this reference refers, or
   1.112 +     *           <code>null</code> if this reference object has been cleared
   1.113 +     */
   1.114 +    public T get() {
   1.115 +        T o = super.get();
   1.116 +        if (o != null && this.timestamp != clock)
   1.117 +            this.timestamp = clock;
   1.118 +        return o;
   1.119 +    }
   1.120 +
   1.121 +}