title: Why does Guava Enums.ifPresent use synchronized under the hood?
body: <p>Guava's <code>Enums.ifPresent(Class, String)</code> calls <code>Enums.getEnumConstants</code> under the hood:</p>
  
  <pre><code>@GwtIncompatible // java.lang.ref.WeakReference
  static &lt;T extends Enum&lt;T&gt;&gt; Map&lt;String, WeakReference&lt;? extends Enum&lt;?&gt;&gt;&gt; getEnumConstants(
  Class&lt;T&gt; enumClass) {
      synchronized (enumConstantCache) {
          Map&lt;String, WeakReference&lt;? extends Enum&lt;?&gt;&gt;&gt; constants = enumConstantCache.get(enumClass);
          if (constants == null) {
              constants = populateCache(enumClass);
          }
          return constants;
      }
  }
  </code></pre>
  
  <p>Why does it need a synchronized block? Wouldn't that incur a <s>heavy</s> performance penalty? Java's <code>Enum.valueOf(Class, String)</code> does not appear to need one. Further on if synchronization is indeed necessary, why do it so inefficiently? One would hope if enum is present in cache, it can be retrieved without locking. Only lock if cache needs to be populated.</p>
  
  <p>For Reference: Maven Dependency</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
      &lt;artifactId&gt;guava&lt;/artifactId&gt;
      &lt;version&gt;23.2-jre&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Edit: By locking I'm referring to a double checking lock.</p>