samples/delegatingwriterfinal/src-api1.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:06 +0200
changeset 65 4db7ceebd2b3
child 67 b029a28df444
permissions -rw-r--r--
Nonsubclassable Writer example
     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     
    14     private Writer(Impl impl) {
    15         this.impl = impl;
    16     }
    17     public final void write(int c) throws IOException {
    18         char[] arr = { (char)c };
    19         impl.write(arr, 0, 1);
    20     }
    21     
    22     public final void write(char cbuf[]) throws IOException {
    23 	impl.write(cbuf, 0, cbuf.length);
    24     }
    25     public final void write(char cbuf[], int off, int len) throws IOException {
    26         impl.write(cbuf, off, len);
    27     }
    28     public final void write(String str) throws IOException {
    29 	impl.write(str, 0, str.length());
    30     }
    31     public final void write(String str, int off, int len) throws IOException {
    32         impl.write(str, off, len);
    33     }
    34     public final void flush() throws IOException {
    35         impl.flush();
    36     }
    37     public final void close() throws IOException {
    38         impl.close();
    39     }
    40 
    41     public static Writer create(Impl impl) {
    42         return new Writer(impl);
    43     }
    44     
    45     public static Writer create(final java.io.Writer w) {
    46         return new Writer(new Impl() {
    47             public void write(String str, int off, int len) throws IOException {
    48                 w.write(str, off, len);
    49             }
    50 
    51             public void write(char[] arr, int off, int len) throws IOException {
    52                 w.write(arr, off, len);
    53             }
    54 
    55             public void close() throws IOException {
    56                 w.close();
    57             }
    58 
    59             public void flush() throws IOException {
    60                 w.flush();
    61             }
    62         });
    63     }
    64     
    65     public static Writer createBuffered(final Writer out) {
    66         class Delegate extends java.io.Writer {
    67             @Override
    68             public void write(char[] cbuf, int off, int len) throws IOException {
    69                 out.write(cbuf, off, len);
    70             }
    71 
    72             @Override
    73             public void flush() throws IOException {
    74                 out.flush();
    75             }
    76 
    77             @Override
    78             public void close() throws IOException {
    79                 out.close();
    80             }
    81             
    82         }
    83         return create(new BufferedWriter(new Delegate()));
    84     }
    85 
    86     
    87     public static interface Impl {
    88         public void close() throws IOException;
    89         public void flush() throws IOException;
    90         public void write(String str, int off, int len) throws IOException;
    91         public void write(char[] arr, int off, int len) throws IOException;
    92     }
    93     
    94     
    95     
    96 }