// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.file;

import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import org.glassfish.jersey.internal.util.Producer;

import java.nio.file.Files;
import java.nio.file.Path;

/**
 * Write a file
 *
 * @author hakonhall
 */
public class FileWriter {
    private final Path path;
    private final FileSync fileSync;
    private final PartialFileData.Builder fileDataBuilder = PartialFileData.builder();
    private final Producer<String> contentProducer;

    private boolean overwriteExistingFile = true;

    public FileWriter(Path path, Producer<String> contentProducer) {
        this.path = path;
        this.fileSync = new FileSync(path);
        this.contentProducer = contentProducer;
    }

    public FileWriter withOwner(String owner) {
        fileDataBuilder.withOwner(owner);
        return this;
    }

    public FileWriter withGroup(String group) {
        fileDataBuilder.withGroup(group);
        return this;
    }

    public FileWriter withPermissions(String permissions) {
        fileDataBuilder.withPermissions(permissions);
        return this;
    }

    public FileWriter onlyIfFileDoesNotAlreadyExist() {
        overwriteExistingFile = false;
        return this;
    }

    public boolean converge(TaskContext context) {
        if (!overwriteExistingFile && Files.isRegularFile(path)) {
            return false;
        }

        fileDataBuilder.withContent(contentProducer.call());
        PartialFileData fileData = fileDataBuilder.create();
        return fileSync.convergeTo(context, fileData);
    }
}

@@ -238,60 +238,8 @@
 ext;
-%0Aimport org.glassfish.jersey.internal.util.Producer;
 %0A%0Aim
@@ -290,16 +290,52 @@
 le.Path;
+%0Aimport java.util.function.Supplier;
 %0A%0A/**%0A *
@@ -573,22 +573,22 @@
 e final 
-Produc
+Suppli
 er%3CStrin
@@ -692,22 +692,22 @@
 h path, 
-Produc
+Suppli
 er%3CStrin
@@ -1565,12 +1565,11 @@
 cer.
-call
+get
 ());

Change Producer to Supplier