samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
1 package org.apidesign.delegatingwriter;
3 import java.io.BufferedWriter;
4 import java.io.IOException;
8 * This is a regular {@link BufferedWriter}, just its implementation
9 * of the append method can choose from three options. This allows us to
10 * simulate the potential pros and cons of various possible implementations.
12 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
14 public class AltBufferedWriter extends BufferedWriter {
15 private final Writer out;
16 private final Behaviour behaviour;
18 public AltBufferedWriter(Writer out) {
19 // behave exactly like BufferedWriter in 1.5 behaves
20 this(out, Behaviour.DELEGATE_TO_SUPER);
22 public AltBufferedWriter(Writer out, Behaviour behaviour) {
25 this.behaviour = behaviour;
29 public Writer append(CharSequence csq) throws IOException {
32 return appendThrowException(csq);
33 case DELEGATE_TO_SUPER:
34 return appendDelegateToSuper(csq);
36 return appendDelegateToUnderlaying(csq);
37 case DELEGATE_CONDITIONALLY:
38 return appendDelegateConditionally(csq);
40 throw new IllegalStateException("Unknown" + behaviour);
44 public Writer appendThrowException(CharSequence csq) throws IOException {
45 /* in case of real code, this would be part of
46 the regular append method. BEGIN: writer.throw
47 public Writer append(CharSequence csq) throws IOException {
48 /* thrown an exception as this method is new and
49 subclasses need to override it */
50 throw new UnsupportedOperationException();
54 public Writer appendDelegateToSuper(CharSequence csq) throws IOException {
55 // non-efficient variant of delegating via converting to String first
56 // and using one of methods that existed in 1.4
57 // BEGIN: writer.super
61 write(csq.toString());
67 public Writer appendDelegateToUnderlaying(CharSequence csq) throws IOException {
68 // BEGIN: writer.delegateout
69 // efficient, yet dangerous delegation skipping methods unknown to
70 // subclasses that used version 1.4
71 if (shouldBufferAsTheSequenceIsNotTooBig(csq)) {
72 write(csq.toString());
78 // END: writer.delegateout
81 private Writer appendDelegateConditionally(CharSequence csq)
83 // BEGIN: writer.conditionally
84 boolean isOverriden = false;
90 ).getDeclaringClass() != Writer.class
95 ).getDeclaringClass() != BufferedWriter.class
99 "write", String.class, Integer.TYPE, Integer.TYPE
100 ).getDeclaringClass() != BufferedWriter.class
102 } catch (Exception ex) {
103 throw new IOException(ex);
106 if (isOverriden || shouldBufferAsTheSequenceIsNotTooBig(csq)) {
107 write(csq.toString());
113 // END: writer.conditionally
116 /** At the end the purpose of BufferedWriter is to buffer writes, this
117 * method is here to decide when it is OK to prefer buffering and when
118 * it is better to delegate directly into the underlaying stream.
120 * @param csq the seqence to evaluate
121 * @return true if buffering from super class should be used
123 private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) {
127 // as buffers are usually bigger than 1024, it makes sense to
128 // pay the penalty of converting the sequence to string, but buffering
130 if (csq.length() < 1024) {
133 // otherwise, just directly write down the char sequence
138 public enum Behaviour {
142 DELEGATE_CONDITIONALLY