home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / misc / Ref.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.7 KB  |  106 lines

  1. /*
  2.  * @(#)Ref.java    1.18 96/03/30
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.misc;
  21.  
  22. /**
  23.  * A "Ref" is an indirect reference to an object that the garbage collector
  24.  * knows about.  An application should override the reconstitute() method with one
  25.  * that will construct the object based on information in the Ref, often by
  26.  * reading from a file.  The get() method retains a cache of the result of the last call to
  27.  * reconstitute() in the Ref.  When space gets tight, the garbage collector
  28.  * will clear old Ref cache entries when there are no other pointers to the
  29.  * object.  In normal usage, Ref will always be subclassed.  The subclass will add the 
  30.  * instance variables necessary for the reconstitute() method to work.  It will also add a 
  31.  * constructor to set them up, and write a version of reconstitute().
  32.  * @version     1.18, 03/30/96
  33.  */
  34.  
  35. public abstract class Ref {
  36.     static int        lruclock;
  37.     private Object  thing;
  38.     private long    priority;
  39.  
  40.     /**
  41.      * Returns a pointer to the object referenced by this Ref.  If the object
  42.      * has been thrown away by the garbage collector, it will be
  43.      * reconstituted. This method does everything necessary to ensure that the garbage
  44.      * collector throws things away in Least Recently Used(LRU) order.  Applications should 
  45.      * never override this method. The get() method effectively caches calls to
  46.      * reconstitute().
  47.      */
  48.     public Object get() {
  49.     Object p = thing;
  50.     if (p == null) {
  51.         /* synchronize if thing is null, but then check again
  52.            in case somebody else beat us to it */
  53.         synchronized (this) {
  54.         if (thing == null) {
  55.             p = reconstitute();
  56.             thing = p;
  57.         }
  58.         }
  59.     }
  60.     priority = ++lruclock;
  61.     return p;    /* Return local copy to avoid potential race */
  62.     }
  63.  
  64.     /**
  65.      * Returns a pointer to the object referenced by this Ref by 
  66.      * reconstituting it from some external source (such as a file).  This method should not 
  67.      * bother with caching since the method get() will deal with that.
  68.      * <p>
  69.      * In normal usage, Ref will always be subclassed.  The subclass will add
  70.      * the instance variables necessary for reconstitute() to work.  It will
  71.      * also add a constructor to set them up, and write a version of
  72.      * reconstitute().
  73.      */
  74.     public abstract Object reconstitute();
  75.  
  76.     /**
  77.      * Flushes the cached object.  Forces the next invocation of get() to
  78.      * invoke reconstitute().
  79.      */
  80.     public void flush() {
  81.     thing = null;
  82.     }
  83.    
  84.     /**
  85.      * Sets the thing to the specified object.
  86.      * @param thing the specified object
  87.      */
  88.     public void setThing(Object thing) {
  89.     this.thing = thing;
  90.     }
  91.  
  92.     /**
  93.      * Checks to see what object is being pointed at by this Ref and returns it.
  94.      */
  95.     public Object check() {
  96.     return thing;
  97.     }
  98.  
  99.     /**
  100.      * Constructs a new Ref.
  101.      */
  102.     public Ref() {
  103.     priority = ++lruclock;
  104.     }
  105. }
  106.