samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:04 +0200
changeset 63 cbba5b31d11c
parent 61 59df94cee246
child 64 7b26c64804c2
permissions -rw-r--r--
CrytoWriter and when it is broken
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@63
    23
    public AltBufferedWriter(Writer out) {
jtulach@63
    24
        // behave exactly like BufferedWriter in 1.5 behaves
jtulach@63
    25
        this(out, Behaviour.DELEGATE_TO_SUPER);
jtulach@63
    26
    }
jtulach@61
    27
    public AltBufferedWriter(Writer out, Behaviour behaviour) {
jtulach@61
    28
        super(out);
jtulach@61
    29
        this.out = out;
jtulach@61
    30
        this.behaviour = behaviour;
jtulach@61
    31
    }
jtulach@61
    32
    
jtulach@61
    33
    @Override
jtulach@61
    34
    public Writer append(CharSequence csq) throws IOException {
jtulach@61
    35
        switch (behaviour) {
jtulach@61
    36
            case THROW_EXCEPTION: return appendThrowException(csq); 
jtulach@61
    37
            case DELEGATE_TO_SUPER: return appendDelegateToSuper(csq);
jtulach@61
    38
            case DELEGATE_TO_OUT: return appendDelegateToUnderlaying(csq);
jtulach@61
    39
            case DELEGATE_CONDITIONALLY: return appendDelegateConditionally(csq);
jtulach@61
    40
            default: throw new IllegalStateException("Unknown" + behaviour);
jtulach@61
    41
        }
jtulach@61
    42
    }
jtulach@61
    43
    
jtulach@61
    44
    public Writer appendThrowException(CharSequence csq) throws IOException {
jtulach@63
    45
        /* in case of real code, this would be part of the regular append method BEGIN: writer.throw
jtulach@63
    46
    public Writer append(CharSequence csq) throws IOException {
jtulach@63
    47
        /* thrown an exception as this method is new and subclasses need to override it */
jtulach@61
    48
        throw new UnsupportedOperationException();
jtulach@61
    49
    }
jtulach@61
    50
    // END: writer.throw
jtulach@61
    51
    
jtulach@61
    52
    public Writer appendDelegateToSuper(CharSequence csq) throws IOException {
jtulach@61
    53
        // non-efficient variant of delegating via converting to String first 
jtulach@61
    54
        // and using one of methods that existed in 1.4
jtulach@61
    55
        // BEGIN: writer.throw.client
jtulach@61
    56
        if (csq == null) {
jtulach@61
    57
            write("null");
jtulach@61
    58
        } else {
jtulach@61
    59
            write(csq.toString());
jtulach@61
    60
        }
jtulach@61
    61
        return this;
jtulach@61
    62
        // END: writer.throw.client
jtulach@61
    63
    }
jtulach@61
    64
    
jtulach@61
    65
    public Writer appendDelegateToUnderlaying(CharSequence csq) throws IOException {
jtulach@61
    66
        // BEGIN: writer.delegateout
jtulach@63
    67
        // efficient, yet dangerous delegation skipping methods unknown to 
jtulach@61
    68
        // subclasses that used version 1.4
jtulach@61
    69
        out.append(csq);
jtulach@61
    70
        return this;
jtulach@61
    71
        // END: writer.delegateout
jtulach@61
    72
    }
jtulach@61
    73
jtulach@61
    74
    private Writer appendDelegateConditionally(CharSequence csq) {
jtulach@61
    75
        throw new UnsupportedOperationException("Not yet implemented");
jtulach@61
    76
    }
jtulach@61
    77
    
jtulach@61
    78
    public enum Behaviour {
jtulach@61
    79
        THROW_EXCEPTION, DELEGATE_TO_SUPER, DELEGATE_TO_OUT, DELEGATE_CONDITIONALLY
jtulach@61
    80
    }
jtulach@61
    81
}