Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Always return true, code : " !Arrays.asList(new boolean[1]).stream().anyMatch(Boolean.FALSE::equals) " #10128

Closed
varyuan opened this issue Mar 17, 2023 · 1 comment · Fixed by #10129
Labels
area/Nacos Core kind/bug Category issues or prs related to bug.
Milestone

Comments

@varyuan
Copy link
Contributor

varyuan commented Mar 17, 2023

Describe the bug
Always return true, code : " !Arrays.asList(new boolean[1]).stream().anyMatch(Boolean.FALSE::equals) "
Impact Code:com.alibaba.nacos.core.distributed.raft.NacosStateMachine#adapterToJRaftSnapshot
line 281

总是返回true,"!Arrays.asList(new boolean[1]).stream().anyMatch(Boolean.FALSE::equals)"
影响代码:com.alibaba.nacos.core.distributed.raft.NacosStateMachine#adapterToJRaftSnapshot
第281行

Expected behavior
Check whether all the values in boolean [] are true. If yes, return true, otherwise return false
要实现的结果是 检查boolean[]中是否全为true, 是则返回true,否则返回false

Actually behavior
Always return true
永远返回true

How to Reproduce
Steps to reproduce the behavior:
已提交pr #10129

import java.util.Arrays;

public class SimpleTest {

    public static void main(String[] args) {
        System.out.println(oldCode());
        System.out.println(newCode());
        System.out.println(Arrays.asList(new boolean[5]).size());
        System.out.println(Arrays.asList(new Boolean[5]).size());
    }

    // 要实现的结果是 检查boolean[]中是否全为true, 是则返回true,否则返回false
    // 旧代码, 永远返回true, 不管results里面元素是什么, 因为Arrays.asList(results) 把boolean[] 当成一个元素加入到新数组
    public static boolean oldCode() {
        boolean[] results = new boolean[5];// 元素默认值false
//        Arrays.fill(results, true);
        return !Arrays.asList(results).stream().anyMatch(Boolean.FALSE::equals);
    }

    //
    public static boolean newCode() {
        Boolean[] results = new Boolean[5];
        Arrays.fill(results, Boolean.FALSE);
        return Arrays.stream(results).allMatch(Boolean.TRUE::equals);
    }
}

Desktop (please complete the following information):

  • OS: [e.g. Centos]
  • Version [e.g. nacos-server 1.3.1, nacos-client 1.3.1]
  • Module [e.g. naming/config]
  • SDK [e.g. original, spring-cloud-alibaba-nacos, dubbo]

Additional context
Add any other context about the problem here.

@KomachiSion KomachiSion added kind/bug Category issues or prs related to bug. area/Nacos Core labels Mar 20, 2023
@KomachiSion KomachiSion added this to the 2.3.0 milestone Mar 20, 2023
@KomachiSion KomachiSion linked a pull request Mar 21, 2023 that will close this issue
5 tasks
@Salhazry
Copy link

9bb12bd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/Nacos Core kind/bug Category issues or prs related to bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants