Ref.java revision 0
1929N/A/*
1929N/A * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
1929N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1929N/A *
1929N/A * This code is free software; you can redistribute it and/or modify it
1929N/A * under the terms of the GNU General Public License version 2 only, as
1929N/A * published by the Free Software Foundation. Sun designates this
1929N/A * particular file as subject to the "Classpath" exception as provided
1929N/A * by Sun in the LICENSE file that accompanied this code.
1929N/A *
1929N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1929N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1929N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1929N/A * version 2 for more details (a copy is included in the LICENSE file that
1929N/A * accompanied this code).
1929N/A *
1929N/A * You should have received a copy of the GNU General Public License version
1929N/A * 2 along with this work; if not, write to the Free Software Foundation,
1929N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1929N/A *
1929N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1929N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1929N/A * have any questions.
1929N/A */
1929N/A
1929N/Apackage sun.misc;
1929N/Aimport java.lang.ref.SoftReference;
1929N/A
1929N/A
2399N/A/**
1929N/A * A "Ref" is an indirect reference to an object that the garbage collector
1929N/A * knows about. An application should override the reconstitute() method with one
1929N/A * that will construct the object based on information in the Ref, often by
1929N/A * reading from a file. The get() method retains a cache of the result of the last call to
1929N/A * reconstitute() in the Ref. When space gets tight, the garbage collector
1929N/A * will clear old Ref cache entries when there are no other pointers to the
1929N/A * object. In normal usage, Ref will always be subclassed. The subclass will add the
1929N/A * instance variables necessary for the reconstitute() method to work. It will also add a
1929N/A * constructor to set them up, and write a version of reconstitute().
1929N/A *
1929N/A * @deprecated This class has been replaced by
1929N/A * <code>java.util.SoftReference</code>.
1929N/A *
1929N/A * @see java.util.SoftReference
2399N/A *
1929N/A */
1929N/A@Deprecated
1929N/A
1929N/Apublic abstract class Ref {
1929N/A
1929N/A private SoftReference soft = null;
2399N/A
1929N/A /**
1929N/A * Returns a pointer to the object referenced by this Ref. If the object
1929N/A * has been thrown away by the garbage collector, it will be
1929N/A * reconstituted. This method does everything necessary to ensure that the garbage
1929N/A * collector throws things away in Least Recently Used(LRU) order. Applications should
1929N/A * never override this method. The get() method effectively caches calls to
1929N/A * reconstitute().
1929N/A */
1929N/A public synchronized Object get() {
2399N/A Object t = check();
1929N/A if (t == null) {
1929N/A t = reconstitute();
1929N/A setThing(t);
1929N/A }
1929N/A return t;
1929N/A }
1929N/A
1929N/A /**
1929N/A * Returns a pointer to the object referenced by this Ref by
1929N/A * reconstituting it from some external source (such as a file). This method should not
1929N/A * bother with caching since the method get() will deal with that.
1929N/A * <p>
1929N/A * In normal usage, Ref will always be subclassed. The subclass will add
1929N/A * the instance variables necessary for reconstitute() to work. It will
1929N/A * also add a constructor to set them up, and write a version of
1929N/A * reconstitute().
1929N/A */
1929N/A public abstract Object reconstitute();
1929N/A
1929N/A /**
1929N/A * Flushes the cached object. Forces the next invocation of get() to
1929N/A * invoke reconstitute().
1929N/A */
1929N/A public synchronized void flush() {
1929N/A SoftReference s = soft;
1929N/A if (s != null) s.clear();
1929N/A soft = null;
1929N/A }
/**
* Sets the thing to the specified object.
* @param thing the specified object
*/
public synchronized void setThing(Object thing) {
flush();
soft = new SoftReference(thing);
}
/**
* Checks to see what object is being pointed at by this Ref and returns it.
*/
public synchronized Object check() {
SoftReference s = soft;
if (s == null) return null;
return s.get();
}
/**
* Constructs a new Ref.
*/
public Ref() { }
/**
* Constructs a new Ref that initially points to thing.
*/
public Ref(Object thing) {
setThing(thing);
}
}