samples/delegatingwriterfinal/src-api2.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:06 +0200
changeset 66 8379bb7c0dff
parent 65 4db7ceebd2b3
child 67 b029a28df444
permissions -rw-r--r--
Tests rewritten to new version, just the Writer version 2.0 does not yet implement Appendable
     1 package api;
     2 
     3 import java.io.BufferedWriter;
     4 import java.io.IOException;
     5 
     6 /** Fixing the problem caused by mixing subclassing and delegation in 
     7  * the <code>java.io.BufferedWriter</code>
     8  *
     9  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    10  */
    11 public final class Writer {
    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     public static Writer create(Impl impl) {
    73         return new Writer(impl, null);
    74     }
    75 
    76     public static Writer create(ImplSeq impl) {
    77         return new Writer(null, impl);
    78     }
    79     
    80     public static Writer create(final java.io.Writer w) {
    81         return new Writer(new Impl() {
    82             public void write(String str, int off, int len) throws IOException {
    83                 w.write(str, off, len);
    84             }
    85 
    86             public void write(char[] arr, int off, int len) throws IOException {
    87                 w.write(arr, off, len);
    88             }
    89 
    90             public void close() throws IOException {
    91                 w.close();
    92             }
    93 
    94             public void flush() throws IOException {
    95                 w.flush();
    96             }
    97         }, null);
    98     }
    99     
   100     public static Writer createBuffered(final Writer out) {
   101         class Delegate extends java.io.Writer {
   102             @Override
   103             public void write(char[] cbuf, int off, int len) throws IOException {
   104                 out.write(cbuf, off, len);
   105             }
   106 
   107             @Override
   108             public void flush() throws IOException {
   109                 out.flush();
   110             }
   111 
   112             @Override
   113             public void close() throws IOException {
   114                 out.close();
   115             }
   116             
   117         }
   118         return create(new BufferedWriter(new Delegate()));
   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     
   135     
   136 }