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-2024 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   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
12   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
13   */
14  public class TimeStamper implements Decoder
15  {
16      /** Result of next call to getResult. */
17      private String result = "";
18  
19      @Override
20      public String getResult()
21      {
22          String retVal = this.result;
23          this.result = "";
24          return retVal;
25      }
26  
27      @Override
28      public int getMaximumWidth()
29      {
30          return 14;
31      }
32  
33      @Override
34      public boolean append(final int address, final byte theByte) throws IOException
35      {
36          if (this.result.length() == 0)
37          {
38              long now = System.currentTimeMillis();
39              this.result = String.format("%10d.%03d ", now / 1000, now % 1000);
40          }
41          return false;
42      }
43  
44      @Override
45      public boolean ignoreForIdenticalOutputCheck()
46      {
47          return false;
48      }
49  
50      @Override
51      public String toString()
52      {
53          return "TimeStamper [result=" + this.result + "]";
54      }
55  
56  }