<commit_before>// Copyright 2019 Oath Inc. 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;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import static com.yahoo.yolean.Exceptions.uncheck;
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;

/**
 * Rewrites default-env.txt files.
 *
 * @author bjorncs
 */
public class DefaultEnvRewriter {

    private final Map<String, Operation> operations = new TreeMap<>();
    private final Path defaultEnvFile;

    public DefaultEnvRewriter(Path defaultEnvFile) {
        this.defaultEnvFile = defaultEnvFile;
    }

    public DefaultEnvRewriter addOverride(String name, String value) {
        return addOperation("override", name, value);
    }

    public DefaultEnvRewriter addFallback(String name, String value) {
        return addOperation("fallback", name, value);
    }

    public DefaultEnvRewriter addUnset(String name) {
        return addOperation("unset", name, null);
    }

    private DefaultEnvRewriter addOperation(String action, String name, String value) {
        if (operations.containsKey(name)) {
            throw new IllegalArgumentException(String.format("Operation on variable '%s' already added", name));
        }
        operations.put(name, new Operation(action, name, value));
        return this;
    }

    public boolean converge() {
        List<String> defaultEnvLines = uncheck(() -> Files.readAllLines(defaultEnvFile));
        List<String> newDefaultEnvLines = new ArrayList<>();<commit_msg>Handle non-existing default-env.txt file<commit_after>// Copyright 2019 Oath Inc. 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;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.ifExists;
import static com.yahoo.yolean.Exceptions.uncheck;
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;

/**
 * Rewrites default-env.txt files.
 *
 * @author bjorncs
 */
public class DefaultEnvRewriter {

    private final Map<String, Operation> operations = new TreeMap<>();
    private final Path defaultEnvFile;

    public DefaultEnvRewriter(Path defaultEnvFile) {
        this.defaultEnvFile = defaultEnvFile;
    }

    public DefaultEnvRewriter addOverride(String name, String value) {
        return addOperation("override", name, value);
    }

    public DefaultEnvRewriter addFallback(String name, String value) {
        return addOperation("fallback", name, value);
    }

    public DefaultEnvRewriter addUnset(String name) {
        return addOperation("unset", name, null);
    }

    private DefaultEnvRewriter addOperation(String action, String name, String value) {
        if (operations.containsKey(name)) {
            throw new IllegalArgumentException(String.format("Operation on variable '%s' already added", name));
        }
        operations.put(name, new Operation(action, name, value));
        return this;
    }

    public boolean converge() {
        List<String> defaultEnvLines = ifExists(() -> Files.readAllLines(defaultEnvFile)).orElse(List.of());
        List<String> newDefaultEnvLines = new ArrayList<>();
{'fraction_non_alphanumeric': 0.10451436388508892, 'fraction_numerical': 0.003283173734610123, 'mean_word_length': 3.8105263157894735, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3025734', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1013, 'n_words': 275}