ReflectionCache { public T get(Class<? extends C> type) { return values.computeIfAbsent(type, valueProvider); } ReflectionCache(SerializableFunction<Class<C>, T> valueProvider); T get(Class<? extends C> type); boolean contains(Class<? extends C> type); void clear(); static Registration addClearAllAction(Runnable action); static void clearAll();  }
@Test public void generateCachedValues() { AtomicInteger count = new AtomicInteger(); ReflectionCache<Object, Integer> cache = new ReflectionCache<>( type -> count.incrementAndGet()); Assert.assertEquals(0, count.get()); Assert.assertEquals(1, cache.get(Object.class).intValue()); Assert.assertEquals(2, cache.get(String.class).intValue()); Assert.assertEquals(1, cache.get(Object.class).intValue()); Assert.assertEquals(2, count.get()); }
@Test public void currentInstancesNotAvailable() { String currentString = "My string"; CurrentInstance.set(String.class, currentString); ReflectionCache<Object, String> cache = new ReflectionCache<>( type -> type.getSimpleName() + ": " + CurrentInstance.get(String.class)); try { String result = cache.get(Object.class); Assert.assertEquals("Current instance should not be in the result", "Object: null", result); Assert.assertEquals( "Current instance should be preserved after running", currentString, CurrentInstance.get(String.class)); } finally { CurrentInstance.set(String.class, null); } }