All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.libs.jaicore.basic.IOwnerBasedConfig Maven / Gradle / Ivy

There is a newer version: 0.2.7
Show newest version
package ai.libs.jaicore.basic;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.aeonbits.owner.Accessible;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Mutable;
import org.api4.java.common.control.IConfig;

public interface IOwnerBasedConfig extends Mutable, Accessible, IConfig {

	/**
	 * Reads properties of a config from a config file.
	 * @param file The file to read in as properties.
	 * @throws IOException Throws an IOException if an issue occurs while reading in the properties from the given file.
	 */
	@Override
	default IOwnerBasedConfig loadPropertiesFromFile(final File file) {
		return this.loadPropertiesFromFileArray(file);
	}

	default IOwnerBasedConfig loadPropertiesFromFileArray(final File... files) {
		try {
			List configLines = new ArrayList<>();
			for (File f : files) {
				configLines.addAll(FileUtil.readFileAsList(f));
			}
			return this.loadPropertiesFromList(configLines);
		} catch (IOException e) {
			throw new PropertiesLoadFailedException("Could not load properties from the given file.", e);
		}
	}

	/**
	 * Loads properties from a resource (instead of a file).
	 * @param resourcePath The path to the resource.
	 * @throws IOException Throws an IOException if an issue occurs while reading in the properties from the given resource.
	 */
	@Override
	default IOwnerBasedConfig loadPropertiesFromResource(final String resourcePath) throws IOException {
		// Get file from resources folder
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		String content = null;
		try (InputStream inputStream = classLoader.getResourceAsStream(resourcePath)) {
			ByteArrayOutputStream result = new ByteArrayOutputStream(1024);
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inputStream.read(buffer)) != -1) {
				result.write(buffer, 0, length);
			}
			content = result.toString(StandardCharsets.UTF_8.name());
		}

		if (content != null) {
			return this.loadPropertiesFromList(Arrays.asList(content.split("\n")));
		}
		return this;
	}

	/**
	 * Loads a properties config from a list of property assignments.
	 *
	 * @param propertiesList The list of property assignments.
	 */
	@Override
	default IOwnerBasedConfig loadPropertiesFromList(final List propertiesList) {
		for (String line : propertiesList) {
			line = line.trim();
			if (!line.contains("=") || line.startsWith("#")) {
				continue;
			}
			String[] split = line.split("=");
			if (split.length > 2) {
				throw new IllegalArgumentException("Property line " + line + " contains more than one \"=\" symbol and cannot be parsed.");
			}
			this.setProperty(split[0].trim(), split.length > 1 ? split[1].trim() : null);
		}
		return this;
	}

	default  T copy(final Class configInterface) {
		if (!configInterface.isInstance(this)) {
			throw new IllegalArgumentException("The config " + this + " does not implement the interface " + configInterface.getClass().getName());
		}
		T clone = ConfigFactory.create(configInterface);
		for (String property : this.propertyNames()) {
			clone.setProperty(property, this.getProperty(property));
		}
		return clone;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy