View Javadoc
1   package org.opentrafficsim.base;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Time;
6   
7   /**
8    * An object with a time stamp, where the object is of a specific class.
9    * <p>
10   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12   * </p>
13   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
15   * @param <C> the time stamped object class.
16   */
17  public class TimeStampedObject<C> implements Serializable
18  {
19      /** */
20      private static final long serialVersionUID = 20160129L;
21  
22      /** The object. */
23      private final C object;
24  
25      /** The time stamp. */
26      private final Time timestamp;
27  
28      /**
29       * Construct a new TimeStampedObject.
30       * @param object C; the object.
31       * @param timestamp Time; the time stamp.
32       */
33      public TimeStampedObject(final C object, final Time timestamp)
34      {
35          this.object = object;
36          this.timestamp = timestamp;
37      }
38  
39      /**
40       * Retrieve the object.
41       * @return C; the object
42       */
43      public final C getObject()
44      {
45          return this.object;
46      }
47  
48      /**
49       * Retrieve the time stamp.
50       * @return Time; the time stamp
51       */
52      public final Time getTimestamp()
53      {
54          return this.timestamp;
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public final String toString()
60      {
61          return "TimeStampedObject [object=" + this.object + ", timestamp=" + this.timestamp + "]";
62      }
63  
64      @Override
65      public int hashCode()
66      {
67          final int prime = 31;
68          int result = 1;
69          result = prime * result + ((object == null) ? 0 : object.hashCode());
70          result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode());
71          return result;
72      }
73  
74      @Override
75      public boolean equals(Object obj)
76      {
77          if (this == obj)
78              return true;
79          if (obj == null)
80              return false;
81          if (getClass() != obj.getClass())
82              return false;
83          TimeStampedObject other = (TimeStampedObject) obj;
84          if (object == null)
85          {
86              if (other.object != null)
87                  return false;
88          }
89          else if (!object.equals(other.object))
90              return false;
91          if (timestamp == null)
92          {
93              if (other.timestamp != null)
94                  return false;
95          }
96          else if (!timestamp.equals(other.timestamp))
97              return false;
98          return true;
99      }
100 
101 }