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