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.djutils.logger.CategoryLogger;
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-2025 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 object the new strong reference will refer to
38 */
39 public StrongReference(final T referent)
40 {
41 this.referent = referent;
42 }
43
44 @Override
45 public final T get()
46 {
47 return this.referent;
48 }
49
50 /**
51 * writes a serializable referent to stream.
52 * @param out the output stream
53 * @throws IOException on IOException
54 */
55 private synchronized void writeObject(final ObjectOutputStream out) throws IOException
56 {
57 out.writeObject(this.get());
58 }
59
60 /**
61 * reads a serializable method from stream.
62 * @param in java.io.ObjectInputStream; the input stream
63 * @throws IOException on IOException
64 * @throws ClassNotFoundException on ClassNotFoundException
65 */
66 private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
67 {
68 try
69 {
70 Field field = getClass().getDeclaredField("referent");
71 field.setAccessible(true);
72 field.set(this, in.readObject());
73 field.setAccessible(false);
74 }
75 catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException exception)
76 {
77 CategoryLogger.always().error(exception, "Error using ReadObject on StrongReference");
78 }
79 }
80 }