/**
 * Copyright 2006-2013 the original author or authors.
 *
 * 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.objenesis;

import java.util.HashMap;
import java.util.Map;

import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.strategy.InstantiatorStrategy;

/**
 * Base class to extend if you want to have a class providing your own default strategy. Can also be
 * instantiated directly.
 * 
 * @author Henri Tremblay
 */
public class ObjenesisBase implements Objenesis {

   /** Strategy used by this Objenesi implementation to create classes */
   protected final InstantiatorStrategy strategy;

   /** Strategy cache. Key = Class, Value = InstantiatorStrategy */
   protected Map<String, ObjectInstantiator<?>> cache;

   /**
    * Constructor allowing to pick a strategy and using cache
    * 
    * @param strategy Strategy to use
    */
   public ObjenesisBase(InstantiatorStrategy strategy) {
      this(strategy, true);
   }

   /**
    * Flexible constructor allowing to pick the strategy and if caching should be used
    * 
    * @param strategy Strategy to use
    * @param useCache If {@link ObjectInstantiator}s should be cached
    */
   public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) {
      if(strategy == null) {
         throw new IllegalArgumentException("A strategy can't be null");
      }
      this.strategy = strategy;
      this.cache = useCache ? new HashMap<String, ObjectInstantiator<?>>() : null;
   }

   @Override
   public String toString() {
      return getClass().getName() + " using " + strategy.getClass().getName()
         + (cache == null ? " without" : " with") + " caching";
   }

   /**
    * Will create a new object without any constructor being called
    * 
    * @param clazz Class to instantiate
    * @return New instance of clazz
    */
   public <T> T newInstance(Class<T> clazz) {
      return getInstantiatorOf(clazz).newInstance();
   }

   /**
    * Will pick the best instantiator for the provided class. If you need to create a lot of
    * instances from the same class, it is way more efficient to create them from the same
    * ObjectInstantiator than calling {@link #newInstance(Class)}.
    * 
    * @param clazz Class to instantiate
    * @return Instantiator dedicated to the class
    */
   @SuppressWarnings("unchecked")
   public <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz) {
      if(cache == null) {
         return strategy.newInstantiatorOf(clazz);
      }
      synchronized (cache) {
         ObjectInstantiator<?> instantiator = cache.get(clazz.getName());
         if(instantiator == null) {
            instantiator = strategy.newInstantiatorOf(clazz);
            cache.put(clazz.getName(), instantiator);
         }
         return (ObjectInstantiator<T>) instantiator;
      }
   }
}

@@ -676,35 +676,33 @@
 til.
-HashMap;%0D%0Aimport java.util.
+concurrent.ConcurrentHash
 Map;
@@ -1249,16 +1249,30 @@
 otected 
+ConcurrentHash
 Map%3CStri
@@ -2007,16 +2007,26 @@
 e ? new 
+Concurrent
 HashMap%3C
@@ -3122,41 +3122,8 @@
  %7D%0D%0A
-      synchronized (cache) %7B%0D%0A   
     
@@ -3196,19 +3196,16 @@
 %0D%0A      
-   
 if(insta
@@ -3220,27 +3220,24 @@
 == null) %7B%0D%0A
-   
          ins
@@ -3293,19 +3293,16 @@
         
-   
 cache.pu
@@ -3302,16 +3302,24 @@
 ache.put
+IfAbsent
 (clazz.g
@@ -3350,25 +3350,19 @@
 %0D%0A      
-   %7D%0D%0A   
+%7D%0D%0A
       re
@@ -3405,25 +3405,16 @@
 iator;%0D%0A
-      %7D%0D%0A
    %7D%0D%0A%7D%0D

Replace the synchronization with a ConcurrentHashMap