@Test public void sequenceFiles() throws IOException { File dataDir = new File("target/data"); FileUtils.deleteDirectory(dataDir); dataDir.mkdirs(); MavenConfigurationSupport support = new MavenConfigurationSupport() { @Override protected void doAction(String prefix, Dictionary<String, Object> config) throws Exception { } }; File newFile = support.nextSequenceFile(dataDir, Pattern.compile("file-(\\d+).txt"), "file-%04d.txt"); assertTrue(Pattern.compile("^file-\\d+\\.txt$").matcher(newFile.getName()).matches()); try (FileWriter fw = new FileWriter(new File(dataDir, "file-abcd.txt"))) { fw.write("~"); } newFile = support.nextSequenceFile(dataDir, Pattern.compile("file-(\\d+).txt"), "file-%04d.txt"); assertTrue(Pattern.compile("^file-\\d+\\.txt$").matcher(newFile.getName()).matches()); try (FileWriter fw = new FileWriter(new File(dataDir, "file-0004.txt"))) { fw.write("~"); } newFile = support.nextSequenceFile(dataDir, Pattern.compile("file-(\\d+).txt"), "file-%04d.txt"); assertTrue(Pattern.compile("^file-\\d+\\.txt$").matcher(newFile.getName()).matches()); }
protected File nextSequenceFile(File dataDir, Pattern pattern, String fileNameFormat) { File[] files = dataDir.listFiles((dir, name) -> pattern.matcher(name).matches()); File result = null; if (files != null && files.length > 0) { List<String> names = new ArrayList<>(Arrays.stream(files).map(File::getName) .collect(TreeSet::new, TreeSet::add, TreeSet::addAll)); names.add(String.format(fileNameFormat, System.currentTimeMillis())); while (names.size() > MAX_SEQUENCE_SIZE) { String name = names.remove(0); new File(dataDir, name).delete(); } result = new File(dataDir, names.get(names.size() - 1)); } if (result == null) { result = new File(dataDir, String.format(fileNameFormat, new Date().getTime())); } return result; }
MavenConfigurationSupport implements Action { protected File nextSequenceFile(File dataDir, Pattern pattern, String fileNameFormat) { File[] files = dataDir.listFiles((dir, name) -> pattern.matcher(name).matches()); File result = null; if (files != null && files.length > 0) { List<String> names = new ArrayList<>(Arrays.stream(files).map(File::getName) .collect(TreeSet::new, TreeSet::add, TreeSet::addAll)); names.add(String.format(fileNameFormat, System.currentTimeMillis())); while (names.size() > MAX_SEQUENCE_SIZE) { String name = names.remove(0); new File(dataDir, name).delete(); } result = new File(dataDir, names.get(names.size() - 1)); } if (result == null) { result = new File(dataDir, String.format(fileNameFormat, new Date().getTime())); } return result; } }
MavenConfigurationSupport implements Action { protected File nextSequenceFile(File dataDir, Pattern pattern, String fileNameFormat) { File[] files = dataDir.listFiles((dir, name) -> pattern.matcher(name).matches()); File result = null; if (files != null && files.length > 0) { List<String> names = new ArrayList<>(Arrays.stream(files).map(File::getName) .collect(TreeSet::new, TreeSet::add, TreeSet::addAll)); names.add(String.format(fileNameFormat, System.currentTimeMillis())); while (names.size() > MAX_SEQUENCE_SIZE) { String name = names.remove(0); new File(dataDir, name).delete(); } result = new File(dataDir, names.get(names.size() - 1)); } if (result == null) { result = new File(dataDir, String.format(fileNameFormat, new Date().getTime())); } return result; }  }
MavenConfigurationSupport implements Action { protected File nextSequenceFile(File dataDir, Pattern pattern, String fileNameFormat) { File[] files = dataDir.listFiles((dir, name) -> pattern.matcher(name).matches()); File result = null; if (files != null && files.length > 0) { List<String> names = new ArrayList<>(Arrays.stream(files).map(File::getName) .collect(TreeSet::new, TreeSet::add, TreeSet::addAll)); names.add(String.format(fileNameFormat, System.currentTimeMillis())); while (names.size() > MAX_SEQUENCE_SIZE) { String name = names.remove(0); new File(dataDir, name).delete(); } result = new File(dataDir, names.get(names.size() - 1)); } if (result == null) { result = new File(dataDir, String.format(fileNameFormat, new Date().getTime())); } return result; }  @Override final Object execute(); }
MavenConfigurationSupport implements Action { protected File nextSequenceFile(File dataDir, Pattern pattern, String fileNameFormat) { File[] files = dataDir.listFiles((dir, name) -> pattern.matcher(name).matches()); File result = null; if (files != null && files.length > 0) { List<String> names = new ArrayList<>(Arrays.stream(files).map(File::getName) .collect(TreeSet::new, TreeSet::add, TreeSet::addAll)); names.add(String.format(fileNameFormat, System.currentTimeMillis())); while (names.size() > MAX_SEQUENCE_SIZE) { String name = names.remove(0); new File(dataDir, name).delete(); } result = new File(dataDir, names.get(names.size() - 1)); } if (result == null) { result = new File(dataDir, String.format(fileNameFormat, new Date().getTime())); } return result; }  @Override final Object execute(); static Logger LOG; }