1 package org.djutils.decoderdumper; 2 3 /** 4 * <p> 5 * Copyright (c) 2013-2023 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 * @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 */ 12 public class FixedString implements Decoder 13 { 14 /** 15 * The String that will be returned by <code>getResult</code> on each invocation of getResult after at least one call to 16 * append. 17 */ 18 private final String fixedResult; 19 20 /** Remember if the append method was ever called. */ 21 private boolean appendWasCalled = false; 22 23 /** 24 * Construct a Decoder that returns a fixed result in the <code>getResult</code> method. 25 * @param fixedResult String; the String that the <code>getResult</code> method will return on each invocation 26 */ 27 public FixedString(final String fixedResult) 28 { 29 this.fixedResult = fixedResult; 30 } 31 32 /** {@inheritDoc} */ 33 @Override 34 public String getResult() 35 { 36 if (this.appendWasCalled) 37 { 38 this.appendWasCalled = false; 39 return this.fixedResult; 40 } 41 return ""; 42 } 43 44 /** {@inheritDoc} */ 45 @Override 46 public int getMaximumWidth() 47 { 48 return this.fixedResult.length(); 49 } 50 51 /** {@inheritDoc} */ 52 @Override 53 public boolean append(final int address, final byte theByte) 54 { 55 this.appendWasCalled = true; 56 return false; 57 } 58 59 /** {@inheritDoc} */ 60 @Override 61 public boolean ignoreForIdenticalOutputCheck() 62 { 63 return false; 64 } 65 66 /** {@inheritDoc} */ 67 @Override 68 public String toString() 69 { 70 return "FixedString [fixedResult=" + this.fixedResult + ", appendWasCalled=" + this.appendWasCalled + "]"; 71 } 72 73 }