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-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="https://opentrafficsim.org/node/13">OpenTrafficSim 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   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
13   */
14  public class HexAddressDecoder implements Decoder
15  {
16      /** Round all printed addresses down to a multiple of this value. */
17      private final int roundToMultiple;
18  
19      /**
20       * Construct a new HexAddressDecoder.
21       * @param roundToMultiple int; if &gt; 1 round addresses down to the nearest (lower) multiple of this value and the append
22       *            method will return true when the last byte before such a multiple is added.
23       */
24      public HexAddressDecoder(final int roundToMultiple)
25      {
26          this.roundToMultiple = roundToMultiple > 0 ? roundToMultiple : 1;
27      }
28  
29      /** Result returned by getResult. */
30      String result = "";
31  
32      /** {@inheritDoc} */
33      @Override
34      public String getResult()
35      {
36          String retVal = this.result;
37          this.result = "";
38          return retVal;
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public int getMaximumWidth()
44      {
45          return 8;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public boolean append(int address, byte theByte)
51      {
52          this.result = String.format("%08x", address / this.roundToMultiple * this.roundToMultiple);
53          return this.roundToMultiple > 1 && address % this.roundToMultiple == this.roundToMultiple - 1;
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public boolean ignoreForIdenticalOutputCheck()
59      {
60          return true;
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public String toString()
66      {
67          return "HexAddressDecoder [result=" + this.result + "]";
68      }
69  
70  }