2

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

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为其他用户标识12345L。我怎样才能做到这一点?

在我的另一个类中,我可以访问该keys对象,因此我想将其复制以制作keysCopy对象,然后修改用户 ID 以包含12345L在其中,有时我可能还需要FlowId通过调用此设置器来进行设置setFlowId

4

1 回答 1

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();
于 2016-01-09T19:22:50.283 回答