samples/delegatingwriterfinal/src-api2.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
     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) 
    36     throws IOException {
    37         if (impl != null) {
    38             impl.write(cbuf, off, len);
    39         } else {
    40             seq.write(new CharSeq(cbuf, off, len));
    41         }
    42     }
    43     public final void write(String str) throws IOException {
    44         if (impl != null) {
    45             impl.write(str, 0, str.length());
    46         } else {
    47             seq.write(str);
    48         }
    49     }
    50     public final void write(String str, int off, int len) 
    51     throws IOException {
    52         if (impl != null) {
    53             impl.write(str, off, len);
    54         } else {
    55             seq.write(str.subSequence(off, off + len));
    56         }
    57     }
    58 
    59     public final void flush() throws IOException {
    60         if (impl != null) {
    61             impl.flush();
    62         } else {
    63             seq.flush();
    64         }
    65     }
    66     public final void close() throws IOException {
    67         if (impl != null) {
    68             impl.close();
    69         } else {
    70             seq.flush();
    71         }
    72     }
    73 
    74     
    75     
    76     public final Writer append(CharSequence csq) throws IOException {
    77         if (impl != null) {
    78             String s = csq == null ? "null" : csq.toString();
    79             impl.write(s, 0, s.length());
    80         } else {
    81             seq.write(csq);
    82         }
    83         return this;
    84     }
    85 
    86     public final Writer append(CharSequence csq, int start, int end) 
    87     throws IOException {
    88         return append(csq.subSequence(start, end));
    89     }
    90 
    91     public final Writer append(char c) throws IOException {
    92         write(c);
    93         return this;
    94     }
    95 
    96     public static Writer create(Impl impl) {
    97         return new Writer(impl, null);
    98     }
    99 
   100     public static Writer create(ImplSeq seq) {
   101         return new Writer(null, seq);
   102     }
   103     
   104     public static Writer create(final java.io.Writer w) {
   105         return new Writer(null, new ImplSeq() {
   106             public void write(CharSequence seq) throws IOException {
   107                 w.append(seq);
   108             }
   109 
   110             public void close() throws IOException {
   111                 w.close();
   112             }
   113 
   114             public void flush() throws IOException {
   115                 w.flush();
   116             }
   117         });
   118     }
   119     
   120     public static Writer createBuffered(final Writer out) {
   121         return create(new SimpleBuffer(out));
   122     }
   123     
   124     
   125     public static interface Impl {
   126         public void close() throws IOException;
   127         public void flush() throws IOException;
   128         public void write(String str, int off, int len) throws IOException;
   129         public void write(char[] arr, int off, int len) throws IOException;
   130     }
   131     public static interface ImplSeq {
   132         public void close() throws IOException;
   133         public void flush() throws IOException;
   134         public void write(CharSequence seq) throws IOException;
   135     }
   136 }
   137 // END: writer.final.2.0