samples/delegatingwriterfinal/src-api2.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 132 3bc4c54f4bcc
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
     1 package api;
     2 
     3 import java.io.IOException;
     4 
     5 /** Fixing the problem caused by mixing subclassing and delegation in 
     6  * the <code>java.io.BufferedWriter</code>
     7  *
     8  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     9  */
    10 // BEGIN: writer.final.2.0
    11 public final class Writer implements Appendable {
    12     private final Impl impl;
    13     private final ImplSeq seq;
    14     
    15     private Writer(Impl impl, ImplSeq seq) {
    16         this.impl = impl;
    17         this.seq = seq;
    18     }
    19     public final void write(int c) throws IOException {
    20         if (impl != null) {
    21             char[] arr = {(char) c};
    22             impl.write(arr, 0, 1);
    23         } else {
    24             seq.write(new CharSeq(c));
    25         }
    26     }
    27     
    28     public final void write(char cbuf[]) throws IOException {
    29         if (impl != null) {
    30             impl.write(cbuf, 0, cbuf.length);
    31         } else {
    32             seq.write(new CharSeq(cbuf, 0, cbuf.length));
    33         }
    34     }
    35     public final void write(char cbuf[], int off, int len) throws IOException {
    36         if (impl != null) {
    37             impl.write(cbuf, off, len);
    38         } else {
    39             seq.write(new CharSeq(cbuf, off, len));
    40         }
    41     }
    42     public final void write(String str) throws IOException {
    43         if (impl != null) {
    44             impl.write(str, 0, str.length());
    45         } else {
    46             seq.write(str);
    47         }
    48     }
    49     public final void write(String str, int off, int len) throws IOException {
    50         if (impl != null) {
    51             impl.write(str, off, len);
    52         } else {
    53             seq.write(str.subSequence(off, off + len));
    54         }
    55     }
    56 
    57     public final void flush() throws IOException {
    58         if (impl != null) {
    59             impl.flush();
    60         } else {
    61             seq.flush();
    62         }
    63     }
    64     public final void close() throws IOException {
    65         if (impl != null) {
    66             impl.close();
    67         } else {
    68             seq.flush();
    69         }
    70     }
    71 
    72     
    73     
    74     public final Writer append(CharSequence csq) throws IOException {
    75         if (impl != null) {
    76             String s = csq == null ? "null" : csq.toString();
    77             impl.write(s, 0, s.length());
    78         } else {
    79             seq.write(csq);
    80         }
    81         return this;
    82     }
    83 
    84     public final Writer append(CharSequence csq, int start, int end) throws IOException {
    85         return append(csq.subSequence(start, end));
    86     }
    87 
    88     public final Writer append(char c) throws IOException {
    89         write(c);
    90         return this;
    91     }
    92 
    93     public static Writer create(Impl impl) {
    94         return new Writer(impl, null);
    95     }
    96 
    97     public static Writer create(ImplSeq impl) {
    98         return new Writer(null, impl);
    99     }
   100     
   101     public static Writer create(final java.io.Writer w) {
   102         return new Writer(null, new ImplSeq() {
   103             public void write(CharSequence seq) throws IOException {
   104                 w.append(seq);
   105             }
   106 
   107             public void close() throws IOException {
   108                 w.close();
   109             }
   110 
   111             public void flush() throws IOException {
   112                 w.flush();
   113             }
   114         });
   115     }
   116     
   117     public static Writer createBuffered(final Writer out) {
   118         return create(new SimpleBuffer(out));
   119     }
   120     
   121     
   122     public static interface Impl {
   123         public void close() throws IOException;
   124         public void flush() throws IOException;
   125         public void write(String str, int off, int len) throws IOException;
   126         public void write(char[] arr, int off, int len) throws IOException;
   127     }
   128     public static interface ImplSeq {
   129         public void close() throws IOException;
   130         public void flush() throws IOException;
   131         public void write(CharSequence seq) throws IOException;
   132     }
   133 }
   134 // END: writer.final.2.0