samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:03 +0200
changeset 61 59df94cee246
child 63 cbba5b31d11c
permissions -rw-r--r--
Demo for writer throwing an exception; and also writing out CD content
jtulach@61
     1
/*
jtulach@61
     2
 * To change this template, choose Tools | Templates
jtulach@61
     3
 * and open the template in the editor.
jtulach@61
     4
 */
jtulach@61
     5
jtulach@61
     6
package org.apidesign.delegatingwriter;
jtulach@61
     7
jtulach@61
     8
import java.io.BufferedWriter;
jtulach@61
     9
import java.io.IOException;
jtulach@61
    10
import java.io.Writer;
jtulach@61
    11
jtulach@61
    12
/**
jtulach@61
    13
 * This is a regular {@link BufferedWriter}, just its implementation
jtulach@61
    14
 * of the append method can choose from three options. This allows us to
jtulach@61
    15
 * simulate the potential pros and cons of various possible implementations.
jtulach@61
    16
 * 
jtulach@61
    17
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@61
    18
 */
jtulach@61
    19
public class AltBufferedWriter extends BufferedWriter {
jtulach@61
    20
    private final Writer out;
jtulach@61
    21
    private final Behaviour behaviour;
jtulach@61
    22
    
jtulach@61
    23
    public AltBufferedWriter(Writer out, Behaviour behaviour) {
jtulach@61
    24
        super(out);
jtulach@61
    25
        this.out = out;
jtulach@61
    26
        this.behaviour = behaviour;
jtulach@61
    27
    }
jtulach@61
    28
    
jtulach@61
    29
    @Override
jtulach@61
    30
    public Writer append(CharSequence csq) throws IOException {
jtulach@61
    31
        switch (behaviour) {
jtulach@61
    32
            case THROW_EXCEPTION: return appendThrowException(csq); 
jtulach@61
    33
            case DELEGATE_TO_SUPER: return appendDelegateToSuper(csq);
jtulach@61
    34
            case DELEGATE_TO_OUT: return appendDelegateToUnderlaying(csq);
jtulach@61
    35
            case DELEGATE_CONDITIONALLY: return appendDelegateConditionally(csq);
jtulach@61
    36
            default: throw new IllegalStateException("Unknown" + behaviour);
jtulach@61
    37
        }
jtulach@61
    38
    }
jtulach@61
    39
    
jtulach@61
    40
    // BEGIN: writer.throw
jtulach@61
    41
    public Writer appendThrowException(CharSequence csq) throws IOException {
jtulach@61
    42
        throw new UnsupportedOperationException();
jtulach@61
    43
    }
jtulach@61
    44
    // END: writer.throw
jtulach@61
    45
    
jtulach@61
    46
    public Writer appendDelegateToSuper(CharSequence csq) throws IOException {
jtulach@61
    47
        // non-efficient variant of delegating via converting to String first 
jtulach@61
    48
        // and using one of methods that existed in 1.4
jtulach@61
    49
        // BEGIN: writer.throw.client
jtulach@61
    50
        if (csq == null) {
jtulach@61
    51
            write("null");
jtulach@61
    52
        } else {
jtulach@61
    53
            write(csq.toString());
jtulach@61
    54
        }
jtulach@61
    55
        return this;
jtulach@61
    56
        // END: writer.throw.client
jtulach@61
    57
    }
jtulach@61
    58
    
jtulach@61
    59
    public Writer appendDelegateToUnderlaying(CharSequence csq) throws IOException {
jtulach@61
    60
        // BEGIN: writer.delegateout
jtulach@61
    61
        // efficient, yet dangerous delegation skipping methods known to 
jtulach@61
    62
        // subclasses that used version 1.4
jtulach@61
    63
        out.append(csq);
jtulach@61
    64
        return this;
jtulach@61
    65
        // END: writer.delegateout
jtulach@61
    66
    }
jtulach@61
    67
jtulach@61
    68
    private Writer appendDelegateConditionally(CharSequence csq) {
jtulach@61
    69
        throw new UnsupportedOperationException("Not yet implemented");
jtulach@61
    70
    }
jtulach@61
    71
    
jtulach@61
    72
    public enum Behaviour {
jtulach@61
    73
        THROW_EXCEPTION, DELEGATE_TO_SUPER, DELEGATE_TO_OUT, DELEGATE_CONDITIONALLY
jtulach@61
    74
    }
jtulach@61
    75
}