samples/delegatingwriterfinal/src-api2.0/api/Writer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:06 +0200
changeset 65 4db7ceebd2b3
child 66 8379bb7c0dff
permissions -rw-r--r--
Nonsubclassable Writer example
     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 public final class Writer {
    11     private final Impl impl;
    12     
    13     private Writer(Impl impl) {
    14         this.impl = impl;
    15     }
    16     public final void write(int c) throws IOException {
    17         impl.write(new CharSeq(c));
    18     }
    19     
    20     public final void write(char cbuf[]) throws IOException {
    21 	impl.write(new CharSeq(cbuf, 0, cbuf.length));
    22     }
    23     public final void write(char cbuf[], int off, int len) throws IOException {
    24         impl.write(new CharSeq(cbuf, off, len));
    25     }
    26     public final void write(String str) throws IOException {
    27 	impl.write(str);
    28     }
    29     public final void write(String str, int off, int len) throws IOException {
    30         impl.write(str.subSequence(off, off + len));
    31     }
    32 
    33     
    34     
    35     public static interface Impl {
    36         public void write(CharSequence seq) throws IOException;
    37     }
    38     
    39     
    40     
    41 }