[...]
On 12/23/2015 03:38 AM, luc wrote:
interface ExceptionLocalizer { /** Localize an exception message. * @param locale locale to use * @param me exception to localize * @return localized message for the exception */ String localize(Locale locale, MathException me); } and having ExceptionFactory hold a user-provided implementation of this interface? public class ExceptionFactory { private static ExceptionLocalizer localizer = new NoOpLocalizer(); public static setLocalizer(ExceptionLocalizer l) { localizer = l; } public static String localize(Locale locale, MathException me) { return localizer.localize(locale, me); } /** Default implementation of the localizer that does nothing. */ private static class NoOpLocalizer implements ExceptionLocalizer { /** {@inheritDoc} */ @Override public String localize(MathException me) { return me.getMessage(); } } } and MathException could implement both getLocalizedMessage() and even getMessage(Locale) by delegating to the user code: public class MathException { public String getLocalizedMessage() { return ExceptionFactory.localize(Locale.getDefault(), this); } public String getMessage(Locale locale) { return ExceptionFactory.localize(locale, this); } ... }
Nice!
One thing that would be nice would be that in addition to the get method, MathException also provides a getKeys to retrieve all keys and a getType to retrieve the type.
Just added getKeys() (Line 48): https://github.com/firefly-math/firefly-math-exceptions/blob/master/src/main/java/com/fireflysemantics/math/exception/MathException.java getType() already there (Line 29 - @Getter annotation produces the getter) Also added getMethodName() and getClassName() to get the source of the exception. Since there is only a single exception there is no need to unroll it to get the root cause. [...] Cheers, Ole --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org