ThreadLocalCache { public V get(K key) { V value = cache.get().computeIfAbsent(key, loader); return requireNonNull(value, "loader returned null value"); } ThreadLocalCache(int maxSizePerThread, Function<K, V> loader); V get(K key);  }
@Test public void testSanity() { AtomicInteger count = new AtomicInteger(0); ThreadLocalCache<String, String> cache = new ThreadLocalCache<>(2, key -> { return key + count.getAndAdd(1); }); assertEquals(cache.get("abc"), "abc0"); assertEquals(cache.get("abc"), "abc0"); assertEquals(cache.get("def"), "def1"); assertEquals(cache.get("abc"), "abc0"); assertEquals(cache.get("ghi"), "ghi2"); assertEquals(cache.get("abc"), "abc3"); }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "loader returned null value") public void testDisallowsNulls() { new ThreadLocalCache<>(10, key -> null).get("foo"); }