<commit_before>package me.coley.recaf.config;

import java.io.File;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.eclipsesource.json.WriterConfig;

import me.coley.recaf.Logging;
import me.coley.recaf.util.Files;
import me.coley.recaf.util.Reflect;

/**
 * Persistent config handler. Saves on exit, loads on startup.
 * 
 * @author Matt
 */
public abstract class Config {
	/**
	 * Map of config instances.
	 */
	private final static Map<Class<?>, Config> instances = new HashMap<>();
	/**
	 * File to store data in.
	 */
	private final File confFile;

	protected Config(String fileName) {
		confFile = new File(fileName + ".json");
		registerInstance();
		addSaveHook();
		setAccessible();
	}

	/**
	 * Save current settings to configuration.
	 */
	protected final void save() {
		try {
			if (!confFile.exists() && !confFile.createNewFile()) {
				// Return if config file cannot be found or cannot be created if
				// it does not exist.
				return;
			}
			JsonObject json = Json.object();
			for (Field field : Reflect.fields(getClass())) {
				// Skip if field does not represent a config option.
				String name = getValueName(field);
				if (name == null) {
					continue;
				}
				// Access via reflection, add value to json object.
				// field.get(this);
				Object value = Reflect.get(this, field);
				if (field.getType().equals(boolean.class)) {
					json.set(name, (boolean) value);
				} else if (field.getType().equals(int.class)) {
					json.set(name, (int) value);
				} else if (field.getType().equals(long.class)) {
					json.set(name, (long) value);
				} else if (field.getType().equals(float.class)) {
					json.set(name, (float) value);
				} else if (field.getType().equals(double.class)) {
					json.set(name, (double) value);
				} else if (field.getType().equals(String.class)) {
					json.set(name, (String) value);
				} else {
					JsonValue converted = convert(field.getType(), value);
					if (converted != null) {
						json.set(name, converted);
					}
				}
			}
			// Write json to file
			StringWriter w = new StringWriter();
			json.writeTo(w, WriterConfig.PRETTY_PRINT);
			Files.writeFile(confFile.getAbsolutePath(), w.toString());
		} catch (Exception e) {
			Logging.error(e);
		}
	}

	/**
	 * Load from configuration.
	 */
	protected final void load() {
		// Return if the config file does not exist.
		if (!confFile.exists()) {
			return;
		}
		// Read values from file
		try {
			JsonObject json = Json.parse(Files.readFile(confFile.getAbsolutePath())).asObject();
			for (Field field : Reflect.fields(getClass())) {
				String name = getValueName(field);
				if (name == null) {
					continue;
				}
				JsonValue value = json.get(name);
				if (value == null) {
					continue;
				}

				if (field.getType().equals(boolean.class)) {
					field.set(this, value.asBoolean());<commit_msg>Implement #104 - Move config files to subdirectory<commit_after>package me.coley.recaf.config;

import java.io.File;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.eclipsesource.json.WriterConfig;

import me.coley.recaf.Logging;
import me.coley.recaf.util.Files;
import me.coley.recaf.util.Reflect;

/**
 * Persistent config handler. Saves on exit, loads on startup.
 * 
 * @author Matt
 */
public abstract class Config {
	/**
	 * Config subfolder to reduce clutter adjacent to the recaf jar.
	 */
	private final static File confFolder = new File("RCconfig");
	/**
	 * Map of config instances.
	 */
	private final static Map<Class<?>, Config> instances = new HashMap<>();
	/**
	 * File to store data in.
	 */
	private final File confFile;

	protected Config(String fileName) {
		confFile = new File(confFolder, fileName + ".json");
		registerInstance();
		addSaveHook();
		setAccessible();
	}

	/**
	 * Save current settings to configuration.
	 */
	protected final void save() {
		try {
			if (!confFolder.exists()) {
				// Create subfolder for config files.
				confFolder.mkdirs();
			}
			if (!confFile.exists() && !confFile.createNewFile()) {
				// Return if config file cannot be found or cannot be created if
				// it does not exist.
				return;
			}
			JsonObject json = Json.object();
			for (Field field : Reflect.fields(getClass())) {
				// Skip if field does not represent a config option.
				String name = getValueName(field);
				if (name == null) {
					continue;
				}
				// Access via reflection, add value to json object.
				// field.get(this);
				Object value = Reflect.get(this, field);
				if (field.getType().equals(boolean.class)) {
					json.set(name, (boolean) value);
				} else if (field.getType().equals(int.class)) {
					json.set(name, (int) value);
				} else if (field.getType().equals(long.class)) {
					json.set(name, (long) value);
				} else if (field.getType().equals(float.class)) {
					json.set(name, (float) value);
				} else if (field.getType().equals(double.class)) {
					json.set(name, (double) value);
				} else if (field.getType().equals(String.class)) {
					json.set(name, (String) value);
				} else {
					JsonValue converted = convert(field.getType(), value);
					if (converted != null) {
						json.set(name, converted);
					}
				}
			}
			// Write json to file
			StringWriter w = new StringWriter();
			json.writeTo(w, WriterConfig.PRETTY_PRINT);
			Files.writeFile(confFile.getAbsolutePath(), w.toString());
		} catch (Exception e) {
			Logging.error(e);
		}
	}

	/**
	 * Load from configuration.
	 */
	protected final void load() {
		// Return if the config subfolder does not exist.
		if (!confFolder.exists()) {
			return;
		}
		// Return if the config file does not exist.
		if (!confFile.exists()) {
			return;
		}
		// Read values from file
		try {
			JsonObject json = Json.parse(Files.readFile(confFile.getAbsolutePath())).asObject();
			for (Field field : Reflect.fields(getClass())) {
				String name = getValueName(field);
				if (name == null) {
					continue;
				}
				JsonValue value = json.get(name);
				if (value == null) {
					continue;
				}

				if (field.getType().equals(boolean.class)) {
					field.set(this, value.asBoolean());
{'fraction_non_alphanumeric': 0.14356666149309327, 'fraction_numerical': 0.0004656216048424647, 'mean_word_length': 3.1493882807469413, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1672836', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2034, 'n_words': 540}