class PasswordFileUpdaterWriter {

    public void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest) throws IOException {
        if (Optional.ofNullable(config).map(Config::getKeys).map(KeyConfiguration::getPasswords).isPresent()
                && !config.getKeys().getPasswords().isEmpty()) {
            throw new ConfigException(new RuntimeException(passwordsMessage));
        }

        if (filesDelegate.exists(pwdDest)) {
            throw new FileAlreadyExistsException(pwdDest.toString());
        }

        LOGGER.info("Writing updated passwords to {}", pwdDest);

        final List<String> passwords;

        if (Optional.ofNullable(config.getKeys()).map(KeyConfiguration::getPasswordFile).isPresent()) {
            passwords = filesDelegate.readAllLines(config.getKeys().getPasswordFile());
        } else {
            LOGGER.info("No existing password file defined in config");
            passwords = new ArrayList<>();

            Optional.ofNullable(config.getKeys())
                    .map(KeyConfiguration::getKeyData)
                    .ifPresent(k -> k.forEach(kk -> passwords.add("")));
        }

        passwords.addAll(
            newPasswords.stream().map(p -> Optional.ofNullable(p)
                                            .map(String::valueOf)
                                            .orElse(""))
            .collect(Collectors.toList())
        );

        filesDelegate.createFile(pwdDest);
        LOGGER.info("Created empty file at {}", pwdDest);

        filesDelegate.setPosixFilePermissions(pwdDest, NEW_PASSWORD_FILE_PERMS);
        filesDelegate.write(pwdDest, passwords, APPEND);
        LOGGER.info("Updated passwords written to {}", pwdDest);
    }

    public  PasswordFileUpdaterWriter(FilesDelegate filesDelegate);

}