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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | package Torello.Java.Additional; import Torello.JavaDoc.JDHeaderBackgroundImg; import Torello.Java.Verbosity; // Used for the JavaDoc '@see' tag, a few lines directly-below import Torello.HTML.Tools.Images.ImageScraper; /** * Another Logging-Helper class. This interface builds on the {@link AppendableSafe} class, and * in this case it meaans that the internal {@code Appendable} that is wrapped by this one is an * instance of {@link AppendableSafe} - <I>rather than the Standard-Java * {@code java.lang.Appendable} interface</I>. * * <BR /><BR />The purpose of the {@link AppendableSafe} class is merely to prevent Java's * Checked-Exception {@code 'IOException'}, and replace it with any number of unchecked * {@code Throwable} options. The purpose of this class is merely to add several convenience * fields ({@link #hasLog} and {@link#level}) so that in the course of writing output-log code, a * programmer is not bogged-down with 'Verbose Log Code' (Logging code that is, itself, extremely * verbose-looking). * * @see ImageScraper * @see AppendableSafe */ @JDHeaderBackgroundImg(EmbedTagFileID={"APPENDABLE_EXTENSION", "APPENDABLE_LOG_JDHBI"}) public class AppendableLog implements Appendable { // ******************************************************************************************** // ******************************************************************************************** // Fields // ******************************************************************************************** // ******************************************************************************************** /** Four space-characters. */ public static final String I4 = " "; /** Eight space-characters. */ public static final String I8 = I4 + I4; /** Twelve space-characters. */ public static final String I12 = I8 + I4; /** Sixteen space-characters. */ public static final String I16 = I8 + I8; /** * The actual log, itself. * * <BR /><BR />This field may be passed and assigned null by the constructor. The user should * be aware to take some care if null log's are allowed in your code. This just means that * there are circumstances where logging is to be "turned off". In such cases, make sure to * write code that chcks whether the log is null before printing to it. (Make sure to avoid * throwing {@code NullPointerException}) * * <BR /><BR />This is an instance of {@link AppendableSafe} because the actual class * {@code java.lang.Appendable} has methods all of which throw {@code IOException}. The * {@link AppendableSafe} class wraps those method calls and blocks the unchecked * {@code IOException's}. * * @see AppendableSafe */ public final AppendableSafe log; /** * Simple way for the user to identify whether or not a null {@code 'appendable'} parameter was * passed to this class Constructor. If null was passed to {@link #log}, then this * {@code boolean} will be false. */ public final boolean hasLog; /** This is nothing more than the enum {@link Verbosity} field {@link Verbosity#level}. */ public final int level; // ******************************************************************************************** // ******************************************************************************************** // Constructor // ******************************************************************************************** // ******************************************************************************************** /** * Construct an instance of this class. The {@code 'appendable'} parameter, if non-null, is * immediately wrapped into an {@link AppendableSafe} instance, and configured to throw * {@link AppendableError} rather than {@code IOException} when / if the internal method throws * any {@code IOException's}. * * @param appendable This may be any instance of {@code java.lang.Appendable}. * * @param verbosity An instance of the {@link Verbosity} enum. If parameter * {@code 'appendable'} is null, then this paramter may also be null. However if * {@code 'appendable'} is non-null, then this may not be null either - or an exception shall * throw. * * @throws NullPointerException If parameter {@code 'appendable'} is non-null, but parameter * {@code 'verbosity'} is null, then a {@code NullPointerException} will throw. */ public AppendableLog(Appendable appendable, Verbosity verbosity) { if (appendable == null) { this.log = null; this.hasLog = false; this.level = 0; } else { this.log = new AppendableSafe(appendable, AppendableSafe.USE_APPENDABLE_ERROR); this.hasLog = true; this.level = verbosity.level; // throws NPE } } /** * Construct an instance of this class. Since there are several ways to configure the internal * {@link AppendableSafe} instance, this constructor allows a user to simply pass an already * built instance of {@code AppendableSafe} as a parameter. * * @param appendableSafe This may be any instance of {@link AppendableSafe}. The primary * purpose of this class is to shunt the {@code IOException} that is thrown by * {@code java.lang.Appendable} methods, and either suppress them, or re-wrap them into an * unchecked-{@code Throwable}. * * <BR /><BR />Being able to leave off the {@code throws IOException} when writing log * code makes the process much easier. * * @param verbosity An instance of the {@link Verbosity} enum. If parameter * {@code 'appendableSafe'} is null, then this paramter may also be null. However if * {@code 'appendableSafe'} is non-null, then this may not be null either - or an exception * shall throw. * * @throws NullPointerException If parameter {@code 'appendableSafe'} is non-null, but * parameter {@code 'verbosity'} is null, then a {@code NullPointerException} will throw. */ public AppendableLog(AppendableSafe appendableSafe, Verbosity verbosity) { if (appendableSafe == null) { this.log = null; this.hasLog = false; this.level = 0; } else { this.log = appendableSafe; this.hasLog = true; this.level = verbosity.level; // throws NPE } } // ******************************************************************************************** // ******************************************************************************************** // java.lang.Appendable Methods // ******************************************************************************************** // ******************************************************************************************** /** * Appends the specified character to this {@code Appendable}. * * <BR /><BR /><SPAN CLASS=CopiedJDK>Description copied from class: * {@code java.lang.Appendable}, <B>JDK 1.8</B></SPAN> * * @param c The character to append * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe append(char c) { return this.log.append(c); } /** * Appends the specified character sequence to this {@code Appendable}. * * <BR /><BR />Depending on which class implements the character sequence {@code 'csq'}, the * entire sequence may not be appended. For instance, if {@code 'csq'} is a * {@code 'CharBuffer'} the subsequence to append is defined by the buffer's position and limit. * * <BR /><BR /><SPAN CLASS=CopiedJDK>Description copied from class: * {@code java.lang.Appendable}, <B>JDK 1.8</B></SPAN> * * @param csq The character sequence to append. If csq is null, then the four characters "null" * are appended to this {@code Appendable}. * * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe append(CharSequence csq) { return this.log.append(csq); } /** * Appends a subsequence of the specified character sequence to this {@code Appendable}. * * <BR /><BR />An invocation of this method of the form {@code out.append(csq, start, end)} * when {@code 'csq'} is not null, behaves in exactly the same way as the invocation: * * <DIV CLASS=LOC>{@code * out.append(csq.subSequence(start, end)) * }</DIV> * * <BR /><BR /><SPAN CLASS=CopiedJDK>Description copied from class: * {@code java.lang.Appendable}, <B>JDK 1.8</B></SPAN> * * @param csq The character sequence from which a subsequence will be appended. If csq is null, * then the four characters "null" are appended to this {@code Appendable}. * * @param start The index of the first character in the subsequence * @param end The index of the character following the last character in the subsequence * * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe append(CharSequence csq, int start, int end) { return this.log.append(csq, start, end); } // ******************************************************************************************** // ******************************************************************************************** // More Appendable Methods // ******************************************************************************************** // ******************************************************************************************** /** * Appends four character spaces of indentation, and then appends {@code String}-parameter * {@code's'}. * * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe appendI4(String s) { return this.log.append(I4).append(s); } /** * Appends eight character spaces of indentation, and then appends {@code String}-parameter * {@code's'}. * * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe appendI8(String s) { return this.log.append(I8).append(s); } /** * Appends twelve character spaces of indentation, and then appends {@code String}-parameter * {@code's'}. * * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe appendI12(String s) { return this.log.append(I12).append(s); } /** * Appends sixteen character spaces of indentation, and then appends {@code String}-parameter * {@code's'}. * * @return A reference to this {@code Appendable}. * * @throws NullPointerException If the {@code AppendableSafe} instance field ({@link #log}) was * passed null during construction of {@code 'this'} instance. */ public AppendableSafe appendI16(String s) { return this.log.append(I16).append(s); } // ******************************************************************************************** // ******************************************************************************************** // Boolean-Test Methods // ******************************************************************************************** // ******************************************************************************************** /** * Quick check that can be used to test whether or not to print to a log. The purpose of an * instance of {@link Verbosity} is that logging information is only printed to an output log * if the user has requested a certain level of verbosity. * * <BR /><BR />This method will return {@code TRUE} if the user has requested logging, and * this instance' internal {@link #level} field is equal to parameter {@code 'level'}. * * @param level An integer that should correspond to the enum {@link Verbosity} internal field * {@link Verbosity#level}. * * @return {@code TRUE} if - * * <BR /><BR /><UL CLASS=JDUL> * <LI><B>{@link #hasLog}</B> is also {@code TRUE}</LI> * <LI><B>{@link #level}</B> equals value passed to parameter {@code 'level'}</LI> * </UL> * * <BR />Returns {@code FALSE} otherwise */ public boolean levelEQ(int level) { if (hasLog) return (this.level == level); return false; } /** * Quick check that can be used to test whether or not to print to a log. The purpose of an * instance of {@link Verbosity} is that logging information is only printed to an output log * if the user has requested a certain level of verbosity. * * <BR /><BR />This method will return {@code TRUE} if the user has requested logging, and * this instance' internal {@link #level} field is greater than or equal to parameter * {@code 'level'}. * * @param level An integer that should correspond to the enum {@link Verbosity} internal field * {@link Verbosity#level}. * * @return {@code TRUE} if - * * <BR /><BR /><UL CLASS=JDUL> * * <LI> Field <B>{@link #hasLog}</B> is also {@code TRUE}</LI> * * <LI> Field <B>{@link #level}</B> is greater than or equal to value passed to parameter * {@code 'level'} * </LI> * </UL> * * <BR />Returns {@code FALSE} otherwise */ public boolean levelGTEQ(int level) { if (hasLog) return (this.level >= level); return false; } } |