/** * Returns the value for the given key. If a value already exists in the cache, then it * is returned immediately. Otherwise the {@code creator.call()} method is invoked and * its result is saved in this cache for future reuse. * * @param key the key for which to get the cached or created value. * @param creator a method for creating a value, to be invoked only if no value are cached for the given key. * @return the value for the given key, which may have been created as a result of this method call. * @throws Exception if an exception occurred during the execution of {@code creator.call()}. */ public V getOrCreate(final K key, final Callable<? extends V> creator) throws Exception { V value = peek(key); if (value == null) { final Handler<V> handler = lock(key); try { value = handler.peek(); if (value == null) { value = creator.call(); } } finally { handler.putAndUnlock(value); } } return value; }
V value = peek(key); if (value == null) { final Handler<V> handler = lock(key); try { value = handler.peek();
V value = peek(key); if (value == null) { final Handler<V> handler = lock(key); try { value = handler.peek();
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(key); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(key); try { grid = handler.peek();
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(key); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(key); try { grid = handler.peek();
Object value = cache.peek(key); if (!type.isInstance(value)) { final Cache.Handler<Object> handler = cache.lock(key); try { value = handler.peek();
Object value = cache.peek(key); if (!type.isInstance(value)) { final Cache.Handler<Object> handler = cache.lock(key); try { value = handler.peek();
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved); try { grid = handler.peek();
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved); try { grid = handler.peek();
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved); try { grid = handler.peek();
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved); try { grid = handler.peek();
return op; handler = cache.lock(key); } else {
return op; handler = cache.lock(key); } else {
Object value = cache.peek(key); if (!(value instanceof Set<?>)) { final Cache.Handler<Object> handler = cache.lock(key); try { value = handler.peek();
Object value = cache.peek(key); if (!(value instanceof Set<?>)) { final Cache.Handler<Object> handler = cache.lock(key); try { value = handler.peek();
/** * Tests adding a single value using the {@link Cache.Handler#putAndUnlock(Object)} method. * This method does all the operations in a single thread. */ @Test public void testPutAndUnlock() { final String key = "The key"; final String value = "The value"; final Cache<String,String> cache = new Cache<>(); assertTrue("No initial value expected.", cache.isEmpty()); assertNull("No initial value expected.", cache.peek(key)); final Cache.Handler<String> handler = cache.lock(key); assertNull("No initial value expected.", handler.peek()); handler.putAndUnlock(value); assertEquals(1, cache.size()); assertEquals(value, cache.peek(key)); assertEquals(singleton(key), cache.keySet()); assertEquals(singleton(new SimpleEntry<>(key, value)), cache.entrySet()); }
final Cache.Handler<String> handler = cache.lock(keyByMainThread); assertTrue(handler instanceof Cache<?,?>.Work); final OtherThread thread = new OtherThread();