Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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_Validation_If Statement - Fatal编程技术网

Java “如何消除一切”;如果;来自我的验证器类的语句

Java “如何消除一切”;如果;来自我的验证器类的语句,java,validation,if-statement,Java,Validation,If Statement,有没有一种方法可以消除这个类中的所有“if”语句,并且仍然保持完全相同的功能 到目前为止,我通过创建两个额外的函数来简化代码:isNameValid和isPhoneValid,但我需要一种方法来摆脱所有的“if”语句 public class ClientValidator implements Validator<Client> { @Override public void validate(Client entity) throws ValidatorExcep

有没有一种方法可以消除这个类中的所有“if”语句,并且仍然保持完全相同的功能

到目前为止,我通过创建两个额外的函数来简化代码:isNameValid和isPhoneValid,但我需要一种方法来摆脱所有的“if”语句

public class ClientValidator implements Validator<Client> {
    @Override
    public void validate(Client entity) throws ValidatorException {
        if(!isNameValid(entity.getName())){
            throw new ClientException("Invalid name!");
        }
        if(!isPhoneValid(entity.getPhone())){
            throw new ClientException("Invalid phone number!");
        }
    }

    private boolean isNameValid(String name) {
        return name.length() > 1 && name.length() < 100;
    }

    private boolean isPhoneValid(String phone) {
        try {
            Long.parseLong(phone);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }
}
公共类ClientValidator实现验证器{
@凌驾
public void validate(客户端实体)引发Validator异常{
如果(!isNameValid(entity.getName())){
抛出新的ClientException(“无效名称!”);
}
如果(!isPhoneValid(entity.getPhone())){
抛出新的ClientException(“无效电话号码!”);
}
}
私有布尔值isNameValid(字符串名称){
返回name.length()>1&&name.length()<100;
}
专用布尔值isPhoneValid(字符串电话){
试一试{
Long.parseLong(电话);
}捕获(数字格式){
返回false;
}
返回true;
}
}
有没有一种方法可以消除这个类中的所有“if”语句,并且仍然保持完全相同的功能

对。这是一种黑客行为,但是
如果
不是唯一的流控制。我看到的最简单的方法是,使用相同逻辑的
while
循环。像

@Override
public void validate(Client entity) throws ValidatorException {
    while (!isNameValid(entity.getName())) {
        throw new ClientException("Invalid name!");         
    }
    while (!isPhoneValid(entity.getPhone())) {
        throw new ClientException("Invalid phone number!");
    }
}
您还可以使用
switch
语句,如

@Override
public void validate(Client entity) throws ValidatorException {
    switch (isNameValid(entity.getName())) {
    case false:
        throw new ClientException("Invalid name!");                         
    }

    switch (isPhoneValid(entity.getPhone())) {
    case false:
        throw new ClientException("Invalid phone number!");
    }
}
有没有一种方法可以消除这个类中的所有“if”语句,并且仍然保持完全相同的功能

对。这是一种黑客行为,但是
如果
不是唯一的流控制。我看到的最简单的方法是,使用相同逻辑的
while
循环。像

@Override
public void validate(Client entity) throws ValidatorException {
    while (!isNameValid(entity.getName())) {
        throw new ClientException("Invalid name!");         
    }
    while (!isPhoneValid(entity.getPhone())) {
        throw new ClientException("Invalid phone number!");
    }
}
您还可以使用
switch
语句,如

@Override
public void validate(Client entity) throws ValidatorException {
    switch (isNameValid(entity.getName())) {
    case false:
        throw new ClientException("Invalid name!");                         
    }

    switch (isPhoneValid(entity.getPhone())) {
    case false:
        throw new ClientException("Invalid phone number!");
    }
}

您可以尝试使用optionals并对方法进行筛选,但您会错过特定于原因的异常:

Optional
.of(entity)
.filter(entity -> isNameValid(entity.getName())
.filter(entity -> isPhoneValid(entity.getPhone())
.orElseThrow(() -> new ClientException("Wrong client data"));

您可以尝试使用optionals并对方法进行筛选,但您会错过特定于原因的异常:

Optional
.of(entity)
.filter(entity -> isNameValid(entity.getName())
.filter(entity -> isPhoneValid(entity.getPhone())
.orElseThrow(() -> new ClientException("Wrong client data"));
那么这个呢:

@Override
public void validate(String entity) throws ClientException {
    String message = !isNameValid(entity.getName()) ? "Invalid name!"
            : !isPhoneValid(entity.getPhone()) ? "Invalid phone number!" : "";
    Stream.of(message).filter(m -> m.isEmpty()).findAny()
            .orElseThrow(() -> new ClientException (message));
}
那么这个呢:

@Override
public void validate(String entity) throws ClientException {
    String message = !isNameValid(entity.getName()) ? "Invalid name!"
            : !isPhoneValid(entity.getPhone()) ? "Invalid phone number!" : "";
    Stream.of(message).filter(m -> m.isEmpty()).findAny()
            .orElseThrow(() -> new ClientException (message));
}

我能想到一些肮脏的把戏,比如

public void validate(Client entity) throws ValidatorException {

    try {
        int len = entity.getName().length();
        int isshort = 1 / len;
        int islong = 1 / max (0, 100- length);

    } catch (Exception e) {
        throw new ClientException("Invalid name!");
    }
   try {
        Long.parseLong(entity.getPhone());
    } catch (NumberFormatException e) {
        throw new ClientException("Invalid phone number!");
    }
}

因此,如果需要的话,我可以想出一些肮脏的把戏,比如

public void validate(Client entity) throws ValidatorException {

    try {
        int len = entity.getName().length();
        int isshort = 1 / len;
        int islong = 1 / max (0, 100- length);

    } catch (Exception e) {
        throw new ClientException("Invalid name!");
    }
   try {
        Long.parseLong(entity.getPhone());
    } catch (NumberFormatException e) {
        throw new ClientException("Invalid phone number!");
    }
}


因此,如果需要,则不需要

如果
在某个地方,则必须有
。把它们塞进一个函数就像“我需要一种方法来去掉所有的
if
语句”——为什么?我希望我能把它们都去掉。也许是一些Java 8课程?@AbhijitSarkar这是一个更大的大学作业的一部分,删除所有的ifs是要求的一部分。啊,我是对的,它闻起来像是家庭作业。你必须在某个地方有
if
s。把它们塞进一个函数就像“我需要一种方法来去掉所有的
if
语句”——为什么?我希望我能把它们都去掉。也许是一些Java 8类?@AbhijitSarkar这是一个更大的大学作业的一部分,删除所有的ifs是要求的一部分。啊,我是对的,闻起来像是家庭作业。我在想这样的事情:使用流进行过滤以删除循环;“那是什么东西?”塞巴斯蒂安格里格什么回路?你的问题有基本的流量控制。虽然这是一个完美的解决方案,“我想我的教授不会喜欢它的。”塞巴斯蒂安·格里格(Sebastian Grigor)你的教授似乎不喜欢良好的编程实践with@ElliottFrisch我举了一个例子,说明如何使用Java8的流来过滤一个列表,从而删除其他必要的循环移除回路;“那是什么东西?”塞巴斯蒂安格里格什么回路?你的问题有基本的流量控制。虽然这是一个完美的解决方案,“我想我的教授不会喜欢它的。”塞巴斯蒂安·格里格(Sebastian Grigor)你的教授似乎不喜欢良好的编程实践with@ElliottFrisch我举了一个例子,说明如何使用Java8的流过滤列表,从而删除其他必要的循环。这看起来很有希望。这看起来很有希望。现在呢@SebastianGrigor现在怎么样@SebastianGrigor