EnumBusinessException.java

package com.tradecloud.domain.exception;

/**
 * So the concept is to have an exception class that takes an enum instead of a string. This way the enum can be translated at the UI level.
 * The initial design was to have a templated class, with the enum being the template parameter. But a limitation on java prevents this
 */
public abstract class EnumBusinessException extends BaseBusinessException {

    private static final long serialVersionUID = 1L;

    protected Enum<?> exceptionType;

    protected String message;

    public EnumBusinessException(Enum<?> exceptionType) {
        super(exceptionType.toString());
        this.exceptionType = exceptionType;
    }

    public EnumBusinessException(Enum<?> exceptionType, String message) {
        super(exceptionType.toString() + ((message != null && !message.trim().isEmpty()) ? " : " + message : ""));
        this.exceptionType = exceptionType;
        this.message = message;
    }

    public EnumBusinessException(Enum<?> exceptionType, Throwable t) {
        super(exceptionType.toString(), t);
        this.exceptionType = exceptionType;
    }

    /*
    public EnumBusinessException(String message, Throwable t) {
        super(message, t);
    }
    */

    /**
     * Helper method to cast up. Centralising here in case it generates compile warnings in all subclasses.
     */
    protected <E extends Enum<?>> E cast(Class<E> c, Enum<?> e) {
        return c.cast(e);
    }

    // Leaving this abstract so the child classes are forced to do the cast.
    public abstract Enum<?> getExceptionType();
}