创建返回布尔值并迭代for-each循环的方法数组

时间:2016-03-25 11:39:05

标签: java arraylist methods

好的,我有一批方法返回boolean true/false个值。

private void saveChangesOnEditButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
        updateMainTabsAccess();
        updateUserPaymentTabPermissions();
        updateUserRegistrationTabPermissions();
        updateUserStudentsTabPermissions();
        updateUserFacultyTabPermissions();
        updateUserHomePermissions(); //saves any update made on existing user settings/permissions
        updateUserInformation(); // sasve any update made on existing user information such as username
    }  

我想知道我是否有可能检查每种方法'通过for-each循环返回值。

我正在考虑创建一个private boolean isUpdateSuccessful()方法。 比如说,

private boolean isUpdateSuccessful(){
    Boolean a = updateMainTabsAccess();
    Boolean b = updateUserPaymentTabPermissions();
    //........so on....
    Boolean result = (a && b &&...)
    return result;
}

问题是,我不知道是否可以将它们放入arraylist或组件数组中,如

ArrayList<Boolean> listOfMethods = new ArrayList<Boolean>(method1,method2..);

然后我可以通过for-each循环检查每个

for(Boolean b:listOfMethods){
    Boolean successful=true;
    successful =  (successful && b)
}

我的问题是:

1.。)如何提取这些方法的返回值并使用方法初始化Arraylist

2。)使用for-each循环,我有什么可能尝试做的事情?我没有,那么你建议我做什么?

我很感激任何答案或建议。我只是想检查每个方法是否成功。我想过使用?1:0:

提前致谢。

6 个答案:

答案 0 :(得分:2)

如果我是你,我会这样做。只是一个示例代码:

private void saveChangesOnEditButtonActionPerformed(java.awt.event.ActionEvent evt) {
    if (updateMainTabsAccess()) {
        if (updateUserPaymentTabPermissions()) {
            if (updateUserRegistrationTabPermissions()) {
                ...
            } else {
                // error on update registration
            }
        } else {
            // error on update payment
        }
    }

具有以上风格:

  • 当前一个方法失败时,你不会执行其他方法。
  • 可以为每个错误提供详细的错误消息。
  • 您无需主要收集和迭代。

答案 1 :(得分:1)

为什么不使用Stream来检查结果:

Stream.<Boolean>of(updateMainTabsAccess(),
    updateUserPaymentTabPermissions(),
    updateUserRegistrationTabPermissions(),
    updateUserStudentsTabPermissions(),
    updateUserFacultyTabPermissions(),
    updateUserHomePermissions(),
    updateUserInformation()).allMatch(b -> b);

这样你就可以摆脱短路评估,也不需要为每种方法创建方法参考。

方法参考

List<Supplier<Boolean>> methods = Arrays.asList(this::updateMainTabsAccess,
                                                this::updateUserPaymentTabPermissions,
                                                ...
);

for (Supplier<Boolean> supplier : methods) {
    boolean methodResult = supplier.get();
    ...
}

这虽然很难被视为改善......

答案 2 :(得分:1)

这将找到你的类中的所有方法,它是在逐个自动调用方法并将响应存储到成功变量后返回布尔值

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        Test test = new Test();
        Class c = test.getClass();      
        boolean successful = true; 
        for (Method method : c.getDeclaredMethods()) {
                if (method.getReturnType().toString().equals("boolean")) {
                    try {
                        String mname = method.getName();
                        Object o = method.invoke(test, null);
                        System.out.format("%s() returned %b%n", mname, (Boolean) o);
                        successful =  successful && (Boolean) o;

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        }       
        System.out.println("final answer : " + successful);
    }

    public boolean a() {
        return true;
    }

    public boolean b() {
        return false;
    }

    public boolean c() {
        return false;
    }

}

希望对你有所帮助。

答案 3 :(得分:0)

如果你想要执行每一个方法并检查每个方法是否都可以简单地编写

boolean success = updateMainTabsAccess() &
    updateUserPaymentTabPermissions() &
    updateUserRegistrationTabPermissions() &
    updateUserStudentsTabPermissions() &
    updateUserFacultyTabPermissions() &
    updateUserHomePermissions() &
    updateUserInformation(); 

答案 4 :(得分:0)

您已收到一些答案。 如果你使用java 8,那么Fabian是一个很好的。

但要直接回答你的观点

  

1.。)如何提取这些方法的返回值并使用方法初始化Arraylist。

    ArrayList<Boolean> resultsList = new ArrayList<Boolean>();

    resultsList.add(updateMainTabsAccess());
    ...
  

2。)使用for-each循环,我有什么可能尝试做的事情?我没有,那么你建议我做什么?

    boolean res = true;

    for (Boolean singleResult : resultsList) {
        res = res && singleResult;
    }

答案 5 :(得分:0)

当Java 8没有引入Lambdas时,这是实现目标的旧式方法。

public class TestMethodsListCall {

    public abstract class Checker {
        public abstract boolean check();
    }

    public static void main(String[] args) {
        new TestMethodsListCall();
    }

    public TestMethodsListCall() {
        final TestMethodsListCall that = this;
        List<Checker> checkers = Arrays.asList( //
                new Checker() { public boolean check() { return that.methodA(); } }, //
                new Checker() { public boolean check() { return that.methodB(); } }  //
                // , ...
        );

        boolean res = true;
        for (Checker c : checkers) {
            res = res & c.check();
            if (!res) {
                // Break, display some message or all together
            }
        }
    }

    public boolean methodA() {
        return true;
    }

    public boolean methodB() {
        return false;
    }
}
相关问题