简体   繁体   English

检查Java中2种方法的特定结果的最佳方法

[英]Best way to check specific result of 2 methods in Java

I have the following method 我有以下方法

public Message JavaMethod(String id1, String id2)

In which I need to call a Dao class's method to verify that an user with the provided Id exist, and if it does not, create a message detailing the Id that couldn't be found on the database with the following method: 我需要在其中调用Dao类的方法来验证具有提供的ID的用户是否存在,如果不存在,请使用以下方法创建一条消息,详细说明在数据库中找不到的ID:

createMessage("Message string",Enum.TYPE,IdofMissingUser);

At first I thought of doing it like this: 起初,我想到这样做:

public Message JavaMethod(String id1, String id2) {

        if(Dao.findUser(id1) == null || Dao.findUser(id2) == null){
           return createMessage("Error",Enum.Error,id1);
        }else{
           //do some other stuff
        }
}

But obviously this way I won't know which of the ids has not been found. 但是显然,这样一来,我将不知道尚未找到哪个ID。

So I went ahead and created an ugly if else cycle: 因此,我继续进行操作,并创建了一个丑陋的循环:

public Message JavaMethod(String id1, String id2) {

    if (Dao.findUser(id1) == null) {
        return createMessage("Error", Enum.Error, id1);

    } else if (Dao.findUser(id2) == null) {
        return createMessage("Error", Enum.Error, id2);
    } else {
        // Do stuff after veryfing users exists
        return createMessage("All OK", Enum.OK, messageData);
    }
}

But I'm not feeling really confident that this is the best solution for this basic issue. 但是,我对这是解决此基本问题的最佳解决方案并没有真正的信心。

What would you guys recommend in this case? 在这种情况下,你们会推荐什么?

You could wrap the ids in a list and use a for loop: 您可以将ID包装在列表中并使用for循环:

public Message someMethod(String id1, String id2) {
    for (String id: Arrays.asList(id1, id2)) {
        if (Dao.findUser(id) == null) {
            return createMessage("Error", Enum.Error, id);
        }
    }
    // Do stuff after verifying users exists
    return createMessage("All OK", Enum.OK, messageData);
}

If you're only ever going to have two IDs, you could deal with a shorthand boolean. 如果只有两个ID,可以使用简写布尔值。 Question is whether that makes it less readable though. 问题是,这是否会使它的可读性降低。 Eg 例如

public Message JavaMethod(String id1, String id2) {
        User user1 = Dao.findUser(id1);
        User user2 = Dao.findUser(id2);            
        if(user1  == null || user2  == null){
           return createMessage("Error",Enum.Error,user1 == null ? id1 : id2);
        }else{
           //do some other stuff
        }
}

This also doesn't deal with if both of the IDs were null, for that you could extend it: 如果两个ID都为null,这也不会处理,因为您可以扩展它:

public Message JavaMethod(String id1, String id2) {
        User user1 = Dao.findUser(id1);
        User user2 = Dao.findUser(id2); 
        if(user1  == null || user2  == null){
           return createMessage("Error",Enum.Error,user1  == null && user2  == null? both : user1 == null ? id1 : id2);
        }else{
           //do some other stuff
        }
}

You'd need to define what you would return for the both variable 您需要定义both变量的返回值

More details on the shorthand boolean annotation can be found here 有关速记布尔注释的更多详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM