Class AppendableTap

  • All Implemented Interfaces:
    java.lang.Appendable

    public class AppendableTap
    extends java.lang.Object
    implements java.lang.Appendable
    AppendableTap - Documentation.

    This class can be used to "wrap" another user-provided Appendable, and "listen" or "record" the append(...) messages which would normally be sent to this other (again, user-provided) Appendable. The original instance of Appendable will receive all of the standard 'append' invocations that it would normally receive. These messages will be first 'passed through' 'this' AppendableTap instance and the character data received shall be saved inside an internal instance of StringBuffer so that it might be later retrieved by the user.

    NOTE: These message may be obtained at any time by using the AppendableTap.getString() method.

    This class' methods are synchronized as text and messaging data may often be used in a multi-threaded portion of an application. This means that the methods any messages which would be sent to this class AppendableTap will force the JVM to lock the instance of this Object, and prevent other threads from accessing this class, until this class' operations have completed.



    • Constructor Summary

      Constructors 
      Constructor
      AppendableTap​(Appendable appendable)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method
      Appendable append​(char c)
      Appendable append​(CharSequence s)
      Appendable append​(CharSequence s, int start, int end)
      void clear()
      String getString()
      String poll()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AppendableTap

        public AppendableTap​(java.lang.Appendable appendable)
        Builds an instance of this class. The provided instance of Appendable will receive all append() invocations that are sent to 'this' instance of AppendTap. It is important to note that only the append() calls sent to this class first will be 'tapped' (logged and kept in an internal StringBuffer instance). Any invocations that are made independently on the provided Appendable will not be saved by this class.
        Parameters:
        appendable - This may be any java Appendable. Any messages sent to this class will be sent to this input Appendable as well.
    • Method Detail

      • append

        public java.lang.Appendable append​(char c)
                                    throws java.io.IOException
        Logs input-parameter character 'c' to an internal StringBuffer, and then subsequently sends 'c' to the user-provided Appendable.
        Specified by:
        append in interface java.lang.Appendable
        Parameters:
        c - Any character data.
        Returns:
        'this' instance of AppendableTap shall be returned, for convenience.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
        1
        2
         sb.append(c); 
         return outputAppendable.append(c);
        
      • append

        public java.lang.Appendable append​(java.lang.CharSequence s)
                                    throws java.io.IOException
        Logs input-parameter CharSequence 's' to an internal StringBuffer, and then subsequently sends 's' to the user-provided Appendable.
        Specified by:
        append in interface java.lang.Appendable
        Parameters:
        s - Any String data.
        Returns:
        'this' instance of AppendableTap shall be returned, for convenience.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
        1
        2
         sb.append(s);
         return outputAppendable.append(s);
        
      • append

        public java.lang.Appendable append​(java.lang.CharSequence s,
                                           int start,
                                           int end)
                                    throws java.io.IOException
        Logs a substring of input-parameter CharSequence 's' defined by the int parameters 'start' and 'end'. This operation would be equivalent to calling the String method String.substring(start, end) on the input CharSequence prior to logging this character data, and sending it to the user-provided Appendable.
        Specified by:
        append in interface java.lang.Appendable
        Parameters:
        s - This may be any Java CharSequence
        start - The first character (inclusive) in the sub-string / sub-sequence of the input CharSequence to log, and pass-on.
        end - The last character (exclusive) of the input sub-string / sub-sequence of the CharSequence to log, and pass-on.
        Returns:
        'this' instance of AppendableTap shall be returned, for convenience.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
        1
        2
         sb.append(s, start, end); 
         return outputAppendable.append(s, start, end);
        
      • getString

        public java.lang.String getString()
        Retrieves the text-data that has been sent to this AppendableTap

        NOTE: Invoking this method has no effect on the underlying output (user-provided - at construction time) Appendable.
        Returns:
        Any character or text data that has been received by 'this' instance of AppendableTap through any of its append(...) methods.
        Code:
        Exact Method Body:
        1
        2
        3
        4
         String s = sb.toString();
         sb.setLength(0);
         sb.append(s);
         return s;
        
      • poll

        public java.lang.String poll()
        Resets or 'clears' the contents of the internal StringBuffer 'tap', and returns the contents that were saved.
        Returns:
        Returns the contents of the internal storage, and clears the contents.
        Code:
        Exact Method Body:
        1
        2
        3
         String s = sb.toString();
         sb.setLength(0);
         return s;
        
      • clear

        public void clear()
        Clears the 'tap' of all contents
        Code:
        Exact Method Body:
        1
         sb.setLength(0);