View Javadoc
1   package org.djutils.event.ref;
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 StrongReference class represents a normal pointer relation to a reference. This class is created to complete the
12   * java.lang.ref package. This class ensures that references can be used without casting to either an object or a reference.
13   * Strong references are not created to be cleaned by the garbage collector. Since they represent normal pointer relations, they
14   * are the only ones which might be serialized. This class therefore implements <code>java.io.Serializable</code>
15   * <p>
16   * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
18   * distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
20   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
21   * https://simulation.tudelft.nl/dsol/manual</a>.
22   * </p>
23   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
24   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @param <T> the type of the reference
26   */
27  public class StrongReference<T extends Serializable> extends Reference<T>
28  {
29      /** The default serial version UID for serializable classes. */
30      private static final long serialVersionUID = 20191230L;
31  
32      /** the referent. */
33      private final transient T referent;
34  
35      /**
36       * Creates a new strong reference that refers to the given object. The new reference is not registered with any queue.
37       * @param referent T; object the new strong reference will refer to
38       */
39      public StrongReference(final T referent)
40      {
41          this.referent = referent;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public final T get()
47      {
48          return this.referent;
49      }
50  
51      /**
52       * writes a serializable referent to stream.
53       * @param out ObjectOutputStream; the output stream
54       * @throws IOException on IOException
55       */
56      private synchronized void writeObject(final ObjectOutputStream out) throws IOException
57      {
58          out.writeObject(this.get());
59      }
60  
61      /**
62       * reads a serializable method from stream.
63       * @param in java.io.ObjectInputStream; the input stream
64       * @throws IOException on IOException
65       * @throws ClassNotFoundException on ClassNotFoundException
66       */
67      private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
68      {
69          try
70          {
71              Field field = getClass().getDeclaredField("referent");
72              field.setAccessible(true);
73              field.set(this, in.readObject());
74              field.setAccessible(false);
75          }
76          catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException exception)
77          {
78              Logger.error(exception, "Error using ReadObject on StrongReference");
79          }
80      }
81  }