BusinessException
public class BusinessException extends RuntimeException
{
private static final long serialVersionUID = 5009464957746664524L;
private String code;
private String message;
public BusinessException(){}
public BusinessException( String message)
{
super();
this.code = "9999";
this.message = message;
}
public BusinessException(String code, String message)
{
super();
this.code = code;
this.message = message;
}
public BusinessException (LocalError localError)
{
this(localError.getCode(), localError.getMessage());
}
public String getCode()
{
return code;
}
@Override
public String getMessage()
{
return message;
}
public LocalError getLocalError()
{
final BusinessException businessException = this;
return new LocalError() {
@Override
public String getMessage()
{
return businessException.getMessage();
}
//@Override
@Override
public String getCode()
{
return businessException.getCode();
}
};
}
}
LocalError
public interface LocalError
{
String getCode();
String getMessage();
}
Validator
import cn.*.core.BusinessException;
import cn.*.core.LocalError;
import org.apache.commons.lang3.Validate;
import org.springframework.util.StringUtils;
import java.util.Collection;
public class Validator extends Validate
{
public static <T> T notNull(final T object, LocalError localError)
{
if (object == null)
{
throw new BusinessException(localError);
}
return object;
}
public static <T> T isNull(final T object, LocalError localError)
{
if (object != null)
{
throw new BusinessException(localError);
}
return object;
}
public static <T extends CharSequence> T notBlank(final T chars, LocalError localError)
{
if (chars == null)
{
throw new BusinessException(localError);
}
if (!StringUtils.hasLength(chars))
{
throw new BusinessException(localError);
}
return chars;
}
public static <T extends CharSequence> T notEmpty(final T chars, LocalError localError)
{
if (chars == null)
{
throw new BusinessException(localError);
}
if (chars.length() == 0)
{
throw new BusinessException(localError);
}
return chars;
}
public static <T extends Collection> T notEmpty(final T collection, LocalError localError)
{
if (collection == null)
{
throw new BusinessException(localError);
}
if (collection.size() == 0)
{
throw new BusinessException(localError);
}
return collection;
}
public static void isNotZero(final int id, LocalError localError)
{
if (id == 0)
{
throw new BusinessException(localError);
}
}
public static void isTrue(boolean expression, LocalError localError)
{
if (expression == false)
{
throw new BusinessException(localError);
}
}
}