@Test public void will_retry_with_another_name_in_sequence_when_directory_already_exists() throws IOException { Files.createDirectory(baseDir.resolve("123")); Path dir = UniqueDirectories.createUniqueDir(baseDir, 123); assertThat("creates the directory", dir, satisfies(p -> Files.isDirectory(p))); assertThat("name is next in sequence", dir.getFileName(), is(Paths.get("124"))); }
public static Path createUniqueDir(Path parentDir, long sequence) throws IOException { Files.createDirectories(parentDir); while (true) { Path path = parentDir.resolve(Long.toString(sequence)); try { return Files.createDirectory(path); } catch (FileAlreadyExistsException e) { sequence++; } } }
UniqueDirectories { public static Path createUniqueDir(Path parentDir, long sequence) throws IOException { Files.createDirectories(parentDir); while (true) { Path path = parentDir.resolve(Long.toString(sequence)); try { return Files.createDirectory(path); } catch (FileAlreadyExistsException e) { sequence++; } } } }
UniqueDirectories { public static Path createUniqueDir(Path parentDir, long sequence) throws IOException { Files.createDirectories(parentDir); while (true) { Path path = parentDir.resolve(Long.toString(sequence)); try { return Files.createDirectory(path); } catch (FileAlreadyExistsException e) { sequence++; } } }  }
UniqueDirectories { public static Path createUniqueDir(Path parentDir, long sequence) throws IOException { Files.createDirectories(parentDir); while (true) { Path path = parentDir.resolve(Long.toString(sequence)); try { return Files.createDirectory(path); } catch (FileAlreadyExistsException e) { sequence++; } } }  static Path createUniqueDir(Path parentDir, long sequence); }
UniqueDirectories { public static Path createUniqueDir(Path parentDir, long sequence) throws IOException { Files.createDirectories(parentDir); while (true) { Path path = parentDir.resolve(Long.toString(sequence)); try { return Files.createDirectory(path); } catch (FileAlreadyExistsException e) { sequence++; } } }  static Path createUniqueDir(Path parentDir, long sequence);  }