Remove jacoco overrides
[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 static java.util.Objects.requireNonNull;
11
12 import javax.annotation.Nonnull;
13 import javax.annotation.concurrent.GuardedBy;
14 import org.opendaylight.yangtools.objcache.impl.StaticObjectCacheBinder;
15 import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
16 import org.opendaylight.yangtools.objcache.spi.NoopObjectCacheBinder;
17
18 /**
19  * Point of entry for acquiring an {@link ObjectCache} instance.
20  */
21 public final class ObjectCacheFactory {
22     private static volatile IObjectCacheFactory factory;
23
24     private ObjectCacheFactory() {
25         throw new UnsupportedOperationException("Utility class should not be instantiated");
26     }
27
28     @GuardedBy("this")
29     private static synchronized IObjectCacheFactory initialize() {
30         // Double-check under lock
31         IObjectCacheFactory fa = factory;
32         if (fa != null) {
33             return fa;
34         }
35
36         try {
37             fa = StaticObjectCacheBinder.getInstance().getProductCacheFactory();
38             factory = fa;
39         } catch (NoClassDefFoundError e) {
40             fa = NoopObjectCacheBinder.INSTANCE.getProductCacheFactory();
41         }
42
43         return fa;
44     }
45
46     public static synchronized void reset() {
47         factory = null;
48     }
49
50     /**
51      * Get an ObjectCache for caching a particular object class. Note
52      * that it may be shared for multiple classes.
53      *
54      * @param objClass Class of objects which are to be cached
55      * @return Object cache instance.
56      */
57     public static ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
58         IObjectCacheFactory fa = factory;
59         if (fa == null) {
60             fa = initialize();
61         }
62
63         return fa.getObjectCache(requireNonNull(objClass));
64     }
65 }