@Test void testLoadCachedFile() throws IOException, RedPenException { Path path = Files.createTempFile("test", "txt"); File file = path.toFile(); System.setProperty("REDPEN_HOME", file.getParentFile().getAbsolutePath()); Files.copy(new ByteArrayInputStream("foo".getBytes()), path, StandardCopyOption.REPLACE_EXISTING); Set<String> strings; strings = WORD_LIST.loadCachedFromFile(path.toFile(), "temp file"); assertEquals(1, strings.size()); assertTrue(strings.contains("foo")); strings = WORD_LIST.loadCachedFromFile(path.toFile(), "temp file"); assertEquals(1, strings.size()); assertTrue(strings.contains("foo")); long lastModified = file.lastModified(); Files.copy(new ByteArrayInputStream("foo\nbar".getBytes()), path, StandardCopyOption.REPLACE_EXISTING); file.setLastModified(lastModified); strings = WORD_LIST.loadCachedFromFile(path.toFile(), "temp file"); assertEquals(1, strings.size()); assertTrue(strings.contains("foo")); file.setLastModified(lastModified + 1000); strings = WORD_LIST.loadCachedFromFile(path.toFile(), "temp file"); assertEquals(2, strings.size()); assertTrue(strings.contains("foo")); assertTrue(strings.contains("bar")); }
public E loadCachedFromFile(File file, String dictionaryName) throws RedPenException { String path = file.getAbsolutePath(); if (!file.exists()) { throw new RedPenException("File not found: " + file); } long currentModified = file.lastModified(); fileNameTimestampMap.computeIfPresent(path, (key, lastModified) -> { if (lastModified != currentModified) { fileCache.remove(key); return null; } else { return lastModified; } }); E loaded = fileCache.computeIfAbsent(path, e -> { try { E newlyLoaded = loadFromFile(file); fileNameTimestampMap.put(path, file.lastModified()); return newlyLoaded; } catch (IOException ioe) { LOG.error(ioe.getMessage()); return null; } }); if (loaded == null) { throw new RedPenException("Failed to load " + dictionaryName + ":" + path); } LOG.info("Succeeded to load " + dictionaryName + "."); return loaded; }
DictionaryLoader { public E loadCachedFromFile(File file, String dictionaryName) throws RedPenException { String path = file.getAbsolutePath(); if (!file.exists()) { throw new RedPenException("File not found: " + file); } long currentModified = file.lastModified(); fileNameTimestampMap.computeIfPresent(path, (key, lastModified) -> { if (lastModified != currentModified) { fileCache.remove(key); return null; } else { return lastModified; } }); E loaded = fileCache.computeIfAbsent(path, e -> { try { E newlyLoaded = loadFromFile(file); fileNameTimestampMap.put(path, file.lastModified()); return newlyLoaded; } catch (IOException ioe) { LOG.error(ioe.getMessage()); return null; } }); if (loaded == null) { throw new RedPenException("Failed to load " + dictionaryName + ":" + path); } LOG.info("Succeeded to load " + dictionaryName + "."); return loaded; } }
DictionaryLoader { public E loadCachedFromFile(File file, String dictionaryName) throws RedPenException { String path = file.getAbsolutePath(); if (!file.exists()) { throw new RedPenException("File not found: " + file); } long currentModified = file.lastModified(); fileNameTimestampMap.computeIfPresent(path, (key, lastModified) -> { if (lastModified != currentModified) { fileCache.remove(key); return null; } else { return lastModified; } }); E loaded = fileCache.computeIfAbsent(path, e -> { try { E newlyLoaded = loadFromFile(file); fileNameTimestampMap.put(path, file.lastModified()); return newlyLoaded; } catch (IOException ioe) { LOG.error(ioe.getMessage()); return null; } }); if (loaded == null) { throw new RedPenException("Failed to load " + dictionaryName + ":" + path); } LOG.info("Succeeded to load " + dictionaryName + "."); return loaded; } DictionaryLoader(Supplier<E> supplier, BiConsumer<E, String> loader); }
DictionaryLoader { public E loadCachedFromFile(File file, String dictionaryName) throws RedPenException { String path = file.getAbsolutePath(); if (!file.exists()) { throw new RedPenException("File not found: " + file); } long currentModified = file.lastModified(); fileNameTimestampMap.computeIfPresent(path, (key, lastModified) -> { if (lastModified != currentModified) { fileCache.remove(key); return null; } else { return lastModified; } }); E loaded = fileCache.computeIfAbsent(path, e -> { try { E newlyLoaded = loadFromFile(file); fileNameTimestampMap.put(path, file.lastModified()); return newlyLoaded; } catch (IOException ioe) { LOG.error(ioe.getMessage()); return null; } }); if (loaded == null) { throw new RedPenException("Failed to load " + dictionaryName + ":" + path); } LOG.info("Succeeded to load " + dictionaryName + "."); return loaded; } DictionaryLoader(Supplier<E> supplier, BiConsumer<E, String> loader); E loadCachedFromResource(String path, String dictionaryName); E loadCachedFromFile(File file, String dictionaryName); }
DictionaryLoader { public E loadCachedFromFile(File file, String dictionaryName) throws RedPenException { String path = file.getAbsolutePath(); if (!file.exists()) { throw new RedPenException("File not found: " + file); } long currentModified = file.lastModified(); fileNameTimestampMap.computeIfPresent(path, (key, lastModified) -> { if (lastModified != currentModified) { fileCache.remove(key); return null; } else { return lastModified; } }); E loaded = fileCache.computeIfAbsent(path, e -> { try { E newlyLoaded = loadFromFile(file); fileNameTimestampMap.put(path, file.lastModified()); return newlyLoaded; } catch (IOException ioe) { LOG.error(ioe.getMessage()); return null; } }); if (loaded == null) { throw new RedPenException("Failed to load " + dictionaryName + ":" + path); } LOG.info("Succeeded to load " + dictionaryName + "."); return loaded; } DictionaryLoader(Supplier<E> supplier, BiConsumer<E, String> loader); E loadCachedFromResource(String path, String dictionaryName); E loadCachedFromFile(File file, String dictionaryName);  }