gpt4 book ai didi

java - 如何将我的构建器对象复制到另一个对象并修改其中的几个字段?

转载 作者:行者123 更新时间:2023-11-30 08:42:05 26 4
gpt4 key购买 nike

我有一个生成器类,如下所示:

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 setTimeout(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
}

我正在制作我的构建器类:

Map<String, String> holder = new HashMap<String, String>();
holder.put("abc", "hello");
RequestKey keys = new RequestKey.Builder(310).setUserId(75076L).baseMap(holder).setTimeout(10000L).build();

现在我想复制 keys 对象并将其分配给新的 RequestKey 对象说 keysCopy 并更改 only keysCopy 变量中的 userid 到其他一些用户 id 说 12345L。我该怎么做?

在我的其他类(class)中,我可以访问这个 keys 对象,所以我想复制它来制作 keysCopy 对象,然后修改用户 ID 以具有 12345L 在其中,有时我可能还需要通过调用此 setter setFlowId 来设置 FlowId

最佳答案

实现您想要的方法的一种方法是创建一个 Builder 构造函数,它采用 RequestKey 对象并将 Builder 的成员变量初始化为那些RequestKey,然后修改你需要的字段。

public Builder(RequestKey key) {
this.userid = key.userid;
this.deviceid = key.deviceid;
this.baseMap = ImmutableMap.<String,String>builder().putAll( key.baseMap );
...
}

RequestKey keys = ...;
RequestKey keysCopy = new RequestKey.Builder( keys ).setUserId( 12345L ).build();

关于java - 如何将我的构建器对象复制到另一个对象并修改其中的几个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34697521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com