Skip to content

Commit c4d68c2

Browse files
committed
Merge branch 'cache-service'
2 parents 2e240f8 + d8d0ec4 commit c4d68c2

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed
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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.Priority;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.service.AbstractService;
42+
import org.scijava.service.Service;
43+
44+
/**
45+
* Trivial {@link CacheService} implementation. Wraps a {@link WeakHashMap}
46+
*/
47+
@Plugin(type = Service.class, priority = Priority.VERY_LOW_PRIORITY)
48+
public class DefaultCacheService extends AbstractService implements
49+
CacheService
50+
{
51+
52+
private Map<Object, Object> map;
53+
54+
@Override
55+
public <K, V> void put(final K key, final V value) {
56+
map.put(key, value);
57+
}
58+
59+
@SuppressWarnings("unchecked")
60+
@Override
61+
public <K, V> V get(final K key) {
62+
return (V) map.get(key);
63+
}
64+
65+
@Override
66+
public <K, V> V get(final K key, final Callable<V> valueLoader)
67+
throws ExecutionException
68+
{
69+
return get(key);
70+
}
71+
72+
// -- Service Methods --
73+
74+
@Override
75+
public void initialize() {
76+
map = new WeakHashMap<Object, Object>();
77+
}
78+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public void testFull() {
100100
org.scijava.welcome.DefaultWelcomeService.class,
101101
org.scijava.widget.DefaultWidgetService.class,
102102
org.scijava.log.StderrLogService.class,
103-
org.scijava.platform.DefaultAppEventService.class };
103+
org.scijava.platform.DefaultAppEventService.class,
104+
org.scijava.cache.DefaultCacheService.class};
104105

105106
final Context context = new Context();
106107
verifyServiceOrder(expected, context);

0 commit comments

Comments
 (0)