user1950349
user1950349

Reputation: 5146

How to copy my builder object to another object and modify few fields in it?

I have a builder class as shown below:

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
}

I am making my builder class like this:

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();

Now I want to make a copy of keys object and assign it to new RequestKey object say it keysCopy and change only userid in keysCopy variable to some other user id say 12345L. How can I do this?

In my other class I have access to this keys object so I want to copy it to make keysCopy object and then modify user id to have 12345L in it and sometimes I might need to set FlowId by calling this setter setFlowId as well.

Upvotes: 2

Views: 2683

Answers (1)

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

One way of achieving what you want is to create a Builder constructor, that takes RequestKey object and initializes Builder's member variables to those of RequestKey, then just modify the fields you need.

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();

Upvotes: 3

Related Questions