AbstractCache implements IDataCache<T> { @Override public void cacheData(T data) { if (!dataIsCachable(data) || dataIsInCache(data.getId())) { return; } if (data.getByteSize() > maxCacheSize) { return; } lock.writeLock().lock(); try { while (!hasStorageFor(data.getByteSize())) { removeEntryByCachingStrategy(); } addEntry(createEntry(data)); } finally { lock.writeLock().unlock(); } } AbstractCache(S entries, IDataStorage<T> cachedStorage, long maxCacheSize,
      Predicate<T> cachingRule); @Override long getMaxCacheSize(); @Override boolean setMaxCacheSize(long maxCacheSize); @Override long getCurrentCacheSize(); @Override long getFreeSpace(); @Override boolean hasStorageFor(long size); @Override void cacheData(T data); @Override void uncacheData(T data); @Override boolean dataIsCachable(T data); @Override boolean dataIsInCache(long id); @Override void clearCache(); @Override boolean dataExists(long id); @Override T loadData(long id); @Override boolean saveData(T data); @Override boolean dataIsStorable(T data); @Override boolean deleteData(T data);  }
@Test public void testCacheData() { AbstractCacheWrapper uut = new AbstractCacheWrapper(entries, storageAll, 6500, predicate -> predicate != null); uut.cacheData(c0); assertTrue(uut.dataIsInCache(c0.getId())); uut.cacheData(c1); assertTrue(uut.dataIsInCache(c1.getId())); uut.cacheData(null); uut.cacheData(c3); assertFalse(uut.dataIsInCache(c0.getId())); assertTrue(uut.dataIsInCache(c1.getId())); assertTrue(uut.dataIsInCache(c3.getId())); uut.cacheData(c3); assertTrue(uut.dataIsInCache(c1.getId())); assertTrue(uut.dataIsInCache(c3.getId())); entries.clear(); uut = new AbstractCacheWrapper(entries, storageAll, 6500, predicate -> false); uut.cacheData(c0); assertFalse(uut.dataIsInCache(c0.getId())); uut.cacheData(c1); assertFalse(uut.dataIsInCache(c1.getId())); entries.clear(); uut = new AbstractCacheWrapper(entries, storageAll, 1000, predicate -> true); uut.cacheData(c3); assertFalse(uut.dataIsInCache(c3.getId())); }