View Javadoc
1   package org.djutils.event.reference;
2   
3   import java.io.IOException;
4   import java.io.ObjectOutputStream;
5   import java.io.Serializable;
6   import java.lang.reflect.Field;
7   
8   import org.pmw.tinylog.Logger;
9   
10  /**
11   * A WeakReference. The WeakReference extends the <code>java.lang.ref.WeakReference</code> and besides implementing the
12   * Reference interface no changes are defined.
13   * <p>
14   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
16   * distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
18   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
19   * https://simulation.tudelft.nl/dsol/manual</a>.
20   * </p>
21   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
22   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @param <T> the type of the reference
24   */
25  public class WeakReference<T extends Serializable> extends Reference<T>
26  {
27      /** The default serial version UID for serializable classes. */
28      private static final long serialVersionUID = 20140830L;
29  
30      /** the wrapped reference to the referent. */
31      private final transient java.lang.ref.WeakReference<T> referent;
32  
33      /**
34       * Creates a new weak reference that refers to the given object. The new reference is not registered with any queue.
35       * @param referent T; object the new weak reference will refer to
36       */
37      public WeakReference(final T referent)
38      {
39          this.referent = new java.lang.ref.WeakReference<T>(referent);
40      }
41  
42      @Override
43      public final T get()
44      {
45          return this.referent.get();
46      }
47  
48      /**
49       * Write a serializable method to a stream.
50       * @param out ObjectOutputStream; the output stream
51       * @throws IOException on IOException
52       */
53      private synchronized void writeObject(final ObjectOutputStream out) throws IOException
54      {
55          out.writeObject(this.get());
56      }
57  
58      /**
59       * Read a serializable method from a stream.
60       * @param in java.io.ObjectInputStream; the input stream
61       * @throws IOException on IOException
62       * @throws ClassNotFoundException on ClassNotFoundException
63       */
64      @SuppressWarnings("unchecked")
65      private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
66      {
67          try
68          {
69              Field field = getClass().getDeclaredField("referent");
70              field.setAccessible(true);
71              field.set(this, new java.lang.ref.WeakReference<T>((T) in.readObject()));
72              field.setAccessible(false);
73          }
74          catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException exception)
75          {
76              Logger.error(exception, "Error using ReadObject on StrongReference");
77          }
78      }
79  }