title: combine putIfAbsent and replace with ConcurrentMap
body: <p>I have a usecase where I have to</p>
  
  <ul>
  <li>insert a new value if the key does not exist in the ConcurrentHashMap</li>
  <li>replace the old value with a new value if the key already exists in
  the ConcurrentHashMap, where the new value is derived from the old value (not an expensive operation)</li>
  </ul>
  
  <p>I've the following code to offer:</p>
  
  <pre><code>public void insertOrReplace(String key, String value) {
          boolean updated = false;
          do {
              String oldValue = concurrentMap.get(key);
              if (oldValue == null) {
                  oldValue = concurrentMap.putIfAbsent(key, value);
                  if (oldValue == null) {
                      updated = true;
                  }
              }
              if (oldValue != null) {
                  final String newValue = recalculateNewValue(oldValue, value);
                  updated = concurrentMap.replace(key, oldValue, newValue);
              }
          } while (!updated);
      }
  </code></pre>
  
  <p>Do you think it's correct and thread-safe?</p>
  
  <p>Is there a simpler way?</p>