litesoft
@ 948
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / shared / utils / ContextThrowables.java
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 |
package com.temp.shared.utils; import java.io.*; import java.util.*; /** * Collect all the Issues/Throwables for a particular "Context" * * @author georgs */ public class ContextThrowables implements Comparable<ContextThrowables> { private final String context; private final List<Throwable> throwables = new ArrayList<Throwable>(); public ContextThrowables( String context, Throwable... throwables ) { this.context = StringUtils.deNull( context ).trim(); add( throwables ); if ( this.throwables.isEmpty() ) { // Validate that there is at least one Throwable! throw new IllegalArgumentException( "No Throwable" ); } } public void add( Throwable... throwables ) { if ( throwables != null ) { for ( Throwable throwable : throwables ) { if ( throwable != null ) { this.throwables.add( throwable ); } } } } public String getContext() { return context; } public Throwable[] getThrowables() { return throwables.toArray( new Throwable[throwables.size()] ); } @Override public int compareTo( ContextThrowables them ) { return this.context.compareTo( them.context ); } @Override public int hashCode() { return context.hashCode(); } @Override public boolean equals( Object obj ) { return (this == obj) || ((obj instanceof ContextThrowables) && equals( (ContextThrowables) obj )); } public boolean equals( ContextThrowables them ) { return (this == them) || ((them != null) && this.context.equals( them.context )); } @Override public String toString() { return appendTo( new StringBuilder(), 0 ).toString(); } public StringBuilder appendTo( StringBuilder sb, int indent ) { sb.append( StringUtils.indent( indent ) ).append( (context.length() != 0) ? context : "No Context" ); if ( !throwables.isEmpty() ) { sb.append( ':' ); indent++; for ( Throwable throwable : throwables ) { sb.append( '\n' ).append( StringUtils.indent( indent ) ).append( StringUtils.simpleClassNameFor( throwable ) ).append( ": " ) .append( throwable.getMessage() ); } } return sb; } /** * @param stream - note in GWT only println Object & String are implemented */ public void dumpToAsHTML( PrintStream stream, boolean fullStackTrace ) { boolean anyThrowables = !throwables.isEmpty(); String header = (context.length() != 0) ? context : "No Context"; if ( !anyThrowables ) { stream.println( header ); } else { stream.println( header + ":" ); stream.println( "<ul>" ); for ( Throwable throwable : throwables ) { if ( fullStackTrace ) { throwable.printStackTrace( stream ); } else { String message = throwable.getMessage(); if ( message != null ) { message = ": " + message; } stream.println( ObjectUtils.getSimpleClassName( throwable ) + message ); } stream.println( "" ); } stream.println( "</ul>" ); } } } |