/** * A Quitter keeps track of the number of times that this Quitter has been asked * to transform, and if that number is less than or equal to this Quitter's * quitting time, then the returned String is the given String with * "- I am on it" appended. After the quitting time, the returned String is the * given String, unchanged. *

* Each Quitter has its own quitting time; the default quitting time is 1. * * @author David Mutchler. Created October 8, 2009. */ public class Quitter implements StringTransformable { private static String messageToApppend = " - I am on it!"; private int quittingTime; private int numberOfTransformationsSoFar; /** * Initializes the quitting time to 1 (so that the first transform returns * the given String with the special message appended while subsequent * transforms return the given String unchanged. */ public Quitter() { this(1); } /** * Initializes the quitting time to the given value (so that transforms * after the quitting time return the String unchanged, while transforms at * or before the quitting time return the String with a message appended. * * @param quittingTime * Number of times to append the string before quitting. */ public Quitter(int quittingTime) { this.quittingTime = quittingTime; this.numberOfTransformationsSoFar = 0; } /** * Keeps track of the number of times that this Quitter has been asked to * transform, and if that number is less than or equal to this Quitter's * quitting time, then the returned String is the given String with * "- I am on it" appended. After the quitting time, the returned String is * the given String, unchanged. * * @param stringToTransform * The String to transform. * @return The transformed String. */ @Override public String transform(String stringToTransform) { ++this.numberOfTransformationsSoFar; if (this.numberOfTransformationsSoFar <= this.quittingTime) { return stringToTransform + Quitter.messageToApppend; } else { return stringToTransform; } } }