@Test public void testComputeDataIfAbsent() { AtomicLong cnt = new AtomicLong(); for (int i = 0; i < 10; i++) { Integer res = (Integer) dataStorage.computeDataIfAbsent(0, () -> { cnt.incrementAndGet(); return 42; }); assertEquals(42, res.intValue()); } assertEquals(1, cnt.intValue()); }
<D> D computeDataIfAbsent(int part, Supplier<D> supplier) { ObjectWithUsageStat objWithStat = storage.get(part); if (objWithStat == null) { Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock()); lock.lock(); try { objWithStat = storage.get(part); if (objWithStat == null) { objWithStat = new ObjectWithUsageStat(supplier.get()); storage.put(part, objWithStat); if (ttl > -1) executor.schedule(new Cleaner(part), ttl, TimeUnit.MILLISECONDS); } } finally { lock.unlock(); } } objWithStat.updateLastAccessTime(); return (D)objWithStat.data; }
PartitionDataStorage implements AutoCloseable { <D> D computeDataIfAbsent(int part, Supplier<D> supplier) { ObjectWithUsageStat objWithStat = storage.get(part); if (objWithStat == null) { Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock()); lock.lock(); try { objWithStat = storage.get(part); if (objWithStat == null) { objWithStat = new ObjectWithUsageStat(supplier.get()); storage.put(part, objWithStat); if (ttl > -1) executor.schedule(new Cleaner(part), ttl, TimeUnit.MILLISECONDS); } } finally { lock.unlock(); } } objWithStat.updateLastAccessTime(); return (D)objWithStat.data; } }
PartitionDataStorage implements AutoCloseable { <D> D computeDataIfAbsent(int part, Supplier<D> supplier) { ObjectWithUsageStat objWithStat = storage.get(part); if (objWithStat == null) { Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock()); lock.lock(); try { objWithStat = storage.get(part); if (objWithStat == null) { objWithStat = new ObjectWithUsageStat(supplier.get()); storage.put(part, objWithStat); if (ttl > -1) executor.schedule(new Cleaner(part), ttl, TimeUnit.MILLISECONDS); } } finally { lock.unlock(); } } objWithStat.updateLastAccessTime(); return (D)objWithStat.data; } PartitionDataStorage(long ttl); }
PartitionDataStorage implements AutoCloseable { <D> D computeDataIfAbsent(int part, Supplier<D> supplier) { ObjectWithUsageStat objWithStat = storage.get(part); if (objWithStat == null) { Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock()); lock.lock(); try { objWithStat = storage.get(part); if (objWithStat == null) { objWithStat = new ObjectWithUsageStat(supplier.get()); storage.put(part, objWithStat); if (ttl > -1) executor.schedule(new Cleaner(part), ttl, TimeUnit.MILLISECONDS); } } finally { lock.unlock(); } } objWithStat.updateLastAccessTime(); return (D)objWithStat.data; } PartitionDataStorage(long ttl); @Override void close(); }
PartitionDataStorage implements AutoCloseable { <D> D computeDataIfAbsent(int part, Supplier<D> supplier) { ObjectWithUsageStat objWithStat = storage.get(part); if (objWithStat == null) { Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock()); lock.lock(); try { objWithStat = storage.get(part); if (objWithStat == null) { objWithStat = new ObjectWithUsageStat(supplier.get()); storage.put(part, objWithStat); if (ttl > -1) executor.schedule(new Cleaner(part), ttl, TimeUnit.MILLISECONDS); } } finally { lock.unlock(); } } objWithStat.updateLastAccessTime(); return (D)objWithStat.data; } PartitionDataStorage(long ttl); @Override void close();  }