View Javadoc

1   /*
2    * @(#) Reference.java Dec 9, 2003 Copyright (c) 2002-2005 Delft University of
3    * Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
4    * This software is proprietary information of Delft University of Technology
5    * The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.event.ref;
8   
9   import java.io.IOException;
10  import java.io.ObjectOutputStream;
11  import java.io.Serializable;
12  
13  /***
14   * A Reference interface defining the indirect pointer access to an object.
15   * <p>
16   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
17   * University of Technology </a>, the Netherlands.
18   * <p>
19   * See for project information <a
20   * href="http://www.simulation.tudelft.nl/dsol/event">www.simulation.tudelft.nl/event
21   * </a> <br>
22   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
23   * General Public License (LGPL) </a>, no warranty
24   * 
25   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
26   * @version $Revision: 1.7 $ $Date: 2005/08/04 12:08:33 $
27   * @see java.lang.ref.Reference
28   * @since 1.5
29   * @param <T>
30   */
31  public abstract class Reference<T> implements Serializable
32  {
33      /***
34       * Returns this reference object's referent. If this reference object has
35       * been cleared, either by the program or by the garbage collector, then
36       * this method returns <code>null</code>.
37       * 
38       * @return The object to which this reference refers, or <code>null</code>
39       *         if this reference object has been cleared
40       */
41      public abstract T get();
42  
43      /***
44       * sets the value of the reference
45       * 
46       * @param value the value to set
47       */
48      protected abstract void set(final T value);
49  
50      /***
51       * writes a serializable method to stream
52       * 
53       * @param out the outputstream
54       * @throws IOException on IOException
55       */
56      private synchronized void writeObject(final ObjectOutputStream out)
57              throws IOException
58      {
59          out.writeObject(this.get());
60      }
61      
62      /***
63       * reads a serializable method from stream
64       * 
65       * @param in the inputstream
66       * @throws IOException on IOException
67       * @throws ClassNotFoundException on ClassNotFoundException
68       */
69      @SuppressWarnings("unchecked")
70      private synchronized void readObject(final java.io.ObjectInputStream in)
71              throws IOException, ClassNotFoundException
72      {
73          this.set((T) in.readObject());
74      }
75  }