samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java
changeset 61 59df94cee246
child 63 cbba5b31d11c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java	Sat Jun 14 09:53:03 2008 +0200
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +
     1.9 +package org.apidesign.delegatingwriter;
    1.10 +
    1.11 +import java.io.BufferedWriter;
    1.12 +import java.io.IOException;
    1.13 +import java.io.Writer;
    1.14 +
    1.15 +/**
    1.16 + * This is a regular {@link BufferedWriter}, just its implementation
    1.17 + * of the append method can choose from three options. This allows us to
    1.18 + * simulate the potential pros and cons of various possible implementations.
    1.19 + * 
    1.20 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.21 + */
    1.22 +public class AltBufferedWriter extends BufferedWriter {
    1.23 +    private final Writer out;
    1.24 +    private final Behaviour behaviour;
    1.25 +    
    1.26 +    public AltBufferedWriter(Writer out, Behaviour behaviour) {
    1.27 +        super(out);
    1.28 +        this.out = out;
    1.29 +        this.behaviour = behaviour;
    1.30 +    }
    1.31 +    
    1.32 +    @Override
    1.33 +    public Writer append(CharSequence csq) throws IOException {
    1.34 +        switch (behaviour) {
    1.35 +            case THROW_EXCEPTION: return appendThrowException(csq); 
    1.36 +            case DELEGATE_TO_SUPER: return appendDelegateToSuper(csq);
    1.37 +            case DELEGATE_TO_OUT: return appendDelegateToUnderlaying(csq);
    1.38 +            case DELEGATE_CONDITIONALLY: return appendDelegateConditionally(csq);
    1.39 +            default: throw new IllegalStateException("Unknown" + behaviour);
    1.40 +        }
    1.41 +    }
    1.42 +    
    1.43 +    // BEGIN: writer.throw
    1.44 +    public Writer appendThrowException(CharSequence csq) throws IOException {
    1.45 +        throw new UnsupportedOperationException();
    1.46 +    }
    1.47 +    // END: writer.throw
    1.48 +    
    1.49 +    public Writer appendDelegateToSuper(CharSequence csq) throws IOException {
    1.50 +        // non-efficient variant of delegating via converting to String first 
    1.51 +        // and using one of methods that existed in 1.4
    1.52 +        // BEGIN: writer.throw.client
    1.53 +        if (csq == null) {
    1.54 +            write("null");
    1.55 +        } else {
    1.56 +            write(csq.toString());
    1.57 +        }
    1.58 +        return this;
    1.59 +        // END: writer.throw.client
    1.60 +    }
    1.61 +    
    1.62 +    public Writer appendDelegateToUnderlaying(CharSequence csq) throws IOException {
    1.63 +        // BEGIN: writer.delegateout
    1.64 +        // efficient, yet dangerous delegation skipping methods known to 
    1.65 +        // subclasses that used version 1.4
    1.66 +        out.append(csq);
    1.67 +        return this;
    1.68 +        // END: writer.delegateout
    1.69 +    }
    1.70 +
    1.71 +    private Writer appendDelegateConditionally(CharSequence csq) {
    1.72 +        throw new UnsupportedOperationException("Not yet implemented");
    1.73 +    }
    1.74 +    
    1.75 +    public enum Behaviour {
    1.76 +        THROW_EXCEPTION, DELEGATE_TO_SUPER, DELEGATE_TO_OUT, DELEGATE_CONDITIONALLY
    1.77 +    }
    1.78 +}