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