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

时间:2016-01-09 18:48:44

标签: java builder builder-pattern

我有一个构建器类,如下所示:

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更改为keysCopy并更改中的用户ID变量到其他一些用户ID说12345L。我怎么能这样做?

在我的其他类中,我可以访问此keys对象,因此我想将其复制到keysCopy对象,然后修改用户ID以使其中包含12345L,有时我可能会需要通过调用此setter FlowId来设置setFlowId

1 个答案:

答案 0 :(得分:2)

实现目标的一种方法是创建一个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();