Avoid redundant ConcurrentHashMap.get call in java.time

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Fixed
    • Priority: P5
    • 20
    • Affects Version/s: None
    • Component/s: core-libs
    • b08

      In a few places in java.time code performs ConcurrentHashMap.putIfAbsent and then ConcurrentHashMap.get by the same key.
      For example java.time.format.DateTimeTextProvider#findStore:

          private Object findStore(TemporalField field, Locale locale) {
              Entry<TemporalField, Locale> key = createEntry(field, locale);
              Object store = CACHE.get(key);
              if (store == null) {
                  store = createStore(field, locale);
                  CACHE.putIfAbsent(key, store);
                  store = CACHE.get(key);
              }
              return store;
          }

      Instead of separate dive into ConcurrentHashMap internal, we can just reuse result of putIfAbsent call.

                  Object prev = CACHE.putIfAbsent(key, store);
                  if (prev != null) {
                      store = prev;
                  }

            Assignee:
            Andrey Turbanov
            Reporter:
            Andrey Turbanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: