samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/CountingWriter.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
     1 
     2 package api.usage.twodotzero;
     3 
     4 import api.Writer;
     5 import java.io.IOException;
     6 import java.util.concurrent.atomic.AtomicInteger;
     7 
     8 /** Writer that counts the number of written in characters.
     9  */
    10 public class CountingWriter implements Writer.ImplSeq {
    11     private final AtomicInteger counter;
    12     
    13     private CountingWriter(AtomicInteger counter) {
    14         this.counter = counter;
    15     }
    16     
    17     public static Writer create(AtomicInteger result) {
    18         return Writer.create(new CountingWriter(result));
    19     }
    20 
    21     @Override
    22     public void write(CharSequence csq) throws IOException {
    23         counter.addAndGet(csq.length());
    24     }
    25 
    26     @Override
    27     public void flush() throws IOException {
    28     }
    29 
    30     @Override
    31     public void close() throws IOException {
    32     }
    33 
    34 }