/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.objcache;

import javax.annotation.Nonnull;

import org.opendaylight.yangtools.objcache.impl.StaticObjectCacheBinder;
import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
import org.opendaylight.yangtools.objcache.spi.NoopObjectCacheBinder;

import com.google.common.base.Preconditions;

/**
 * Point of entry for acquiring an {@link ObjectCache} instance.
 */
public final class ObjectCacheFactory {
    private static IObjectCacheFactory FACTORY;

    private static synchronized IObjectCacheFactory initialize() {
        // Double-check under lock
        if (FACTORY != null) {
            return FACTORY;
        }

        IObjectCacheFactory f;
        try {
            f = StaticObjectCacheBinder.getInstance().getProductCacheFactory();
            FACTORY = f;
        } catch (NoClassDefFoundError e) {
            f = NoopObjectCacheBinder.INSTANCE.getProductCacheFactory();
        }

        return f;
    }

    public static synchronized void reset() {
        FACTORY = null;
    }

    /**
     * Get an ObjectCache for caching a particular object class. Note
     * that it may be shared for multiple classes.
     *
     * @param objClass Class of objects which are to be cached
     * @return Object cache instance.
     */
    public static ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
        IObjectCacheFactory f = FACTORY;
        if (f == null) {
            f = initialize();
        }

        return f.getObjectCache(Preconditions.checkNotNull(objClass));
    }
}

@@ -358,32 +358,124 @@
 ort 
-javax.annotation.Nonnull
+com.google.common.base.Preconditions;%0A%0Aimport javax.annotation.Nonnull;%0Aimport javax.annotation.concurrent.GuardedBy
 ;%0A%0Ai
@@ -689,54 +689,8 @@
 r;%0A%0A
-import com.google.common.base.Preconditions;%0A%0A
 /**%0A
@@ -817,16 +817,25 @@
  static 
+volatile 
 IObjectC
@@ -846,25 +846,48 @@
 Factory 
-FACTORY;%0A
+factory;%0A%0A    @GuardedBy(%22this%22)
 %0A    pri
@@ -993,19 +993,54 @@
     
-if (FACTORY
+IObjectCacheFactory f = factory;%0A        if (f
  != 
@@ -1066,23 +1066,17 @@
  return 
-FACTORY
+f
 ;%0A      
@@ -1084,39 +1084,8 @@
  %7D%0A%0A
-        IObjectCacheFactory f;%0A
     
@@ -1186,23 +1186,23 @@
         
-FACTORY
+factory
  = f;%0A  
@@ -1405,23 +1405,23 @@
         
-FACTORY
+factory
  = null;
@@ -1790,15 +1790,15 @@
 f = 
-FACTORY
+factory
 ;%0A  

Fix ObjectCacheFactory thread safety