View Javadoc
1   package org.djutils.decoderdumper;
2   
3   /**
4    * Keep track of the address of the decoder-dumper and call flushLine when the last possible address of a line is received.
5    * <p>
6    * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
8    * </p>
9    * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
10   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
11   */
12  public class HexAddressDecoder implements Decoder
13  {
14      /** Round all printed addresses down to a multiple of this value. */
15      private final int roundToMultiple;
16  
17      /**
18       * Construct a new HexAddressDecoder.
19       * @param roundToMultiple int; if &gt; 1 round addresses down to the nearest (lower) multiple of this value and the append
20       *            method will return true when the last byte before such a multiple is added.
21       */
22      public HexAddressDecoder(final int roundToMultiple)
23      {
24          this.roundToMultiple = roundToMultiple > 0 ? roundToMultiple : 1;
25      }
26  
27      /** Result returned by getResult. */
28      private String result = "";
29  
30      @Override
31      public String getResult()
32      {
33          String retVal = this.result;
34          this.result = "";
35          return retVal;
36      }
37  
38      @Override
39      public int getMaximumWidth()
40      {
41          return 8;
42      }
43  
44      @Override
45      public boolean append(final int address, final byte theByte)
46      {
47          this.result = String.format("%08x", address / this.roundToMultiple * this.roundToMultiple);
48          return this.roundToMultiple > 1 && address % this.roundToMultiple == this.roundToMultiple - 1;
49      }
50  
51      @Override
52      public boolean ignoreForIdenticalOutputCheck()
53      {
54          return true;
55      }
56  
57      @Override
58      public String toString()
59      {
60          return "HexAddressDecoder [result=" + this.result + "]";
61      }
62  
63  }