最近、自分のプロジェクトの 1 つで Builder パターンの使用を開始し、Builder クラスに何らかの検証を追加しようとしています。コンパイル時にこれを行うことはできないと想定しているため、実行時にこの検証を行っています。しかし、私は間違っているかもしれません。それが、コンパイル時にこれを実行できるかどうかを確認しようとしていることです。
public final class RequestKey {
private final Long userid;
private final String deviceid;
private final String flowid;
private final int clientid;
private final long timeout;
private final boolean abcFlag;
private final boolean defFlag;
private final Map<String, String> baseMap;
private RequestKey(Builder builder) {
this.userid = builder.userid;
this.deviceid = builder.deviceid;
this.flowid = builder.flowid;
this.clientid = builder.clientid;
this.abcFlag = builder.abcFlag;
this.defFlag = builder.defFlag;
this.baseMap = builder.baseMap.build();
this.timeout = builder.timeout;
}
public static class Builder {
protected final int clientid;
protected Long userid = null;
protected String deviceid = null;
protected String flowid = null;
protected long timeout = 200L;
protected boolean abcFlag = false;
protected boolean defFlag = true;
protected ImmutableMap.Builder<String, String> baseMap = ImmutableMap.builder();
public Builder(int clientid) {
checkArgument(clientid > 0, "clientid must not be negative or zero");
this.clientid = clientid;
}
public Builder setUserId(long userid) {
checkArgument(userid > 0, "userid must not be negative or zero");
this.userid = Long.valueOf(userid);
return this;
}
public Builder setDeviceId(String deviceid) {
checkNotNull(deviceid, "deviceid cannot be null");
checkArgument(deviceid.length() > 0, "deviceid can't be an empty string");
this.deviceid = deviceid;
return this;
}
public Builder setFlowId(String flowid) {
checkNotNull(flowid, "flowid cannot be null");
checkArgument(flowid.length() > 0, "flowid can't be an empty string");
this.flowid = flowid;
return this;
}
public Builder baseMap(Map<String, String> baseMap) {
checkNotNull(baseMap, "baseMap cannot be null");
this.baseMap.putAll(baseMap);
return this;
}
public Builder abcFlag(boolean abcFlag) {
this.abcFlag = abcFlag;
return this;
}
public Builder defFlag(boolean defFlag) {
this.defFlag = defFlag;
return this;
}
public Builder addTimeout(long timeout) {
checkArgument(timeout > 0, "timeout must not be negative or zero");
this.timeout = timeout;
return this;
}
public RequestKey build() {
if (!this.isValid()) {
throw new IllegalStateException("You have to pass at least one"
+ " of the following: userid, flowid or deviceid");
}
return new RequestKey(this);
}
private boolean isValid() {
return !(TestUtils.isEmpty(userid) && TestUtils.isEmpty(flowid) && TestUtils.isEmpty(deviceid));
}
}
// getters here
}
問題文:
上記のビルダー パターンでは、必須のパラメーターは 1 つだけで、残りはオプションですが、またはを設定clientId
する必要があります。これら 3 つのいずれも設定されていない場合、上記のコードに示すようなエラー メッセージが表示されます。そのチェックは実行時に行っています。可能であればコンパイル時にこのチェックを行い、すべてが提供されない限りパターンをビルドしないでください。userid
flowid
deviceid
IllegalStateException
毎回これら 3 つの ID をすべて渡すことは必須ではありません。3 つすべて、場合によっては 2 つ、場合によっては 1 つだけを渡すことができますが、条件はいずれか 1 つを設定する必要があります。
実行時にこれを行うのではなく、コンパイル時にのみ ID 検証を行うことができるように、ビルダー パターンを改善するにはどうすればよいですか?
同じことを正確に説明しているこのSOリンクを見つけましたが、シナリオでどのように使用できるかわかりませんか? また、ひねりを加えたこのビルダーパターンとこのSOの質問
ビルダーパターンでこれをどのように修正できますか?