Java实现校验工具类Validation

文章目录


前言

使用Java实现一个校验工具类,可以简洁判断入口参数是否有问题,包含:快速失败、全部失败两种机制。

代码

import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

@Getter
@ToString
public class Validation {
    private final boolean fail;
    private final String failMessage;

    /**
     * 私有构造器,屏蔽外部使用 new 创建对象
     *
     * @param builder builder模式
     */
    private Validation(Builder builder) {
        this.fail = builder.fail;
        this.failMessage = builder.failMessage == null ? null : builder.failMessage.toString();
    }

    public boolean isSuccess() {
        return !fail;
    }

    /**
     * 快速失败:只要有一个条件失败就返回
     *
     * @return FastFailBuilder
     */
    public static FastFailBuilder fastFailBuilder() {
        return new FastFailBuilder();
    }

    /**
     * 全部失败:返回所有失败条件的消息
     *
     * @return AllFailBuilder
     */
    public static AllFailBuilder allFailBuilder() {
        return new AllFailBuilder();
    }


    private static class Builder {
        boolean fail;
        StringBuilder failMessage;
    }

    public static class FastFailBuilder extends Builder {
        private FastFailBuilder() {
        }

        public FastFailBuilder failIf(boolean condition, String failMessage) {
            if (this.fail) {
                return this;
            }
            if (condition) {
                this.fail = true;
                this.failMessage = new StringBuilder(failMessage);
            }
            return this;
        }

        public Validation result() {
            return new Validation(this);
        }
    }

    public static class AllFailBuilder extends Builder {
        private AllFailBuilder() {
        }

        public AllFailBuilder failIf(boolean condition, String failMessage) {
            if (condition) {
                this.fail = true;
                if (StringUtils.isEmpty(this.failMessage)) {
                    this.failMessage = new StringBuilder(failMessage);
                } else {
                    this.failMessage.append(" && ").append(failMessage);
                }
            }
            return this;
        }

        public Validation result() {
            return new Validation(this);
        }
    }
}

使用示例如下:

import com.idiudiu.common.constant.Common;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

@Data
public class LoginRequest {
    private String username;
    private String password; // md5加密后的密码

    public Validation checkParam() {
        return Validation.allFailBuilder()
                .failIf(StringUtils.isBlank(username), "the username is blank")
                .failIf(StringUtils.isBlank(password), "the password is blank")
                .result();
    }
}
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值