Java 方法链可选地基于参数
Java Method Chaining Optionally based on arguments
如果第 3 方在方法中请求参数 attachments
,我如何避免使用 if
并在知道我的参数可能为空的情况下中断方法链接?该方法具有以下定义。
// import org.jetbrains.annotations.NotNull;
EmailBuilder withAttachments(@NotNull List<Attachment> attachments);
我更愿意 NOT 在附件 == null 时对 .withAttachments 使用 if 条件。我知道 javascript 有 method?(),但什么适合 java8 或更高版本?在 (attachments == null) 的情况下,我根本不想调用 .withAttachments()
。但是,我没有看到与 javascript 或 typescript.
中的 methodA?() 相当的语法
return emailBuilder()
.withSubject(email.getSubject())
.withReplyTo(replyAddresses)
.withAttachments(attachments) // This is conditional...based on attachments
.withHeader("X-showheader", email.getShowHeader());
.build();
我需要这样做吗?
EmailBuilder eb = emailBuilder()
.withSubject(email.getSubject())
.withReplyTo(replyAddresses);
if(attachments)
eb = eb.withAttachments(attachments); // This is conditional...based on attachments
eb = eb.withHeader("X-showheader", email.getHeader())
.build;
return eb;
如果 withAttachments()
不允许 null
值,那么是的,您需要 if (attachments != null)
。
但是,由于构建器(通常)不需要特定的方法调用顺序,您可以稍微清理一下代码。
EmailBuilder eb = emailBuilder()
.withSubject(email.getSubject())
.withReplyTo(replyAddresses)
.withHeader("X-showheader", email.getHeader());
if (attachments != null)
eb.withAttachments(attachments);
return eb.build();
我假设您不能更改 withAttachments
的约定来忽略空值调用?您可以在上游将附件包装在 Optional
中,然后提供 orElse
一个空的但不为空的任何类型 attachments
的 impl,例如(假设 attachments
是 List
):
Optional<...> optionalAttachments = Optional.ofNullable(attachments);
...
.withAttachments(optionalAttachments.orElse(Collections.emptyList())
更新(基于评论的输入,向安德烈亚斯致敬)
你也可以用三元来实现,例如:
.withAttachments(attachments != null ? attachments : Collections.emptyList())
如果第 3 方在方法中请求参数 attachments
,我如何避免使用 if
并在知道我的参数可能为空的情况下中断方法链接?该方法具有以下定义。
// import org.jetbrains.annotations.NotNull;
EmailBuilder withAttachments(@NotNull List<Attachment> attachments);
我更愿意 NOT 在附件 == null 时对 .withAttachments 使用 if 条件。我知道 javascript 有 method?(),但什么适合 java8 或更高版本?在 (attachments == null) 的情况下,我根本不想调用 .withAttachments()
。但是,我没有看到与 javascript 或 typescript.
return emailBuilder()
.withSubject(email.getSubject())
.withReplyTo(replyAddresses)
.withAttachments(attachments) // This is conditional...based on attachments
.withHeader("X-showheader", email.getShowHeader());
.build();
我需要这样做吗?
EmailBuilder eb = emailBuilder()
.withSubject(email.getSubject())
.withReplyTo(replyAddresses);
if(attachments)
eb = eb.withAttachments(attachments); // This is conditional...based on attachments
eb = eb.withHeader("X-showheader", email.getHeader())
.build;
return eb;
如果 withAttachments()
不允许 null
值,那么是的,您需要 if (attachments != null)
。
但是,由于构建器(通常)不需要特定的方法调用顺序,您可以稍微清理一下代码。
EmailBuilder eb = emailBuilder()
.withSubject(email.getSubject())
.withReplyTo(replyAddresses)
.withHeader("X-showheader", email.getHeader());
if (attachments != null)
eb.withAttachments(attachments);
return eb.build();
我假设您不能更改 withAttachments
的约定来忽略空值调用?您可以在上游将附件包装在 Optional
中,然后提供 orElse
一个空的但不为空的任何类型 attachments
的 impl,例如(假设 attachments
是 List
):
Optional<...> optionalAttachments = Optional.ofNullable(attachments);
...
.withAttachments(optionalAttachments.orElse(Collections.emptyList())
更新(基于评论的输入,向安德烈亚斯致敬)
你也可以用三元来实现,例如:
.withAttachments(attachments != null ? attachments : Collections.emptyList())