samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package org.apidesign.delegatingwriter;
8 import java.io.BufferedWriter;
9 import java.io.IOException;
10 import java.io.Writer;
13 * This is a regular {@link BufferedWriter}, just its implementation
14 * of the append method can choose from three options. This allows us to
15 * simulate the potential pros and cons of various possible implementations.
17 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
19 public class AltBufferedWriter extends BufferedWriter {
20 private final Writer out;
21 private final Behaviour behaviour;
23 public AltBufferedWriter(Writer out, Behaviour behaviour) {
26 this.behaviour = behaviour;
30 public Writer append(CharSequence csq) throws IOException {
32 case THROW_EXCEPTION: return appendThrowException(csq);
33 case DELEGATE_TO_SUPER: return appendDelegateToSuper(csq);
34 case DELEGATE_TO_OUT: return appendDelegateToUnderlaying(csq);
35 case DELEGATE_CONDITIONALLY: return appendDelegateConditionally(csq);
36 default: throw new IllegalStateException("Unknown" + behaviour);
40 // BEGIN: writer.throw
41 public Writer appendThrowException(CharSequence csq) throws IOException {
42 throw new UnsupportedOperationException();
46 public Writer appendDelegateToSuper(CharSequence csq) throws IOException {
47 // non-efficient variant of delegating via converting to String first
48 // and using one of methods that existed in 1.4
49 // BEGIN: writer.throw.client
53 write(csq.toString());
56 // END: writer.throw.client
59 public Writer appendDelegateToUnderlaying(CharSequence csq) throws IOException {
60 // BEGIN: writer.delegateout
61 // efficient, yet dangerous delegation skipping methods known to
62 // subclasses that used version 1.4
65 // END: writer.delegateout
68 private Writer appendDelegateConditionally(CharSequence csq) {
69 throw new UnsupportedOperationException("Not yet implemented");
72 public enum Behaviour {
73 THROW_EXCEPTION, DELEGATE_TO_SUPER, DELEGATE_TO_OUT, DELEGATE_CONDITIONALLY