@Test @SuppressWarnings("unchecked") public void testFindOrInsert() throws Exception { TypeCache<Object> typeCache = new TypeCache<Object>(TypeCache.Sort.WEAK); Object key = new Object(); assertThat(typeCache.find(ClassLoader.getSystemClassLoader(), key), nullValue(Class.class)); Callable<Class<?>> callable = mock(Callable.class); when(callable.call()).thenReturn((Class) Void.class); assertThat(typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), key, callable, new Object()), is((Object) Void.class)); verify(callable).call(); assertThat(typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), key, callable), is((Object) Void.class)); assertThat(typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), key, callable, new Object()), is((Object) Void.class)); verifyNoMoreInteractions(callable); }
public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy) { Class<?> type = find(classLoader, key); if (type != null) { return type; } else { try { return insert(classLoader, key, lazy.call()); } catch (Throwable throwable) { throw new IllegalArgumentException("Could not create type", throwable); } } }
TypeCache extends ReferenceQueue<ClassLoader> { public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy) { Class<?> type = find(classLoader, key); if (type != null) { return type; } else { try { return insert(classLoader, key, lazy.call()); } catch (Throwable throwable) { throw new IllegalArgumentException("Could not create type", throwable); } } } }
TypeCache extends ReferenceQueue<ClassLoader> { public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy) { Class<?> type = find(classLoader, key); if (type != null) { return type; } else { try { return insert(classLoader, key, lazy.call()); } catch (Throwable throwable) { throw new IllegalArgumentException("Could not create type", throwable); } } } TypeCache(Sort sort); }
TypeCache extends ReferenceQueue<ClassLoader> { public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy) { Class<?> type = find(classLoader, key); if (type != null) { return type; } else { try { return insert(classLoader, key, lazy.call()); } catch (Throwable throwable) { throw new IllegalArgumentException("Could not create type", throwable); } } } TypeCache(Sort sort); @SuppressFBWarnings(value = "GC_UNRELATED_TYPES", justification = "Cross-comparison is intended") Class<?> find(ClassLoader classLoader, T key); @SuppressFBWarnings(value = "GC_UNRELATED_TYPES", justification = "Cross-comparison is intended") Class<?> insert(ClassLoader classLoader, T key, Class<?> type); Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy); Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy, Object monitor); void expungeStaleEntries(); void clear(); }
TypeCache extends ReferenceQueue<ClassLoader> { public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy) { Class<?> type = find(classLoader, key); if (type != null) { return type; } else { try { return insert(classLoader, key, lazy.call()); } catch (Throwable throwable) { throw new IllegalArgumentException("Could not create type", throwable); } } } TypeCache(Sort sort); @SuppressFBWarnings(value = "GC_UNRELATED_TYPES", justification = "Cross-comparison is intended") Class<?> find(ClassLoader classLoader, T key); @SuppressFBWarnings(value = "GC_UNRELATED_TYPES", justification = "Cross-comparison is intended") Class<?> insert(ClassLoader classLoader, T key, Class<?> type); Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy); Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy, Object monitor); void expungeStaleEntries(); void clear();  }