Skip to content

Commit 5893d52

Browse files
committed
Add cache service
Adds the base interface for CacheService, a minial default implementation, and updates the ContextCreationTest as needed. See #142
1 parent 2e240f8 commit 5893d52

3 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.cache;
33+
34+
import java.util.concurrent.Callable;
35+
import java.util.concurrent.ExecutionException;
36+
37+
import org.scijava.service.SciJavaService;
38+
39+
/**
40+
* Base interface for cache services in SciJava
41+
*/
42+
public interface CacheService extends SciJavaService {
43+
44+
/**
45+
* Stores the given object in the cache.
46+
*
47+
* @param key A key.
48+
* @param value A value.
49+
*/
50+
<K, V> void put(K key, V value);
51+
52+
/**
53+
* @param key A key
54+
* @return The cached object, or null if the object is not in the cache.
55+
*/
56+
<K, V> V get(K key);
57+
58+
/**
59+
* @param key A key
60+
* @param valueLoader A value loader which will be used if null is returned
61+
* for the given key.
62+
* @return The cached object, or if the object is not in the cache the result
63+
* of the value loader.
64+
* @throws ExecutionException
65+
*/
66+
<K, V> V get(K key, Callable<V> valueLoader) throws ExecutionException;
67+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.cache;
33+
34+
import java.util.Map;
35+
import java.util.WeakHashMap;
36+
import java.util.concurrent.Callable;
37+
import java.util.concurrent.ExecutionException;
38+
39+
import org.scijava.plugin.Plugin;
40+
import org.scijava.service.AbstractService;
41+
import org.scijava.service.Service;
42+
43+
/**
44+
* Trivial {@link CacheService} implementation. Wraps a {@link WeakHashMap}
45+
*/
46+
@Plugin(type = Service.class)
47+
public class DefaultCacheService extends AbstractService implements
48+
CacheService
49+
{
50+
51+
private Map<Object, Object> map;
52+
53+
@Override
54+
public <K, V> void put(final K key, final V value) {
55+
map.put(key, value);
56+
}
57+
58+
@SuppressWarnings("unchecked")
59+
@Override
60+
public <K, V> V get(final K key) {
61+
return (V) map.get(key);
62+
}
63+
64+
@Override
65+
public <K, V> V get(final K key, final Callable<V> valueLoader)
66+
throws ExecutionException
67+
{
68+
return get(key);
69+
}
70+
71+
// -- Service Methods --
72+
73+
@Override
74+
public void initialize() {
75+
map = new WeakHashMap<Object, Object>();
76+
}
77+
}

‎src/test/java/org/scijava/ContextCreationTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public void testFull() {
7575
org.scijava.script.DefaultScriptService.class,
7676
org.scijava.app.DefaultAppService.class,
7777
org.scijava.app.DefaultStatusService.class,
78+
org.scijava.cache.DefaultCacheService.class,
7879
org.scijava.command.DefaultCommandService.class,
7980
org.scijava.console.DefaultConsoleService.class,
8081
org.scijava.convert.DefaultConvertService.class,

0 commit comments

Comments
 (0)