<commit_before>//
// PluginModuleInfo.java
//

/*
ImageJ software for multidimensional image processing and analysis.

Copyright (c) 2010, ImageJDev.org.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of the ImageJDev.org developers nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package imagej.plugin;

import imagej.event.Events;
import imagej.module.ItemVisibility;
import imagej.module.Module;
import imagej.module.ModuleException;
import imagej.module.ModuleInfo;
import imagej.module.ModuleItem;
import imagej.module.event.ModuleInfoUpdatedEvent;
import imagej.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A collection of metadata about a particular {@link RunnablePlugin}. Unlike
 * its more general superclass {@link PluginInfo}, a
 * <code>PluginModuleInfo</code> implements {@link ModuleInfo}, allowing it to
 * describe and instantiate the plugin in {@link Module} form.
 * 
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @see ModuleInfo
 * @see PluginModule
 * @see RunnablePlugin
 */
public class PluginModuleInfo<R extends RunnablePlugin> extends PluginInfo<R>
	implements ModuleInfo
{

	/** List of items with fixed, preset values. */
	private Map<String, Object> presets;

	/** Factory used to create a module associated with the associated plugin. */
	private PluginModuleFactory factory;

	/**
	 * Flag indicating whether the plugin parameters have been parsed. Parsing the
	 * parameters requires loading the plugin class, so doing so is deferred until
	 * information about the parameters is actively needed.
	 */
	private boolean paramsParsed;

	/** Table of inputs, keyed on name. */
	private final Map<String, ModuleItem<?>> inputMap =
		new HashMap<String, ModuleItem<?>>();

	/** Table of outputs, keyed on name. */
	private final Map<String, ModuleItem<?>> outputMap =
		new HashMap<String, ModuleItem<?>>();

	/** Ordered list of input items. */
	private final List<ModuleItem<?>> inputList = new ArrayList<ModuleItem<?>>();

	/** Ordered list of output items. */
	private final List<ModuleItem<?>> outputList =
		new ArrayList<ModuleItem<?>>();

	// -- Constructors --

	public PluginModuleInfo(final String className, final Class<R> pluginType) {
		super(className, pluginType);
		setPresets(null);
		setPluginModuleFactory(null);
	}

	public PluginModuleInfo(final String className, final Class<R> pluginType,
		final Plugin plugin)
	{
		super(className, pluginType, plugin);
		setPresets(null);
		setPluginModuleFactory(null);
	}

	// -- RunnablePluginInfo methods --

	/** Sets the table of items with fixed, preset values. */
	public void setPresets(final Map<String, Object> presets) {
		if (presets == null) {
			this.presets = new HashMap<String, Object>();
		}
		else {
			this.presets = presets;
		}
	}

	/** Gets the table of items with fixed, preset values. */
	public Map<String, Object> getPresets() {
		return presets;
	}

	/** Sets the factory used to construct {@link PluginModule} instances. */
	public void setPluginModuleFactory(final PluginModuleFactory factory) {
		if (factory == null) {
			this.factory = new DefaultPluginModuleFactory();
		}
		else {
			this.factory = factory;
		}
	}

	/** Gets the factory used to construct {@link PluginModule} instances. */
	public PluginModuleFactory getPluginModuleFactory() {
		return factory;
	}

	// -- ModuleInfo methods --

	@Override
	public PluginModuleItem<?> getInput(final String name) {
		parseParams();
		return (PluginModuleItem<?>) inputMap.get(name);
	}

	@Override
	public PluginModuleItem<?> getOutput(final String name) {
		parseParams();
		return (PluginModuleItem<?>) outputMap.get(name);
	}

	@Override
	public Iterable<ModuleItem<?>> inputs() {
		parseParams();
		return Collections.unmodifiableList(inputList);
	}

	@Override
	public Iterable<ModuleItem<?>> outputs() {
		parseParams();
		return Collections.unmodifiableList(outputList);
	}

	@Override
	public String getDelegateClassName() {
		return getClassName();
	}

	@Override
	public PluginModule<R> createModule() throws ModuleException {
		return factory.createModule(this);
	}

	@Override
	public boolean canPreview() {
		final Class<?> pluginClass = loadPluginClass();
		if (pluginClass == null) return false;
		return PreviewPlugin.class.isAssignableFrom(pluginClass);<commit_msg>Widen return type of createModule() method.<commit_after>//
// PluginModuleInfo.java
//

/*
ImageJ software for multidimensional image processing and analysis.

Copyright (c) 2010, ImageJDev.org.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of the ImageJDev.org developers nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package imagej.plugin;

import imagej.event.Events;
import imagej.module.ItemVisibility;
import imagej.module.Module;
import imagej.module.ModuleException;
import imagej.module.ModuleInfo;
import imagej.module.ModuleItem;
import imagej.module.event.ModuleInfoUpdatedEvent;
import imagej.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A collection of metadata about a particular {@link RunnablePlugin}. Unlike
 * its more general superclass {@link PluginInfo}, a
 * <code>PluginModuleInfo</code> implements {@link ModuleInfo}, allowing it to
 * describe and instantiate the plugin in {@link Module} form.
 * 
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @see ModuleInfo
 * @see PluginModule
 * @see RunnablePlugin
 */
public class PluginModuleInfo<R extends RunnablePlugin> extends PluginInfo<R>
	implements ModuleInfo
{

	/** List of items with fixed, preset values. */
	private Map<String, Object> presets;

	/** Factory used to create a module associated with the associated plugin. */
	private PluginModuleFactory factory;

	/**
	 * Flag indicating whether the plugin parameters have been parsed. Parsing the
	 * parameters requires loading the plugin class, so doing so is deferred until
	 * information about the parameters is actively needed.
	 */
	private boolean paramsParsed;

	/** Table of inputs, keyed on name. */
	private final Map<String, ModuleItem<?>> inputMap =
		new HashMap<String, ModuleItem<?>>();

	/** Table of outputs, keyed on name. */
	private final Map<String, ModuleItem<?>> outputMap =
		new HashMap<String, ModuleItem<?>>();

	/** Ordered list of input items. */
	private final List<ModuleItem<?>> inputList = new ArrayList<ModuleItem<?>>();

	/** Ordered list of output items. */
	private final List<ModuleItem<?>> outputList =
		new ArrayList<ModuleItem<?>>();

	// -- Constructors --

	public PluginModuleInfo(final String className, final Class<R> pluginType) {
		super(className, pluginType);
		setPresets(null);
		setPluginModuleFactory(null);
	}

	public PluginModuleInfo(final String className, final Class<R> pluginType,
		final Plugin plugin)
	{
		super(className, pluginType, plugin);
		setPresets(null);
		setPluginModuleFactory(null);
	}

	// -- RunnablePluginInfo methods --

	/** Sets the table of items with fixed, preset values. */
	public void setPresets(final Map<String, Object> presets) {
		if (presets == null) {
			this.presets = new HashMap<String, Object>();
		}
		else {
			this.presets = presets;
		}
	}

	/** Gets the table of items with fixed, preset values. */
	public Map<String, Object> getPresets() {
		return presets;
	}

	/** Sets the factory used to construct {@link PluginModule} instances. */
	public void setPluginModuleFactory(final PluginModuleFactory factory) {
		if (factory == null) {
			this.factory = new DefaultPluginModuleFactory();
		}
		else {
			this.factory = factory;
		}
	}

	/** Gets the factory used to construct {@link PluginModule} instances. */
	public PluginModuleFactory getPluginModuleFactory() {
		return factory;
	}

	// -- ModuleInfo methods --

	@Override
	public PluginModuleItem<?> getInput(final String name) {
		parseParams();
		return (PluginModuleItem<?>) inputMap.get(name);
	}

	@Override
	public PluginModuleItem<?> getOutput(final String name) {
		parseParams();
		return (PluginModuleItem<?>) outputMap.get(name);
	}

	@Override
	public Iterable<ModuleItem<?>> inputs() {
		parseParams();
		return Collections.unmodifiableList(inputList);
	}

	@Override
	public Iterable<ModuleItem<?>> outputs() {
		parseParams();
		return Collections.unmodifiableList(outputList);
	}

	@Override
	public String getDelegateClassName() {
		return getClassName();
	}

	@Override
	public Module createModule() throws ModuleException {
		return factory.createModule(this);
	}

	@Override
	public boolean canPreview() {
		final Class<?> pluginClass = loadPluginClass();
		if (pluginClass == null) return false;
		return PreviewPlugin.class.isAssignableFrom(pluginClass);
{'fraction_non_alphanumeric': 0.09267370952503651, 'fraction_numerical': 0.000687108133642532, 'mean_word_length': 4.563306258958433, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 80, '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': '935752', 'n_tokens_mistral': 3366, 'n_tokens_neox': 3084, 'n_words': 1194}