Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法简化源代码?_Java_Spring_Spring Boot - Fatal编程技术网

Java 有没有办法简化源代码?

Java 有没有办法简化源代码?,java,spring,spring-boot,Java,Spring,Spring Boot,我目前正在使用spring boot实现公告板API。 我将向您展示的源代码场景是关于公告板或注释修改 修改前,双方都需要在评论或公告栏输入时提交密码 因此,我只需使用'isBoard'作为输入:隐藏值,在'boardRepository'中执行'findById',并将其与密码编码器的匹配函数进行比较,然后更新布尔值 但是,下面的代码显示相同的逻辑被复制和使用。 有没有办法让这更简单 @Transactional public boolean tryToUpdateArticleOrRepl

我目前正在使用spring boot实现公告板API。 我将向您展示的源代码场景是关于公告板或注释修改

修改前,双方都需要在评论或公告栏输入时提交密码

因此,我只需使用'isBoard'作为输入:隐藏值,在'boardRepository'中执行'findById',并将其与密码编码器的匹配函数进行比较,然后更新布尔值

但是,下面的代码显示相同的逻辑被复制和使用。 有没有办法让这更简单

 @Transactional
public boolean tryToUpdateArticleOrReply(Long id, String password, boolean isBoard) throws ApiException {
    AtomicBoolean result = new AtomicBoolean(false);

    if(isBoard) {
         boardRepository.findById(id).ifPresent(
                board -> {
                    result.compareAndSet(passwordEncoder.matches(board.getUserPass(), password), true);
                }
         );
    } else {
        replyRepository.findById(id).ifPresent(
                reply -> {
                    result.compareAndSet(passwordEncoder.matches(reply.getUserPass(), password), true);
                }
        );
    }
    if(result.get())
      return result.get();

    throw new ApiException("INVALID_USER_PASS", "you submitted invaild password.", new ApiExceptionData().add("user_pass", password));
}

如果
findById
getUserPass
方法来自接口(并且
findById
方法返回
getUserPass
来自的接口),那么您可以简单地执行以下操作:

private boolean check(FindByIdInterface repository,
                      Long id,
                      String password) {
    return repository.findById(id)
            .map(result -> 
                passwordEncoder.matches(
                    result.getUserPass(), password))
            .orElse(false); 
}
然后根据
isBoard
变量,在选择要传递的
repository
的位置调用它<代码>isBoard?boardRepository:replyRepository


如果它们不是来自共享接口,那么您可以使用java的一些功能接口有效地“假装”它们来自共享接口

/* R = repository type, T = return type of 'findById' */
private <R, T> check(Function<R, Optional<T>> getByIdMapper,
                     Function<T, String> getUserPassMapper,
                     R repository,
                     Long id,
                     String password) {
    return getByIdMapper.apply(repository)
            .map(result ->
                passwordEncoder.matches(
                    getUserPassMapper.apply(result),
                    password))
            .orElse(false);
}


注意,在这两种情况下都不需要使用原子布尔。

您可以这样做:

@Transactional
public boolean tryToUpdateArticleOrReply(Long id, String password, boolean isBoard) throws ApiException {
    Optional<String> optUserPass;
    if (isBoard) {
        optUserPass = boardRepository.findById(id).map(Board::getUserPass);
    else {
        optUserPass = replyRepository.findById(id).map(Reply::getUserPass);
    }
    if (optUserPass.isPresent() && passwordEncoder.matches(password, optUserPass.get()))
        return true;
    throw new ApiException("INVALID_USER_PASS", "you submitted invaild password.", new ApiExceptionData().add("user_pass", password));
}
@Transactional
公共布尔tryToUpdateArticleOrReply(长id、字符串密码、布尔isBoard)引发异常{
可选optUserPass;
如果(isBoard){
optUserPass=boardRepository.findById(id).map(Board::getUserPass);
否则{
optUserPass=replyRepository.findById(id).map(Reply::getUserPass);
}
if(optUserPass.isPresent()&&passwordEncoder.matches(password,optUserPass.get()))
返回true;
抛出新的ApiException(“无效的用户密码”,“您提交了invaild密码”)、新的ApiExceptionData().add(“用户密码”);
}
注1:方法名称似乎有点不合适

注2:当只能返回
true
时,为什么有
boolean
返回类型

@Transactional
public boolean tryToUpdateArticleOrReply(Long id, String password, boolean isBoard) throws ApiException {
    Optional<String> optUserPass;
    if (isBoard) {
        optUserPass = boardRepository.findById(id).map(Board::getUserPass);
    else {
        optUserPass = replyRepository.findById(id).map(Reply::getUserPass);
    }
    if (optUserPass.isPresent() && passwordEncoder.matches(password, optUserPass.get()))
        return true;
    throw new ApiException("INVALID_USER_PASS", "you submitted invaild password.", new ApiExceptionData().add("user_pass", password));
}