/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.core.config;

import java.util.Map;
import java.util.WeakHashMap;

import static java.util.Collections.synchronizedMap;

/**
 * A registry of {@link JdbiConfig} instances by type.
 *
 * @see Configurable
 */
public class ConfigRegistry {
    private final Map<Class<? extends JdbiConfig<?>>, JdbiConfig<?>> cache = synchronizedMap(new WeakHashMap<>());

    /**
     * Creates a new config registry.
     */
    public ConfigRegistry() {}

    private ConfigRegistry(ConfigRegistry that) {
        that.cache.forEach((type, config) -> {
            JdbiConfig<?> copy = config.createCopy();
            copy.setRegistry(this);
            cache.put(type, copy);
        });
    }

    /**
     * Returns this registry's instance of the given config class. Creates an instance on-demand if this registry does
     * not have one of the given type yet.
     *
     * @param configClass the config class type.
     * @param <C>         the config class type.
     * @return the given config class instance that belongs to this registry.
     */
    public <C extends JdbiConfig<C>> C get(Class<C> configClass) {
        return configClass.cast(cache.computeIfAbsent(configClass, type -> {
            try {
                C config = configClass.getDeclaredConstructor().newInstance();
                config.setRegistry(this);
                return config;
            } catch (ReflectiveOperationException e) {
                throw new IllegalStateException("Unable to instantiate config class " + configClass
                    + ". Is there a public no-arg constructor?", e);
            }
        }));
    }

    /**
     * Returns a copy of this config registry.
     *
     * @return a copy of this config registry
     * @see JdbiConfig#createCopy() config objects in the returned registry are copies of the corresponding
     * config objects from this registry.
     */
    public ConfigRegistry createCopy() {
        return new ConfigRegistry(this);
    }
}

@@ -628,29 +628,25 @@
 til.
-WeakHashMap;%0A
+Optional;
 %0Aimport 
 stat
@@ -645,15 +645,8 @@
 ort 
-static 
 java
@@ -655,32 +655,33 @@
 til.
-Collections.synchronized
+concurrent.ConcurrentHash
 Map;
@@ -795,24 +795,76 @@
 gRegistry %7B%0A
+    private final Object createLock = new Object();%0A
     private 
@@ -925,39 +925,31 @@
 %3E%3E c
-ache = synchronizedMap(new Weak
+onfigs = new Concurrent
 Hash
@@ -955,17 +955,16 @@
 hMap%3C%3E()
-)
 ;%0A%0A    /
@@ -1108,20 +1108,22 @@
   that.c
-ache
+onfigs
 .forEach
@@ -1246,20 +1246,22 @@
        c
-ache
+onfigs
 .put(typ
@@ -1729,74 +1729,244 @@
     
-return configClass.cast(cache.computeIfAbsent(configClass, type -%3E
+// we would computeIfAbsent if not for JDK-8062841 %3E:(%0A        final JdbiConfig%3C?%3E lookup = configs.get(configClass);%0A        if (lookup != null) %7B%0A            return configClass.cast(lookup);%0A        %7D%0A        synchronized (createLock)
  %7B%0A 
@@ -2126,22 +2126,110 @@
  return 
-config
+Optional.ofNullable(configClass.cast(configs.putIfAbsent(configClass, config))).orElse(config)
 ;%0A      
@@ -2452,32 +2452,32 @@
 ;%0A            %7D%0A
+
         %7D));%0A   
@@ -2473,11 +2473,8 @@
    %7D
-));
 %0A   

Replace synchronized WeakHashMap with ConcurrentHashMap