001package Torello.Java;
002
003import java.io.*;
004import java.util.Locale;
005
006/**
007 * A class that is nearly identical to the <CODE>StringBuilder</CODE> and / or
008 * <CODE>StringWriter</CODE> standard Java Classes.
009 * 
010 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=STWR>
011 */
012public class StorageWriter extends Writer
013{
014    /**
015    * This value is true by default.  When this is {@code TRUE}, all output text will be sent to
016    * {@code System.out}, in addition to the underlying {@code StringBuffer}.
017    */
018    public boolean sysOut = true;
019
020    /**
021     * All output text is sent to this {@code StringBuffer} for storage, it may cleared by calling
022     * {@link #erase()}.  The underlying {@code String} obtained, at any point, by invoking
023     * {@link #getString()}
024     */
025    protected StringBuffer sb = new StringBuffer();
026
027    /** Creates a new StringBuffer, and saves the value of sysOut */
028    public StorageWriter(boolean sendToSysOut)
029    { this.sysOut = sendToSysOut; }
030
031    /** Default constructor, sysOut = true; */
032    public StorageWriter()
033    { this.sysOut = true; }
034
035    /** Does nothing, dummy method.  Is required by definition of java.io.Writer class */
036    public void close() { }
037
038    /** Does nothing, dummy method.  Is required by definition of java.io.Writer class */
039    public void flush() { }
040
041    /**
042     * Simply calls toString() on the internally saved StringBuffer.
043     *
044     * <BR /><BR /><SPAN STYLE="color: blue; font-weight: bold">This method is really the center
045     * of this class, it allows a programmer to write log-data or log-information to both the
046     * terminal, and to an internal {@code java.lang.StringBuffer}.  This internally saved data can
047     * be retrieved later and written to a data-file (in addition to the data which was written to
048     * the screen).</SPAN>
049     *
050     * <BR /><BR /><B>NOTE:</B> Java does have an entire package ({@code java.util.logging}) -
051     * unfortunately, it is not one of the Java 8 Packages I have "gone through" completely, so I
052     * used this simple, less-featured, version of a logging class.
053     *
054     * @return This returns the entire contents of the "log" as a String.  This method is the
055     * primary reason this class exists.
056     */
057    public String getString()
058    { return sb.toString(); }
059
060    /** Clears the "internal memory" in the {@code StringBuffer}. */
061    public void erase() { sb.setLength(0); }
062
063    public void print(boolean b)    { if (sysOut) System.out.print(b); sb.append(b); }
064    public void print(char c)       { if (sysOut) System.out.print(c); sb.append(c); }
065    public void print(char[] s)     { if (sysOut) System.out.print(s); sb.append(s); }
066    public void print(double d)     { if (sysOut) System.out.print(d); sb.append(d); }
067    public void print(float f)      { if (sysOut) System.out.print(f); sb.append(f); }
068    public void print(int i)        { if (sysOut) System.out.print(i); sb.append(i); }
069    public void print(long l)       { if (sysOut) System.out.print(l); sb.append(l); }
070    public void print(Object o)     { if (sysOut) System.out.print(o); sb.append(o); }
071    public void print(String s)     { if (sysOut) System.out.print(s); sb.append(s); }
072
073    public void println()           { if (sysOut) System.out.println(); sb.append("\n"); }
074    public void println(boolean x)  { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
075    public void println(char x)     { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
076    public void println(char[] x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
077    public void println(double x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
078    public void println(float x)    { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
079    public void println(int x)      { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
080    public void println(long x)     { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
081    public void println(Object x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
082    public void println(String x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
083
084    public void write(char[] buf)   { if (sysOut) System.out.print(buf); sb.append(buf); }
085    public void write(int c)        { if (sysOut) System.out.print((char) c); sb.append((char) c); }
086
087    public Writer append(char c)
088    {
089        if (sysOut) System.out.print(c);
090        sb.append(c);
091        return this; 
092    }
093
094    public Writer append(CharSequence csq)
095    {
096        if (sysOut) System.out.print(csq);
097        sb.append(csq);
098        return this;
099    }
100
101    public Writer append(CharSequence csq, int start, int end)
102    {
103        String s = csq.subSequence(start, end).toString();
104        if (sysOut) System.out.print(s);
105        sb.append(s);
106        return this;
107    }
108
109    public Writer format(Locale l, String format, Object... args)
110    {
111        String temp = String.format(l, format, args);
112        if (sysOut) System.out.print(temp);
113        sb.append(temp);
114        return this;
115    }
116
117    public Writer format(String format, Object... args)
118    {
119        String temp = String.format(format, args);
120        if (sysOut) System.out.print(temp);
121        sb.append(temp);
122        return this;
123    }
124
125    public Writer printf(Locale l, String format, Object... args)
126    {
127        String temp = String.format(l, format, args);
128        if (sysOut) System.out.print(temp);
129        sb.append(temp);
130        return this;
131    }
132
133    public Writer printf(String format, Object... args)
134    {
135        String temp = String.format(format, args);
136        if (sysOut) System.out.print(temp);
137        sb.append(temp);
138        return this;
139    }
140
141    public void write(String s, int off, int len)
142    { 
143        s = s.substring(off, off+len); 
144        if (sysOut) System.out.print(s);
145        sb.append(s);
146    }
147
148    public void write(char[] buf, int off, int len)
149    {
150        String s = String.valueOf(buf, off, len);
151        if (sysOut) System.out.print(s);
152        sb.append(s);
153    }
154}