1 | package com.ether; |
---|
2 | |
---|
3 | import java.util.Formatter; |
---|
4 | import java.util.Locale; |
---|
5 | |
---|
6 | @SuppressWarnings("serial") |
---|
7 | public class FormattedException extends Exception |
---|
8 | { |
---|
9 | public FormattedException(final Object... parameters) { |
---|
10 | this(Locale.getDefault(), DEFAULT_MESSAGE, DEFAULT_THROWABLE, parameters); |
---|
11 | } |
---|
12 | |
---|
13 | public FormattedException(final Locale locale, final Object... parameters) { |
---|
14 | this(locale, DEFAULT_MESSAGE, DEFAULT_THROWABLE, parameters); |
---|
15 | } |
---|
16 | |
---|
17 | public FormattedException(final String message, final Object... parameters) { |
---|
18 | this(Locale.getDefault(), message, DEFAULT_THROWABLE, parameters); |
---|
19 | } |
---|
20 | |
---|
21 | public FormattedException(final Locale locale, final String message, final Object... parameters) { |
---|
22 | this(locale, message, null, parameters); |
---|
23 | } |
---|
24 | |
---|
25 | public FormattedException(final Throwable throwable, final Object... parameters) { |
---|
26 | this(Locale.getDefault(), throwable.getMessage(), throwable, parameters); |
---|
27 | } |
---|
28 | |
---|
29 | public FormattedException(final String message, final Throwable throwable, final Object... parameters) { |
---|
30 | this(Locale.getDefault(), message, throwable, parameters); |
---|
31 | } |
---|
32 | |
---|
33 | public FormattedException(final Locale locale, final String message, final Throwable throwable, final Object... parameters) { |
---|
34 | super(format(locale, message, parameters), throwable); |
---|
35 | } |
---|
36 | |
---|
37 | private static String format(final Locale locale, final String message, final Object[] parameters) |
---|
38 | { |
---|
39 | final StringBuilder stringBuilder = new StringBuilder(); |
---|
40 | final Formatter formatter = new Formatter(stringBuilder); |
---|
41 | formatter.format(locale, message, parameters); |
---|
42 | return stringBuilder.toString(); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Prefixes the message with code if not null. |
---|
47 | */ |
---|
48 | public static String prefix(final Enum<? extends Code> code, final String message) |
---|
49 | { |
---|
50 | final StringBuffer stringBuffer = new StringBuffer(); |
---|
51 | |
---|
52 | if (code != null) { |
---|
53 | stringBuffer.append(code.toString()); |
---|
54 | stringBuffer.append(" - "); |
---|
55 | } |
---|
56 | |
---|
57 | stringBuffer.append(message); |
---|
58 | return stringBuffer.toString(); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
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. |
---|
64 | * If the subclass is not an enum it won't be recognized by the constructor. |
---|
65 | */ |
---|
66 | public static interface Code |
---|
67 | { |
---|
68 | } |
---|
69 | |
---|
70 | public static String DEFAULT_MESSAGE = "no message"; |
---|
71 | public static String DEFAULT_THROWABLE = null; |
---|
72 | } |
---|