1 6 21 22 package de.schlichtherle.io; 23 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.PrintStream ; 27 import java.io.PrintWriter ; 28 import java.util.Comparator ; 29 30 67 public class ChainableIOException extends IOException implements Cloneable { 68 69 75 static final Comparator PRIORITY_COMP = new Comparator () { 77 public int compare(Object o1, Object o2) { 78 final int cmp = ((ChainableIOException) o1).getPriority() 79 - ((ChainableIOException) o2).getPriority(); 80 return cmp != 0 ? cmp : APPEARANCE_COMP.compare(o1, o2); 81 } 82 }; 83 84 88 static final Comparator APPEARANCE_COMP = new Comparator () { 90 public int compare(Object o1, Object o2) { 91 return ((ChainableIOException) o1).getAppearance() 92 - ((ChainableIOException) o2).getAppearance(); 93 } 94 }; 95 96 private static int maxPrintExceptions = 5; 97 98 102 public static int getMaxPrintExceptions() { 103 return maxPrintExceptions; 104 } 105 106 110 public static void setMaxPrintExceptions(int maxPrintExcepions) { 111 ChainableIOException.maxPrintExceptions = maxPrintExcepions; 112 } 113 114 124 private ChainableIOException prior; 125 126 private final int appearance; 127 128 int maxAppearance; 129 130 138 public ChainableIOException(ChainableIOException priorException) { 139 this(priorException, null, null); 140 } 141 142 152 public ChainableIOException( 153 ChainableIOException priorException, 154 String message) { 155 this(priorException, message, null); 156 } 157 158 171 public ChainableIOException( 172 ChainableIOException priorException, 173 IOException cause) { 174 this(priorException, null, cause); 175 } 176 177 191 public ChainableIOException( 192 ChainableIOException priorException, 193 String message, 194 IOException cause) { 195 super(message); 196 this.prior = priorException; 197 if (cause != null) 198 initCause(cause); 199 if (priorException != null) 200 maxAppearance = priorException.maxAppearance + 1; 201 else 202 maxAppearance = 0; 203 appearance = maxAppearance; 204 } 205 206 209 public Object clone() { 210 try { 211 return super.clone(); 212 } catch (CloneNotSupportedException cannotHappen) { 213 throw new AssertionError (cannotHappen); 214 } 215 } 216 217 224 public int getPriority() { 225 return 0; 226 } 227 228 231 public final int getAppearance() { 232 return appearance; 233 } 234 235 239 public ChainableIOException getPrior() { 240 return prior; 241 } 242 243 256 public ChainableIOException sortPriority() { 257 return sort(PRIORITY_COMP); 258 } 259 260 271 public ChainableIOException sortAppearance() { 272 return sort(APPEARANCE_COMP); 273 } 274 275 private ChainableIOException sort(final Comparator comp) { 276 if (prior != null) { 277 final ChainableIOException sortedPrior = prior.sort(comp); 278 if (sortedPrior == prior && comp.compare(this, prior) >= 0) 279 return this; 280 else 281 return sortedPrior.insert((ChainableIOException) clone(), comp); 282 } else { 283 return this; 284 } 285 } 286 287 private ChainableIOException insert( 288 final ChainableIOException element, 289 final Comparator comp) { 290 if (comp.compare(element, this) >= 0) { 291 element.prior = this; 293 element.maxAppearance = Math.max(element.appearance, maxAppearance); 294 return element; 295 } else { 296 final ChainableIOException clone 298 = (ChainableIOException) clone(); 299 if (prior != null) { 300 clone.prior = prior.insert(element, comp); 301 clone.maxAppearance = Math.max(clone.appearance, clone.prior.maxAppearance); 302 } else { 303 element.prior = null; 304 clone.prior = element; 305 clone.maxAppearance = element.maxAppearance; 306 } 307 return clone; 308 } 309 } 310 311 325 public void printStackTrace(PrintStream s) { 326 printStackTrace(s, getMaxPrintExceptions()); 327 } 328 329 343 public void printStackTrace(PrintStream s, int maxExceptions) { 344 maxExceptions--; 345 346 if (prior != null) { 347 if (maxExceptions > 0) { 348 prior.printStackTrace(s, maxExceptions); 349 s.println("Followed, but not caused by:"); 350 } else { 351 s.println("(Omitting " + prior.getNumExceptions() + " exception(s) at the start of this list)"); 352 } 353 } 354 355 super.printStackTrace(s); 356 } 357 358 private int getNumExceptions() { 359 return prior != null ? prior.getNumExceptions() + 1 : 1; 360 } 361 362 376 public void printStackTrace(PrintWriter s) { 377 printStackTrace(s, getMaxPrintExceptions()); 378 } 379 380 394 public void printStackTrace(PrintWriter s, int maxExceptions) { 395 maxExceptions--; 396 397 if (prior != null) { 398 if (maxExceptions > 0) { 399 prior.printStackTrace(s, maxExceptions); 400 s.println("Followed, but not caused by:"); 401 } else { 402 s.println("(Omitting " + prior.getNumExceptions() + " exception(s) at the start of this list)"); 403 } 404 } 405 406 super.printStackTrace(s); 407 } 408 } 409
| Popular Tags
|