samples/delegatingwriterfinal/src-api2.0/api/Writer.java
changeset 66 8379bb7c0dff
parent 65 4db7ceebd2b3
child 67 b029a28df444
     1.1 --- a/samples/delegatingwriterfinal/src-api2.0/api/Writer.java	Sat Jun 14 09:53:06 2008 +0200
     1.2 +++ b/samples/delegatingwriterfinal/src-api2.0/api/Writer.java	Sat Jun 14 09:53:06 2008 +0200
     1.3 @@ -1,5 +1,6 @@
     1.4  package api;
     1.5  
     1.6 +import java.io.BufferedWriter;
     1.7  import java.io.IOException;
     1.8  
     1.9  /** Fixing the problem caused by mixing subclassing and delegation in 
    1.10 @@ -9,30 +10,124 @@
    1.11   */
    1.12  public final class Writer {
    1.13      private final Impl impl;
    1.14 +    private final ImplSeq seq;
    1.15      
    1.16 -    private Writer(Impl impl) {
    1.17 +    private Writer(Impl impl, ImplSeq seq) {
    1.18          this.impl = impl;
    1.19 +        this.seq = seq;
    1.20      }
    1.21      public final void write(int c) throws IOException {
    1.22 -        impl.write(new CharSeq(c));
    1.23 +        if (impl != null) {
    1.24 +            char[] arr = {(char) c};
    1.25 +            impl.write(arr, 0, 1);
    1.26 +        } else {
    1.27 +            seq.write(new CharSeq(c));
    1.28 +        }
    1.29      }
    1.30      
    1.31      public final void write(char cbuf[]) throws IOException {
    1.32 -	impl.write(new CharSeq(cbuf, 0, cbuf.length));
    1.33 +        if (impl != null) {
    1.34 +            impl.write(cbuf, 0, cbuf.length);
    1.35 +        } else {
    1.36 +            seq.write(new CharSeq(cbuf, 0, cbuf.length));
    1.37 +        }
    1.38      }
    1.39      public final void write(char cbuf[], int off, int len) throws IOException {
    1.40 -        impl.write(new CharSeq(cbuf, off, len));
    1.41 +        if (impl != null) {
    1.42 +            impl.write(cbuf, off, len);
    1.43 +        } else {
    1.44 +            seq.write(new CharSeq(cbuf, off, len));
    1.45 +        }
    1.46      }
    1.47      public final void write(String str) throws IOException {
    1.48 -	impl.write(str);
    1.49 +        if (impl != null) {
    1.50 +            impl.write(str, 0, str.length());
    1.51 +        } else {
    1.52 +            seq.write(str);
    1.53 +        }
    1.54      }
    1.55      public final void write(String str, int off, int len) throws IOException {
    1.56 -        impl.write(str.subSequence(off, off + len));
    1.57 +        if (impl != null) {
    1.58 +            impl.write(str, off, len);
    1.59 +        } else {
    1.60 +            seq.write(str.subSequence(off, off + len));
    1.61 +        }
    1.62      }
    1.63  
    1.64 +    public final void flush() throws IOException {
    1.65 +        if (impl != null) {
    1.66 +            impl.flush();
    1.67 +        } else {
    1.68 +            seq.flush();
    1.69 +        }
    1.70 +    }
    1.71 +    public final void close() throws IOException {
    1.72 +        if (impl != null) {
    1.73 +            impl.close();
    1.74 +        } else {
    1.75 +            seq.flush();
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    public static Writer create(Impl impl) {
    1.80 +        return new Writer(impl, null);
    1.81 +    }
    1.82 +
    1.83 +    public static Writer create(ImplSeq impl) {
    1.84 +        return new Writer(null, impl);
    1.85 +    }
    1.86 +    
    1.87 +    public static Writer create(final java.io.Writer w) {
    1.88 +        return new Writer(new Impl() {
    1.89 +            public void write(String str, int off, int len) throws IOException {
    1.90 +                w.write(str, off, len);
    1.91 +            }
    1.92 +
    1.93 +            public void write(char[] arr, int off, int len) throws IOException {
    1.94 +                w.write(arr, off, len);
    1.95 +            }
    1.96 +
    1.97 +            public void close() throws IOException {
    1.98 +                w.close();
    1.99 +            }
   1.100 +
   1.101 +            public void flush() throws IOException {
   1.102 +                w.flush();
   1.103 +            }
   1.104 +        }, null);
   1.105 +    }
   1.106 +    
   1.107 +    public static Writer createBuffered(final Writer out) {
   1.108 +        class Delegate extends java.io.Writer {
   1.109 +            @Override
   1.110 +            public void write(char[] cbuf, int off, int len) throws IOException {
   1.111 +                out.write(cbuf, off, len);
   1.112 +            }
   1.113 +
   1.114 +            @Override
   1.115 +            public void flush() throws IOException {
   1.116 +                out.flush();
   1.117 +            }
   1.118 +
   1.119 +            @Override
   1.120 +            public void close() throws IOException {
   1.121 +                out.close();
   1.122 +            }
   1.123 +            
   1.124 +        }
   1.125 +        return create(new BufferedWriter(new Delegate()));
   1.126 +    }
   1.127      
   1.128      
   1.129      public static interface Impl {
   1.130 +        public void close() throws IOException;
   1.131 +        public void flush() throws IOException;
   1.132 +        public void write(String str, int off, int len) throws IOException;
   1.133 +        public void write(char[] arr, int off, int len) throws IOException;
   1.134 +    }
   1.135 +    public static interface ImplSeq {
   1.136 +        public void close() throws IOException;
   1.137 +        public void flush() throws IOException;
   1.138          public void write(CharSequence seq) throws IOException;
   1.139      }
   1.140