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
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package org.apidesign.delegatingwriter;
     7 
     8 import java.io.BufferedWriter;
     9 import java.io.IOException;
    10 import java.io.Writer;
    11 
    12 /**
    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.
    16  * 
    17  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    18  */
    19 public class AltBufferedWriter extends BufferedWriter {
    20     private final Writer out;
    21     private final Behaviour behaviour;
    22     
    23     public AltBufferedWriter(Writer out, Behaviour behaviour) {
    24         super(out);
    25         this.out = out;
    26         this.behaviour = behaviour;
    27     }
    28     
    29     @Override
    30     public Writer append(CharSequence csq) throws IOException {
    31         switch (behaviour) {
    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);
    37         }
    38     }
    39     
    40     // BEGIN: writer.throw
    41     public Writer appendThrowException(CharSequence csq) throws IOException {
    42         throw new UnsupportedOperationException();
    43     }
    44     // END: writer.throw
    45     
    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
    50         if (csq == null) {
    51             write("null");
    52         } else {
    53             write(csq.toString());
    54         }
    55         return this;
    56         // END: writer.throw.client
    57     }
    58     
    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
    63         out.append(csq);
    64         return this;
    65         // END: writer.delegateout
    66     }
    67 
    68     private Writer appendDelegateConditionally(CharSequence csq) {
    69         throw new UnsupportedOperationException("Not yet implemented");
    70     }
    71     
    72     public enum Behaviour {
    73         THROW_EXCEPTION, DELEGATE_TO_SUPER, DELEGATE_TO_OUT, DELEGATE_CONDITIONALLY
    74     }
    75 }