Enable checkstyle on objectcache and mockito-config
[yangtools.git] / common / object-cache-api / src / main / java / org / opendaylight / yangtools / objcache / ObjectCacheFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.objcache;
9
10 import com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12 import javax.annotation.concurrent.GuardedBy;
13 import org.opendaylight.yangtools.objcache.impl.StaticObjectCacheBinder;
14 import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
15 import org.opendaylight.yangtools.objcache.spi.NoopObjectCacheBinder;
16
17 /**
18  * Point of entry for acquiring an {@link ObjectCache} instance.
19  */
20 public final class ObjectCacheFactory {
21     private static volatile IObjectCacheFactory factory;
22
23     private ObjectCacheFactory() {
24         throw new UnsupportedOperationException("Utility class should not be instantiated");
25     }
26
27     @GuardedBy("this")
28     private static synchronized IObjectCacheFactory initialize() {
29         // Double-check under lock
30         IObjectCacheFactory fa = factory;
31         if (fa != null) {
32             return fa;
33         }
34
35         try {
36             fa = StaticObjectCacheBinder.getInstance().getProductCacheFactory();
37             factory = fa;
38         } catch (NoClassDefFoundError e) {
39             fa = NoopObjectCacheBinder.INSTANCE.getProductCacheFactory();
40         }
41
42         return fa;
43     }
44
45     public static synchronized void reset() {
46         factory = null;
47     }
48
49     /**
50      * Get an ObjectCache for caching a particular object class. Note
51      * that it may be shared for multiple classes.
52      *
53      * @param objClass Class of objects which are to be cached
54      * @return Object cache instance.
55      */
56     public static ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
57         IObjectCacheFactory fa = factory;
58         if (fa == null) {
59             fa = initialize();
60         }
61
62         return fa.getObjectCache(Preconditions.checkNotNull(objClass));
63     }
64 }