<commit_before>package prefs;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.egit.github.core.RepositoryId;
import util.FileHelper;
import util.HTLog;
import util.JsonHelper;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Represents persistent user configuration.
 *
 * The Preferences class is the facade of all configuration and preferences information. All preferences and config
 * settings can be set in this class.
 *
 * Use either Preferences.create() or Preferences.load() to obtain a Preferences instance.
 *
 * Overrides PMD's recommendation that this class should be final.
 * It cannot be as we need to mock it.
 * 
 */
public class Preferences { // NOPMD

    private static final Logger logger = LogManager.getLogger(Preferences.class.getName());

    public static final String DIRECTORY = "settings";

    public static final String SESSION_CONFIG_FILENAME = "global.json";
    public static final String USER_CONFIG_FILENAME = "user.json";

    private static final String DEFAULT_FILE_CONTENTS = "{}";

    private final String configDirectory;
    private final String sessionConfigFileName;
    private final String userConfigFileName;

    private final SessionConfig sessionConfig;
    private final UserConfig userConfig;

    /**
     * Initialises a Preferences instance that contains the configurations in the config files specified.
     * @param configDirectory The directory that the config files are held in.
     * @param sessionConfigFileName The name of the session config file to load.
     * @param userConfigFileName The name of the user config file to load.
     * @return The initialised Preferences instance.
     */
    public static Preferences load(String configDirectory, String sessionConfigFileName, String userConfigFileName) {
        return new Preferences(configDirectory, sessionConfigFileName, userConfigFileName, false);
    }

    /**
     * Initialises a Preferences instance that creates new config files.
     * @param configDirectory The directory that the config files are held in.
     * @param sessionConfigFileName The name of the session config file to create.
     * @param userConfigFileName The name of the user config file to create.
     * @return The initialised Preferences instance.
     */
    public static Preferences create(String configDirectory, String sessionConfigFileName, String userConfigFileName) {
        return new Preferences(configDirectory, sessionConfigFileName, userConfigFileName, true);
    }

    /**
     * @param configDirectory The directory that the config files are held in.
     * @param sessionConfigFileName The name of the session config file
     * @param userConfigFileName The name of the user config file
     * @param ignoreExisting True if existing config file is to be ignored.
     */
    private Preferences(String configDirectory,
                        String sessionConfigFileName,
                        String userConfigFileName,
                        boolean ignoreExisting) {
        this.configDirectory = configDirectory;
        this.sessionConfigFileName = sessionConfigFileName;
        this.userConfigFileName = userConfigFileName;

        if (ignoreExisting) {
            this.sessionConfig = createConfig(DEFAULT_FILE_CONTENTS, SessionConfig.class);
            this.userConfig = createConfig(DEFAULT_FILE_CONTENTS, UserConfig.class);
        } else {
            this.sessionConfig = loadConfig(configDirectory, sessionConfigFileName, SessionConfig.class);
            this.userConfig = loadConfig(configDirectory, userConfigFileName, UserConfig.class);
        }
    }

    /**
     * Creates a new Config instance
     * @param contents The JSON representation of an contents of the Config object
     * @param configClass The class of the Config instance
     * @return The created Config instance
     */
    private <T> T createConfig(String contents, Class<T> configClass) {
        return JsonHelper.fromJsonString(contents, configClass);
    }

    /**
     * Loads a Config instance from a file on disk
     * @param configDirectory The directory that the config file is held in.
     * @param configFileName The filename of the config file on disk
     * @param configClass The class of the config to load
     * @return The loaded Config instance
     */
    private <T> T loadConfig(String configDirectory, String configFileName, Class<T> configClass) {
        String fileContents = DEFAULT_FILE_CONTENTS;
        if (FileHelper.isFileExists(configDirectory, configFileName)) {
            try {
                fileContents = FileHelper.getFileContents(configDirectory, configFileName);
            } catch (IOException e) {
                // if we can't read the file, just ignore the existing files
                HTLog.error(logger, e);
                logger.error(e.toString());
                fileContents = DEFAULT_FILE_CONTENTS;
            }
        }

        return createConfig(fileContents, configClass);
    }

    /**
     * Saves the session and user configs to file
     */
    private void save() {<commit_msg>Renamed booleans and removed duplicated code<commit_after>package prefs;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.egit.github.core.RepositoryId;
import util.FileHelper;
import util.HTLog;
import util.JsonHelper;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Represents persistent user configuration.
 *
 * The Preferences class is the facade of all configuration and preferences information. All preferences and config
 * settings can be set in this class.
 *
 * Use either Preferences.create() or Preferences.load() to obtain a Preferences instance.
 *
 * Overrides PMD's recommendation that this class should be final.
 * It cannot be as we need to mock it.
 * 
 */
public class Preferences { // NOPMD

    private static final Logger logger = LogManager.getLogger(Preferences.class.getName());

    public static final String DIRECTORY = "settings";

    public static final String SESSION_CONFIG_FILENAME = "global.json";
    public static final String USER_CONFIG_FILENAME = "user.json";

    private static final String DEFAULT_FILE_CONTENTS = "{}";

    private final String configDirectory;
    private final String sessionConfigFileName;
    private final String userConfigFileName;

    private final SessionConfig sessionConfig;
    private final UserConfig userConfig;

    /**
     * Initialises a Preferences instance that contains the configurations in the config files specified.
     * @param configDirectory The directory that the config files are held in.
     * @param sessionConfigFileName The name of the session config file to load.
     * @param userConfigFileName The name of the user config file to load.
     * @return The initialised Preferences instance.
     */
    public static Preferences load(String configDirectory, String sessionConfigFileName, String userConfigFileName) {
        return new Preferences(configDirectory, sessionConfigFileName, userConfigFileName, false);
    }

    /**
     * Initialises a Preferences instance that creates new config files.
     * @param configDirectory The directory that the config files are held in.
     * @param sessionConfigFileName The name of the session config file to create.
     * @param userConfigFileName The name of the user config file to create.
     * @return The initialised Preferences instance.
     */
    public static Preferences create(String configDirectory, String sessionConfigFileName, String userConfigFileName) {
        return new Preferences(configDirectory, sessionConfigFileName, userConfigFileName, true);
    }

    /**
     * @param configDirectory The directory that the config files are held in.
     * @param sessionConfigFileName The name of the session config file
     * @param userConfigFileName The name of the user config file
     * @param isIgnoreExisting True if existing config file is to be ignored.
     */
    private Preferences(String configDirectory,
                        String sessionConfigFileName,
                        String userConfigFileName,
                        boolean isIgnoreExisting) {
        this.configDirectory = configDirectory;
        this.sessionConfigFileName = sessionConfigFileName;
        this.userConfigFileName = userConfigFileName;

        if (isIgnoreExisting) {
            this.sessionConfig = createConfig(DEFAULT_FILE_CONTENTS, SessionConfig.class);
            this.userConfig = createConfig(DEFAULT_FILE_CONTENTS, UserConfig.class);
        } else {
            this.sessionConfig = loadConfig(configDirectory, sessionConfigFileName, SessionConfig.class);
            this.userConfig = loadConfig(configDirectory, userConfigFileName, UserConfig.class);
        }
    }

    /**
     * Creates a new Config instance
     * @param contents The JSON representation of an contents of the Config object
     * @param configClass The class of the Config instance
     * @return The created Config instance
     */
    private <T> T createConfig(String contents, Class<T> configClass) {
        return JsonHelper.fromJsonString(contents, configClass);
    }

    /**
     * Loads a Config instance from a file on disk
     * @param configDirectory The directory that the config file is held in.
     * @param configFileName The filename of the config file on disk
     * @param configClass The class of the config to load
     * @return The loaded Config instance
     */
    private <T> T loadConfig(String configDirectory, String configFileName, Class<T> configClass) {
        String fileContents = DEFAULT_FILE_CONTENTS;
        if (FileHelper.isFileExists(configDirectory, configFileName)) {
            try {
                fileContents = FileHelper.getFileContents(configDirectory, configFileName);
            } catch (IOException e) {
                // if we can't read the file, just ignore the existing files
                HTLog.error(logger, e);
                logger.error(e.toString());
            }
        }

        return createConfig(fileContents, configClass);
    }

    /**
     * Saves the session and user configs to file
     */
    private void save() {
{'fraction_non_alphanumeric': 0.06497094407926074, 'fraction_numerical': 0.0003810612555968372, 'mean_word_length': 3.144492696407422, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5698611', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2465, 'n_words': 945}