10a8c2a59891eabde9ef0c4da91ebfd9bc38ed19
[yangtools.git] / common / object-cache-guava / src / main / java / org / opendaylight / yangtools / objcache / guava / GuavaObjectCacheFactory.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.guava;
9
10 import com.google.common.base.FinalizableReferenceQueue;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.objcache.ObjectCache;
13 import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
14
15 public final class GuavaObjectCacheFactory implements AutoCloseable, IObjectCacheFactory {
16     private static final GuavaObjectCacheFactory INSTANCE = new GuavaObjectCacheFactory();
17     private final FinalizableReferenceQueue  queue = new FinalizableReferenceQueue();
18     private final ObjectCache cache;
19
20     private GuavaObjectCacheFactory() {
21         // FIXME: make this more dynamic using a spec
22         this.cache = new GuavaObjectCache(queue);
23     }
24
25     @Override
26     public ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
27         return cache;
28     }
29
30     @Override
31     public void close() {
32         queue.close();
33     }
34
35     /**
36      * Return a factory instance.
37      *
38      * @return A factory instance.
39      */
40     public static GuavaObjectCacheFactory getInstance() {
41         return INSTANCE;
42     }
43 }