public static Map<String, Supplier<JGitFileSystem>> create(JGitFileSystemProviderConfiguration config) { return Collections.synchronizedMap(new LinkedHashMap<String, Supplier<JGitFileSystem>>(config.getJgitFileSystemsInstancesCache() + 1, 0.75f, true) {
@Override public Supplier<JGitFileSystem> putIfAbsent(String key, Supplier<JGitFileSystem> value) { Supplier<JGitFileSystem> jGitFileSystemSupplier = super.putIfAbsent(key, value); if (size() > config.getJgitFileSystemsInstancesCache()) { fitListToCacheSize(); } return jGitFileSystemSupplier; }
@Override public boolean removeEldestEntry(Map.Entry eldest) { if (removeEldestEntryIterations > config.getJgitRemoveEldestEntryIterations()) { removeEldestEntryIterations = 0; return false; } if (size() > config.getJgitFileSystemsInstancesCache()) { JGitFileSystem targetFS = (JGitFileSystem) ((MemoizedFileSystemsSupplier) eldest.getValue()).get(); if (targetFS.hasBeenInUse()) { removeEldestEntryIterations++; this.remove(eldest.getKey()); this.put((String) eldest.getKey(), (MemoizedFileSystemsSupplier) eldest.getValue()); return false; } else { return true; } } return false; } });
@Test public void addAndGetTest() { when(config.getJgitFileSystemsInstancesCache()).thenReturn(2); cache = new JGitFileSystemsCache(config); assertTrue(cache.fileSystemsSuppliers.isEmpty()); assertTrue(cache.memoizedSuppliers.isEmpty()); assertEquals(null, cache.get("fs1")); JGitFileSystem fs1 = mock(JGitFileSystem.class); Supplier<JGitFileSystem> fs1Supplier = () -> fs1; cache.addSupplier("fs1", fs1Supplier); assertFalse(cache.fileSystemsSuppliers.isEmpty()); assertFalse(cache.memoizedSuppliers.isEmpty()); JGitFileSystemProxy fs1Proxy = (JGitFileSystemProxy) cache.get("fs1"); assertEquals(fs1, fs1Proxy.getRealJGitFileSystem()); assertTrue(cache.containsKey("fs1")); cache.clear(); assertTrue(cache.fileSystemsSuppliers.isEmpty()); assertTrue(cache.memoizedSuppliers.isEmpty()); }
@Test public void addAndGetTest() { when(config.getJgitFileSystemsInstancesCache()).thenReturn(2); cache = new JGitFileSystemsCache(config); assertTrue(cache.fileSystemsSuppliers.isEmpty()); assertTrue(cache.memoizedSuppliers.isEmpty()); assertEquals(null, cache.get("fs1")); JGitFileSystem fs1 = mock(JGitFileSystem.class); Supplier<JGitFileSystem> fs1Supplier = () -> fs1; cache.addSupplier("fs1", fs1Supplier); assertFalse(cache.fileSystemsSuppliers.isEmpty()); assertFalse(cache.memoizedSuppliers.isEmpty()); JGitFileSystemProxy fs1Proxy = (JGitFileSystemProxy) cache.get("fs1"); assertEquals(fs1, fs1Proxy.getRealJGitFileSystem()); assertTrue(cache.containsKey("fs1")); cache.clear(); assertTrue(cache.fileSystemsSuppliers.isEmpty()); assertTrue(cache.memoizedSuppliers.isEmpty()); }
@Test public void addMoreFSThanCacheSupports() { when(config.getJgitFileSystemsInstancesCache()).thenReturn(2); cache = new JGitFileSystemsCache(config);
@Test public void addMoreFSThanCacheSupports() { when(config.getJgitFileSystemsInstancesCache()).thenReturn(2); cache = new JGitFileSystemsCache(config);