title: How to use default value in the builder pattern if that value is not passed and also make thread safe?
body: <p>I am trying to use Builder Pattern for my class.. </p>
  
  <p>Below is my Builder class which I have built by following Joshua Bloch version as he showed in Effective Java, 2nd Edition. Our customer will mostly pass <code>userId</code>, <code>clientId</code> always but other fields are optional and they may or may not pass it. Here Preference is an ENUM class having four fields in it.</p>
  
  <pre><code>public final class InputKeys {
  
      private long userid;
      private long clientid;
      private long timeout = 500L;
      private Preference pref;
      private boolean debug;
      private Map&lt;String, String&gt; parameterMap;
  
      private InputKeys(Builder builder) {
          this.userid = builder.userId;
          this.clientid = builder.clientId;
          this.pref = builder.preference;
          this.parameterMap = builder.parameterMap;
          this.timeout = builder.timeout;
          this.debug = builder.debug;
      }
  
      public static class Builder {
          protected final long userId;
          protected final long clientId;
          protected long timeout;
          protected Preference preference;
          protected boolean debug;
          protected Map&lt;String, String&gt; parameterMap;
  
          public Builder(long userId, long clientId) {
              this.userId = userId;
              this.clientId = clientId;
          }
  
          public Builder parameterMap(Map&lt;String, String&gt; parameterMap) {
              this.parameterMap = parameterMap;
              return this;
          }
  
          public Builder preference(Preference preference) {
              this.preference = preference;
              return this;
          }
  
          public Builder debug(boolean debug) {
              this.debug = debug;
              return this;
          }
  
          public Builder timeout(long timeout) {
              this.timeout = timeout;
              return this;
          }
  
          public InputKeys build() {
              return new InputKeys(this);
          }
      }
  }
  </code></pre>
  
  <p>Below is my ENUM class - </p>
  
  <pre><code>public enum Preference {
      PRIMARY,
      SECONDARY
  }    
  </code></pre>
  
  <p>I am making a call like this to construct the <code>InputKeys</code> parameter - </p>
  
  <pre><code>InputKeys keys = new InputKeys.Builder(12000L, 33L).build();    
  System.out.println(keys);
  </code></pre>
  
  <p>The only problem here I am seeing is, if the customers are not passing any timeout value, I need to have default timeout value set as 500 always but if they are passing any timeout value then it should override my default timeout value. And this is not working for me as when I see my <code>keys</code> in the debug mode, it always has timeout value as 0.</p>
  
  <p>Is there anything I am missing? And also I am trying to make this class immutable and thread safe.. Is this a thread safe and immutable version of Builder or I have missed something?</p>
  
  <p><strong>UPDATE:-</strong>    </p>
  
  <p>Specific scenarios when people will be using this is we have a factory which customers are going to use to make a call to our code. We have a simple interface which one of our class is implementing it and then we have a factory by which we will call a certain method of that implementation which accepts this <code>keys</code> parameter.  </p>
  
  <p>So they will be using the below code in there application to make a call and it might be possible they will be running there application in a multithreaded environment.</p>
  
  <pre><code>InputKeys keys = new InputKeys.Builder(12000L, 33L).build();    
  
  IClient client = ClientFactory.getInstance();
  client.getData(keys);
  </code></pre>
  
  <p>And then InputKeys Builder can be used multiple times to construct the keys, it's not one time. Whatever userId and clientId they will be getting they will construct the InputKeys again.</p>