Changeset 402 for tapas/common
- Timestamp:
- 03/08/12 17:20:11 (12 years ago)
- Location:
- tapas/common
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tapas/common/interface/com/ether/FormattedException.java
r376 r402 5 5 6 6 @SuppressWarnings("serial") 7 public class FormattedException extends Exception 7 public class FormattedException 8 extends Exception 8 9 { 9 public FormattedException(final Object... parameters) { 10 this(Locale.getDefault(), DEFAULT_MESSAGE, DEFAULT_THROWABLE, parameters); 10 public FormattedException( final Object... parameters ) 11 { 12 this( Locale.getDefault(), DEFAULT_MESSAGE, DEFAULT_THROWABLE, parameters ); 11 13 } 12 14 13 public FormattedException(final Locale locale, final Object... parameters) { 14 this(locale, DEFAULT_MESSAGE, DEFAULT_THROWABLE, parameters); 15 public FormattedException( final Throwable throwable, final Object... parameters ) 16 { 17 this( Locale.getDefault(), throwable.getMessage(), throwable, parameters ); 15 18 } 16 19 17 public FormattedException(final String message, final Object... parameters) { 18 this(Locale.getDefault(), message, DEFAULT_THROWABLE, parameters); 20 public FormattedException( final Locale locale, final String message, final Object... parameters ) 21 { 22 this( locale, message, null, parameters ); 19 23 } 20 24 21 public FormattedException(final Locale locale, final String message, final Object... parameters) { 22 this(locale, message, null, parameters); 25 public FormattedException( final String message, final Throwable throwable, final Object... parameters ) 26 { 27 this( Locale.getDefault(), message, throwable, parameters ); 23 28 } 24 29 25 public FormattedException(final Throwable throwable, final Object... parameters) { 26 this(Locale.getDefault(), throwable.getMessage(), throwable, parameters); 30 public FormattedException( final Locale locale, final String message, final Throwable throwable, final Object... parameters ) 31 { 32 super( format( locale, message, parameters ), throwable ); 27 33 } 28 34 29 public FormattedException(final String message, final Throwable throwable, final Object... parameters) { 30 this(Locale.getDefault(), message, throwable, parameters); 35 public FormattedException( final Enum<? extends Code> code, final String message, final Throwable throwable, 36 final Object... parameters ) 37 { 38 this( prefix( code, message ), throwable, parameters ); 31 39 } 32 40 33 public FormattedException(final Locale locale, final String message, final Throwable throwable, final Object... parameters) { 34 super(format(locale, message, parameters), throwable); 41 private static String format( final Locale locale, final String message, final Object[] parameters ) 42 { 43 final StringBuilder stringBuilder = new StringBuilder(); 44 final Formatter formatter = new Formatter( stringBuilder ); 45 formatter.format( locale, message, parameters ); 46 return stringBuilder.toString(); 35 47 } 36 48 37 p rivate static String format(final Locale locale, final String message, final Object[] parameters)49 public static String formatThrowable( final Enum<? extends Code> code, final String message, final Throwable throwable ) 38 50 { 39 final StringBuilder stringBuilder = new StringBuilder();40 final Formatter formatter = new Formatter(stringBuilder);41 formatter.format(locale, message, parameters);42 return stringBuilder.toString();51 if( throwable.equals( EXCEPTION_THROWABLE ) ) 52 return code.name(); 53 else 54 return prefix( code, message ); 43 55 } 44 56 … … 46 58 * Prefixes the message with code if not null. 47 59 */ 48 public static String prefix( final Enum<? extends Code> code, final String message)60 public static String prefix( final Enum<? extends Code> code, final String message ) 49 61 { 50 62 final StringBuffer stringBuffer = new StringBuffer(); 51 63 52 if (code != null) { 53 stringBuffer.append(code.toString()); 54 stringBuffer.append(" - "); 55 } 64 if( code != null ) 65 { 66 stringBuffer.append( code.toString() ); 67 stringBuffer.append( " - " ); 68 } 56 69 57 stringBuffer.append(message); 58 return stringBuffer.toString(); 70 stringBuffer.append( message ); 71 return stringBuffer.toString(); 72 } 73 74 public static Throwable getExceptionThrowable() 75 { 76 return EXCEPTION_THROWABLE; 59 77 } 60 78 61 79 /** 62 * The error code, it identifies the type of error. 63 * The derived classes must creates their own code as enum and implements this interface. 80 * The error code, it identifies the type of error. 81 * The derived classes must creates their own code as enum and implements this interface. 64 82 * If the subclass is not an enum it won't be recognized by the constructor. 65 83 */ … … 70 88 public static String DEFAULT_MESSAGE = "no message"; 71 89 public static String DEFAULT_THROWABLE = null; 90 private static final Throwable EXCEPTION_THROWABLE = new Throwable( "EXCEPTION_THROWABLE" ); 72 91 }
Note: See TracChangeset
for help on using the changeset viewer.