1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | package Torello.Java.Additional; import Torello.JavaDoc.JDHeaderBackgroundImg; import Torello.Java.UnreachableError; // Used for the JavaDoc '@see' tag, a few lines directly-below import Torello.Java.OSCommands; import java.io.IOException; /** * Builds a composite {@code java.lang.Appendable} using up to <B STYLE='color: red;'>three</B> * independent input-parameter {@code Appendable's}. Character data appended to an instance of * {@code TriAppendable} will, in turn, be appended to any of the input-{@code Appendable's} which * have been provided to this class' constructor and are non-null. * * <BR /><BR />Passing null input-{@code Appendable's} <B STYLE='color: red;'><I>will not</I></B> * cause this class to fail, and <B STYLE='color: red;'><I>will not</I></B> generate * {@code NullPointerException's}. One of the primary values of this class is that the chunk of * code that tests for a "Null Log" is done inside this class. * * <BR /><BR />The meaning of a "Null Log" is just that a user has decided against logging a tool * or utility's output to any logger, and has passed 'null' to one of the log references in his * code. * * @see OSCommands */ @JDHeaderBackgroundImg (EmbedTagFileID={"APPENDABLE_EXTENSION", "TRI_APPENDABLE_JDHBI", "BI_TRI_BOTH_JDHBI"}) public class TriAppendable implements Appendable { private final Appendable appendable; /** * Creates an instance of this class, using up to three input {@code Appendable's}. * * @param a Any {@code java.lang.Appendable}. This parameter may be null, and if it is it will * be ignored. Passing null here, will not generated a {@code NullPointerException} * * @param b Any {@code java.lang.Appendable}. This parameter may be null, and if it is it will * be ignored. Passing null here, will not generated a {@code NullPointerException} * * @param c Any {@code java.lang.Appendable}. This parameter may be null, and if it is it will * be ignored. Passing null here, will not generated a {@code NullPointerException} */ public TriAppendable(Appendable a, Appendable b, Appendable c) { // Minor coding trick. Pretend this is a base-2 number, with three bits. I think this // improves efficiency a tiny bit, since it isn't comparing the nulls over and over again. // Here it checks the references for nulls only once. int theSum = ((a == null) ? 0 : 4) + ((b == null) ? 0 : 2) + ((c == null) ? 0 : 1); // if ((a == null) && (b == null) && (c == null)) this.appendable = new Appendable() if (theSum == 0) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { return this; } public Appendable append(CharSequence cs) throws IOException { return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { return this; } }; // else if ((a == null) && (b == null) && (c != null)) this.appendable = new Appendable() else if (theSum == 1) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { c.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { c.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { c.append(cs, start, end); return this; } }; // else if ((a == null) && (b != null) && (c == null)) this.appendable = new Appendable() else if (theSum == 2) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { b.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { b.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { b.append(cs, start, end); return this; } }; // else if ((a == null) && (b != null) && (c != null)) this.appendable = new Appendable() else if (theSum == 3) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { b.append(ch); c.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { b.append(cs); c.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { b.append(cs, start, end); c.append(cs, start, end); return this; } }; // else if ((a != null) && (b == null) && (c == null)) this.appendable = new Appendable() else if (theSum == 4) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { a.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { a.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { a.append(cs, start, end); return this; } }; // else if ((a != null) && (b == null) && (c != null)) this.appendable = new Appendable() else if (theSum == 5) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { a.append(ch); c.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { a.append(cs); c.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { a.append(cs, start, end); c.append(cs, start, end); return this; } }; // else if ((a != null) && (b != null) && (c == null)) this.appendable = new Appendable() else if (theSum == 6) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { a.append(ch); b.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { a.append(cs); b.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { a.append(cs, start, end); b.append(cs, start, end); return this; } }; // else if ((a != null) && (b != null) && (c != null)) this.appendable = new Appendable() else if (theSum == 7) this.appendable = new Appendable() { public Appendable append(char ch) throws IOException { a.append(ch); b.append(ch); c.append(ch); return this; } public Appendable append(CharSequence cs) throws IOException { a.append(cs); b.append(cs); c.append(cs); return this; } public Appendable append(CharSequence cs, int start, int end) throws IOException { a.append(cs, start, end); b.append(cs, start, end); c.append(cs, start, end); return this; } }; // This appears (on cursory inspection) very "unreachable". However the java compiler // doesn't seem to like it! (javac complains without this last line) else throw new UnreachableError(); } /** * Logs a {@code char} (input-parameter {@code 'ch'}) to any / all non-null * {@code Appendable's} that were passed to this class' constructor. * * @param ch Any Java Character * * @throws IOException Throws if any of the underlying {@code Appendable's} throw * {@code IOException} upon having their corresponding {@code append()}-method invoked. */ public Appendable append(char ch) throws IOException { return this.appendable.append(ch); } /** * Logs a {@code CharSequence} (input-parameter {@code 'cs'}) to any / all non-null * {@code Appendable's} that were passed to this class' constructor. * * @param cs Any Java {@code CharSequence} * * @throws IOException Throws if any of the underlying {@code Appendable's} throw * {@code IOException} upon having their corresponding {@code append()}-method invoked. */ public Appendable append(CharSequence cs) throws IOException { return this.appendable.append(cs); } /** * Logs a partial / substring {@code CharSequence} (input-parameter {@code 'cs'}) to any / all * non-null {@code Appendable's} that were passed to this class' constructor. * * @param cs Any Java {@code CharSequence} * * @param start Position within {@code 'cs'} of the {@code substring's} first character to be * appended * * @param end Position within {@code 'cs'} of the {@code substring's} last character to be * appended * * @throws IOException Throws if any of the underlying {@code Appendable's} throw * {@code IOException} upon having their corresponding {@code append()}-method invoked. */ public Appendable append(CharSequence cs, int start, int end) throws IOException { return this.appendable.append(cs, start, end); } } |