View Javadoc
1   package org.djutils.decoderdumper;
2   
3   /**
4    * <p>
5    * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
6    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
7    * </p>
8    * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
9    * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
10   */
11  public class FixedString implements Decoder
12  {
13      /**
14       * The String that will be returned by <code>getResult</code> on each invocation of getResult after at least one call to
15       * append.
16       */
17      private final String fixedResult;
18  
19      /** Remember if the append method was ever called. */
20      private boolean appendWasCalled = false;
21  
22      /**
23       * Construct a Decoder that returns a fixed result in the <code>getResult</code> method.
24       * @param fixedResult String; the String that the <code>getResult</code> method will return on each invocation
25       */
26      public FixedString(final String fixedResult)
27      {
28          this.fixedResult = fixedResult;
29      }
30  
31      @Override
32      public String getResult()
33      {
34          if (this.appendWasCalled)
35          {
36              this.appendWasCalled = false;
37              return this.fixedResult;
38          }
39          return "";
40      }
41  
42      @Override
43      public int getMaximumWidth()
44      {
45          return this.fixedResult.length();
46      }
47  
48      @Override
49      public boolean append(final int address, final byte theByte)
50      {
51          this.appendWasCalled = true;
52          return false;
53      }
54  
55      @Override
56      public boolean ignoreForIdenticalOutputCheck()
57      {
58          return false;
59      }
60  
61      @Override
62      public String toString()
63      {
64          return "FixedString [fixedResult=" + this.fixedResult + ", appendWasCalled=" + this.appendWasCalled + "]";
65      }
66  
67  }