<commit_before>package de.tudresden.inf.lat.tabula.extension;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 * This models an extension that can execute other extensions.
 *
 */
public class ExtensionManager implements Extension {

	public static final String NAME = "ext";
	public static final String HELP = "extension manager";
	public static final int REQUIRED_ARGUMENTS = 1;
	public static final char NEW_LINE = '\n';
	public static final char SPACE = ' ';

	private final List<Extension> extensions = new ArrayList<>();
	private final Map<String, Extension> extensionMap = new TreeMap<>();

	/**
	 * Constructs an extension manager.
	 * 
	 * @param extensions
	 *            list of extensions
	 */
	public ExtensionManager(List<Extension> extensions) {
		if (Objects.nonNull(extensions)) {
			this.extensions.addAll(extensions);
			extensions.forEach(extension -> {
				String key = extension.getExtensionName();
				if (this.extensionMap.containsKey(key)) {
					throw new ExtensionException("Only one implementation is allowed for each extension, and '" + key
							+ "' was at least twice.");
				}
				this.extensionMap.put(key, extension);
			});
		}
	}

	@Override
	public boolean process(List<String> arguments) {
		if (Objects.isNull(arguments) || arguments.size() < REQUIRED_ARGUMENTS) {
			return false;
		} else {
			String command = arguments.get(0);
			List<String> newArguments = new ArrayList<>();
			newArguments.addAll(arguments);
			newArguments.remove(0);
			Extension extension = this.extensionMap.get(command);
			if (Objects.isNull(extension)) {
				throw new ExtensionException("Extension '" + command + "' was not found.");
			} else {
				extension.process(newArguments);
				return true;
			}
		}
	}

	@Override
	public String getExtensionName() {
		return NAME;
	}

	@Override
	public String getHelp() {
		StringBuffer sbuf = new StringBuffer();
		this.extensions.forEach(extension -> {
			sbuf.append(extension.getExtensionName());
			sbuf.append(SPACE);
			sbuf.append(extension.getHelp());<commit_msg>Improve exception handling in extension manager<commit_after>package de.tudresden.inf.lat.tabula.extension;

import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import de.tudresden.inf.lat.tabula.datatype.ParseException;

/**
 * This models an extension that can execute other extensions.
 *
 */
public class ExtensionManager implements Extension {

	public static final String NAME = "ext";
	public static final String HELP = "extension manager";
	public static final int REQUIRED_ARGUMENTS = 1;
	public static final char NEW_LINE = '\n';
	public static final char SPACE = ' ';

	private final List<Extension> extensions = new ArrayList<>();
	private final Map<String, Extension> extensionMap = new TreeMap<>();

	/**
	 * Constructs an extension manager.
	 * 
	 * @param extensions
	 *            list of extensions
	 */
	public ExtensionManager(List<Extension> extensions) {
		if (Objects.nonNull(extensions)) {
			this.extensions.addAll(extensions);
			extensions.forEach(extension -> {
				String key = extension.getExtensionName();
				if (this.extensionMap.containsKey(key)) {
					throw new ExtensionException("Only one implementation is allowed for each extension, and '" + key
							+ "' was at least twice.");
				}
				this.extensionMap.put(key, extension);
			});
		}
	}

	@Override
	public boolean process(List<String> arguments) {
		Objects.requireNonNull(arguments);
		if (arguments.size() < REQUIRED_ARGUMENTS) {
			throw new ExtensionException("No extension name was given.");
		} else {
			String command = arguments.get(0);
			List<String> newArguments = new ArrayList<>();
			newArguments.addAll(arguments);
			newArguments.remove(0);
			Extension extension = this.extensionMap.get(command);
			if (Objects.isNull(extension)) {
				throw new ExtensionException("Extension '" + command + "' was not found.");
			} else if (newArguments.size() < extension.getRequiredArguments()) {
				throw new ExtensionException("Insufficient number of arguments for extension '" + command + "'.");
			} else {
				try {
					return extension.process(newArguments);
				} catch (ParseException | UncheckedIOException e) {
					throw new ExtensionException(e);
				}
			}
		}
	}

	@Override
	public String getExtensionName() {
		return NAME;
	}

	@Override
	public String getHelp() {
		StringBuffer sbuf = new StringBuffer();
		this.extensions.forEach(extension -> {
			sbuf.append(extension.getExtensionName());
			sbuf.append(SPACE);
			sbuf.append(extension.getHelp());
{'fraction_non_alphanumeric': 0.11677282377919321, 'fraction_numerical': 0.0012738853503184713, 'mean_word_length': 4.065591397849462, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, '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': '4744484', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1306, 'n_words': 359}