samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/CountingWriter.java
changeset 66 8379bb7c0dff
child 194 2c53209c30e4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/CountingWriter.java	Sat Jun 14 09:53:06 2008 +0200
     1.3 @@ -0,0 +1,36 @@
     1.4 +
     1.5 +package api.usage.twodotzero;
     1.6 +
     1.7 +import api.Writer;
     1.8 +import java.io.IOException;
     1.9 +import java.util.concurrent.atomic.AtomicInteger;
    1.10 +
    1.11 +/** Writer that counts the number of written in characters.
    1.12 + *
    1.13 + * @author Jaroslav Tulach
    1.14 + */
    1.15 +public class CountingWriter implements Writer.ImplSeq {
    1.16 +    private final AtomicInteger counter;
    1.17 +    
    1.18 +    private CountingWriter(AtomicInteger counter) {
    1.19 +        this.counter = counter;
    1.20 +    }
    1.21 +    
    1.22 +    public static Writer create(AtomicInteger result) {
    1.23 +        return Writer.create(new CountingWriter(result));
    1.24 +    }
    1.25 +
    1.26 +    @Override
    1.27 +    public void write(CharSequence csq) throws IOException {
    1.28 +        counter.addAndGet(csq.length());
    1.29 +    }
    1.30 +
    1.31 +    @Override
    1.32 +    public void flush() throws IOException {
    1.33 +    }
    1.34 +
    1.35 +    @Override
    1.36 +    public void close() throws IOException {
    1.37 +    }
    1.38 +
    1.39 +}