View Javadoc
1   package org.djutils.decoderdumper;
2   
3   import java.io.IOException;
4   
5   /**
6    * Decoder that returns a time stamp in milliseconds that applies to the time of dump of the first byte on the output line.
7    * <p>
8    * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
10   * <p>
11   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 3, 2019 <br>
12   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
14   */
15  public class TimeStamper implements Decoder
16  {
17      /** Result of next call to getResult. */
18      private String result = "";
19  
20      /** {@inheritDoc} */
21      @Override
22      public String getResult()
23      {
24          String retVal = this.result;
25          this.result = "";
26          return retVal;
27      }
28  
29      /** {@inheritDoc} */
30      @Override
31      public int getMaximumWidth()
32      {
33          return 14;
34      }
35  
36      /** {@inheritDoc} */
37      @Override
38      public boolean append(final int address, final byte theByte) throws IOException
39      {
40          if (this.result.length() == 0)
41          {
42              long now = System.currentTimeMillis();
43              this.result = String.format("%10d.%03d ", now / 1000, now % 1000);
44          }
45          return false;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public boolean ignoreForIdenticalOutputCheck()
51      {
52          return false;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public String toString()
58      {
59          return "TimeStamper [result=" + this.result + "]";
60      }
61  
62  }