Introduce Object Cache API and no-op implementation
[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 javax.annotation.Nonnull;
11
12 import org.opendaylight.yangtools.objcache.impl.StaticObjectCacheBinder;
13 import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
14
15 import com.google.common.base.Preconditions;
16
17 /**
18  * Point of entry for acquiring an {@link ObjectCache} instance.
19  */
20 public final class ObjectCacheFactory {
21         private static IObjectCacheFactory FACTORY;
22
23         private static synchronized IObjectCacheFactory initialize() {
24                 // Double-check under lock
25                 if (FACTORY != null) {
26                         return FACTORY;
27                 }
28
29                 final IObjectCacheFactory f = StaticObjectCacheBinder.getInstance().getProductCacheFactory();
30                 FACTORY = f;
31                 return f;
32         }
33
34         public static synchronized void reset() {
35                 FACTORY = null;
36         }
37
38         /**
39          * Get an ObjectCache for caching a particular object class. Note
40          * that it may be shared for multiple classes.
41          * 
42          * @param objClass Class of objects which are to be cached
43          * @return Object cache instance.
44          */
45         public static ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
46                 IObjectCacheFactory f = FACTORY;
47                 if (f == null) {
48                         f = initialize();
49                 }
50
51                 return f.getObjectCache(Preconditions.checkNotNull(objClass));
52         }
53 }