Package Torello.Java.Additional
Class AppendableTap
- java.lang.Object
-
- Torello.Java.Additional.AppendableTap
-
- All Implemented Interfaces:
java.lang.Appendable
public class AppendableTap extends java.lang.Object implements java.lang.Appendable
AppendableImplementation:
This class implements and upgrades the Simple, Javajava.lang.Appendableinterface, providing additional features and improvements to it. TheAppendableinterace is a viable design-decsion possibility for outputing text-data to a log.
AnAppendableis both blindingly easy, and highly customizable to use for third-party developers who may not be familiar with the internals of another developer's source-code.
This Class' Primary Addition toAppendable:
This class allows a user to 'wrap' anotherAppendable, and copy the output that it receives in it'sappend(...)method invocations to an internal storage buffer. That internal storage may be retrieved whenever the need arises.An implementation of the standard Java'Appendable'interface that keeps an internal log of all text-data that 'passes through' theAppendable. This class can be used as a wrapper another (user-provided)Appendable, so that this class may "listen" or "record" allappend(...)messages which are being sent to the wrapped (the other, user-provided)Appendable. The original instance ofAppendablewill still continue to receive all of its'append'text & character data, as it normally would. However, this wrapper class will be recording the character-data in a buffer.
All messages sent to the wrappedAppendablewill, first, 'pass through'thisAppendableTapinstance. The character data received shall be saved inside an internal instance ofStringBuffer, so that it may later be retrieved.
RECORDED MESSAGES:
The recorded-message(s) may be obtained, at any time, using the methodAppendableTap.getString().
SYNCHRONIZED METHODS:
This class' methods are synchronized. Text and messaging data sent in a multi-threaded environment / application is quite common. This means that the methods any messages which would be sent to this class (AppendableTap) will force the JVM to lock the instance of thisObject, and prevent other threads from accessing this class, until this class' operations have completed. (This is what the Java-Keyword'synchronized'means).
This was an early decision decision made solely because of the original use for this class in the multi-threadedOSResponseandOSCommandsclasses. ClassAppendableTapwas eventually replaced by the classesTriAppendableand is no longer regularly used in that set of tools.
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/AppendableTap.java
- Open New Browser-Tab: Torello/Java/Additional/AppendableTap.java
File Size: 4,636 Bytes Line Count: 120 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor AppendableTap(Appendable appendable)
-
Method Summary
Methods: interface java.lang.Appendable Modifier and Type Method Appendableappend(char c)Appendableappend(CharSequence s)Appendableappend(CharSequence s, int start, int end)Retrieve Tap's Contents, as a java.lang.String Modifier and Type Method StringgetString()Retrieve Tap's Contents, and Reset / Empty the Tap Modifier and Type Method Stringpoll()Reset / Empty the Tap Modifier and Type Method voidclear()
-
-
-
Constructor Detail
-
AppendableTap
public AppendableTap(java.lang.Appendable appendable)
Builds an instance of this class. The provided instance ofAppendablewill receive allappend()invocations that are sent to'this'instance ofAppendTap. It is important to note that only theappend()calls sent to this class first will be 'tapped' (logged and kept in an internalStringBufferinstance). Any invocations that are made independently on the providedAppendablewill not be saved by this class.- Parameters:
appendable- This may be any javaAppendable. Any messages sent to this class will be sent to this inputAppendableas well.
-
-
Method Detail
-
append
public java.lang.Appendable append(char c) throws java.io.IOException
Logs input-parameter character'c'to an internalStringBuffer, and then subsequently sends'c'to the user-providedAppendable.- Specified by:
appendin interfacejava.lang.Appendable- Parameters:
c- Any character data.- Returns:
'this'instance ofAppendableTapshall be returned, for convenience.- Throws:
java.io.IOException- Code:
- Exact Method Body:
sb.append(c); return outputAppendable.append(c);
-
append
public java.lang.Appendable append(java.lang.CharSequence s) throws java.io.IOException
Logs input-parameterCharSequence 's'to an internalStringBuffer, and then subsequently sends's'to the user-providedAppendable.- Specified by:
appendin interfacejava.lang.Appendable- Parameters:
s- AnyStringdata.- Returns:
'this'instance ofAppendableTapshall be returned, for convenience.- Throws:
java.io.IOException- Code:
- Exact Method Body:
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-parameterCharSequence 's'defined by theintparameters'start'and'end'. This operation would be equivalent to calling theStringmethodString.substring(start, end)on the inputCharSequenceprior to logging this character data, and sending it to the user-providedAppendable.- Specified by:
appendin interfacejava.lang.Appendable- Parameters:
s- This may be any JavaCharSequencestart- The first character (inclusive) in the sub-string / sub-sequence of the inputCharSequenceto log, and pass-on.end- The last character (exclusive) of the input sub-string / sub-sequence of theCharSequenceto log, and pass-on.- Returns:
'this'instance ofAppendableTapshall be returned, for convenience.- Throws:
java.io.IOException- Code:
- Exact Method Body:
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 thisAppendableTapNote: 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 ofAppendableTapthrough any of itsappend(...)methods. - Code:
- Exact Method Body:
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 internalStringBuffer'tap', and returns the contents that were saved.- Returns:
- Returns the contents of the internal storage, and clears the contents.
- Code:
- Exact Method Body:
String s = sb.toString(); sb.setLength(0); return s;
-
clear
public void clear()
Clears the 'tap' of all contents- Code:
- Exact Method Body:
sb.setLength(0);
-
-