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-2023 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    * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 3, 2019 <br>
10   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
11   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
12   */
13  public class HexAddressDecoder implements Decoder
14  {
15      /** Round all printed addresses down to a multiple of this value. */
16      private final int roundToMultiple;
17  
18      /**
19       * Construct a new HexAddressDecoder.
20       * @param roundToMultiple int; if &gt; 1 round addresses down to the nearest (lower) multiple of this value and the append
21       *            method will return true when the last byte before such a multiple is added.
22       */
23      public HexAddressDecoder(final int roundToMultiple)
24      {
25          this.roundToMultiple = roundToMultiple > 0 ? roundToMultiple : 1;
26      }
27  
28      /** Result returned by getResult. */
29      private String result = "";
30  
31      /** {@inheritDoc} */
32      @Override
33      public String getResult()
34      {
35          String retVal = this.result;
36          this.result = "";
37          return retVal;
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public int getMaximumWidth()
43      {
44          return 8;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public boolean append(final int address, final byte theByte)
50      {
51          this.result = String.format("%08x", address / this.roundToMultiple * this.roundToMultiple);
52          return this.roundToMultiple > 1 && address % this.roundToMultiple == this.roundToMultiple - 1;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public boolean ignoreForIdenticalOutputCheck()
58      {
59          return true;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public String toString()
65      {
66          return "HexAddressDecoder [result=" + this.result + "]";
67      }
68  
69  }