/** * 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);
V value = peek(key); if (value == null) { final Handler<V> handler = lock(key);
final Path rlon = DataDirectory.DATUM_CHANGES.resolve(longitudeShifts).toAbsolutePath(); final Object key = new AbstractMap.SimpleImmutableEntry<>(rlat, rlon); DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(key); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(key);
final Path rlon = DataDirectory.DATUM_CHANGES.resolve(longitudeShifts).toAbsolutePath(); final Object key = new AbstractMap.SimpleImmutableEntry<>(rlat, rlon); DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(key); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(key);
final Class<T> type = proxy.type; final Key key = new Key(type, normalizeCode(code)); Object value = cache.peek(key); if (!type.isInstance(value)) { final Cache.Handler<Object> handler = cache.lock(key);
final Class<T> type = proxy.type; final Key key = new Key(type, normalizeCode(code)); Object value = cache.peek(key); if (!type.isInstance(value)) { final Cache.Handler<Object> handler = cache.lock(key);
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved);
DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved);
static DatumShiftGridFile<Angle,Angle> getOrLoad(final Path file) throws FactoryException { final Path resolved = DataDirectory.DATUM_CHANGES.resolve(file).toAbsolutePath(); DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved);
static DatumShiftGridFile<Angle,Angle> getOrLoad(final Path file) throws FactoryException { final Path resolved = DataDirectory.DATUM_CHANGES.resolve(file).toAbsolutePath(); DatumShiftGridFile<?,?> grid = DatumShiftGridFile.CACHE.peek(resolved); if (grid == null) { final Cache.Handler<DatumShiftGridFile<?,?>> handler = DatumShiftGridFile.CACHE.lock(resolved);
if (context == null) { final CRSPair key = new CRSPair(sourceCRS, targetCRS); op = cache.peek(key); if (op != null) { return op;
if (context == null) { final CRSPair key = new CRSPair(sourceCRS, targetCRS); op = cache.peek(key); if (op != null) { return op;
/** * 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()); }
ArgumentChecks.ensureNonNull("targetCRS", targetCRS); final Key key = new Key(normalizeCode(sourceCRS), normalizeCode(targetCRS)); Object value = cache.peek(key); if (!(value instanceof Set<?>)) { final Cache.Handler<Object> handler = cache.lock(key);
ArgumentChecks.ensureNonNull("targetCRS", targetCRS); final Key key = new Key(normalizeCode(sourceCRS), normalizeCode(targetCRS)); Object value = cache.peek(key); if (!(value instanceof Set<?>)) { final Cache.Handler<Object> handler = cache.lock(key);
final CoordinateOperation op = factorySIS.cache.peek(key); if (op != null) return asList(op); // Must be a modifiable list as per this method contract.
thread.start(); TestUtilities.waitForBlockedState(thread); assertNull("The blocked thread shall not have added a value.", cache.peek(keyByOtherThread));
final CoordinateOperation op = factorySIS.cache.peek(key); if (op != null) return op;