我使用 Spring boot 开发了一个用于发送电子邮件的 shell 项目,例如。
sendmail -from foo@bar.com -password foobar -subject "hello world" -to aaa@bbb.com
如果缺少 from
和 password
参数,则使用默认的发送方和密码,例如 noreply@bar.com
和 123456
。
因此,如果用户传递 from
参数,他们也必须传递 password
参数,反之亦然。也就是说,要么两者都是非空的,要么两者都是空的。
我怎样才能优雅地检查这个?
现在我的方法是
if ((from != null && password == null) || (from == null && password != null)) {
throw new RuntimeException("from and password either both exist or both not exist");
}