org.scijava.module.AbstractModuleInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scijava-common Show documentation
Show all versions of scijava-common Show documentation
SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2017 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
* Institute of Molecular Cell Biology and Genetics, University of
* Konstanz, and KNIME GmbH.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
*
* 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.
* #L%
*/
package org.scijava.module;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.scijava.AbstractUIDetails;
import org.scijava.util.Types;
/**
* Abstract superclass of {@link ModuleInfo} implementation.
*
* By default, {@link ModuleItem}s are stored in {@link HashMap}s and
* {@link ArrayList}s, internally.
*
*
* @author Curtis Rueden
*/
public abstract class AbstractModuleInfo extends AbstractUIDetails implements
ModuleInfo
{
/** Table of inputs, keyed on name. */
private HashMap> inputMap;
/** Table of outputs, keyed on name. */
private HashMap> outputMap;
/** Ordered list of input items. */
private ArrayList> inputList;
/** Ordered list of output items. */
private ArrayList> outputList;
/** Whether lazy initialization is complete. */
private boolean initialized;
// -- ModuleInfo methods --
@Override
public ModuleItem> getInput(final String name) {
return inputMap().get(name);
}
@Override
public ModuleItem getInput(final String name, final Class type) {
return castItem(getInput(name), type);
}
@Override
public ModuleItem> getOutput(final String name) {
return outputMap().get(name);
}
@Override
public ModuleItem getOutput(final String name, final Class type) {
return castItem(getOutput(name), type);
}
@Override
public Iterable> inputs() {
return Collections.unmodifiableList(inputList());
}
@Override
public Iterable> outputs() {
return Collections.unmodifiableList(outputList());
}
// -- Internal methods --
/**
* Parses input and output parameters. Intended to be overridden by concrete
* subclasses.
*/
protected void parseParameters() {
// NB: Do nothing by default.
}
/** Clears input and output parameters. */
protected void clearParameters() {
inputMap.clear();
outputMap.clear();
inputList.clear();
outputList.clear();
}
/**
* Adds an input. Intended to be called from overridden
* {@link #parseParameters()} methods.
*/
protected void registerInput(final ModuleItem> input) {
inputMap.put(input.getName(), input);
inputList.add(input);
}
/**
* Adds an output. Intended to be called from overridden
* {@link #parseParameters()} methods.
*/
protected void registerOutput(final ModuleItem> output) {
outputMap.put(output.getName(), output);
outputList.add(output);
}
/** Gets {@link #inputMap}, initializing if needed. */
protected Map> inputMap() {
if (!initialized) initParameters();
return inputMap;
}
/** Gets {@link #inputList}, initializing if needed. */
protected Map> outputMap() {
if (!initialized) initParameters();
return outputMap;
}
/** Gets {@link #outputMap}, initializing if needed. */
protected List> inputList() {
if (!initialized) initParameters();
return inputList;
}
/** Gets {@link #outputList}, initializing if needed. */
protected List> outputList() {
if (!initialized) initParameters();
return outputList;
}
// -- Helper methods --
private ModuleItem castItem(final ModuleItem> item,
final Class type)
{
final Class> itemType = item.getType();
// if (!type.isAssignableFrom(itemType)) {
final Class> saneItemType = Types.box(itemType);
if (!Types.isAssignable(type, saneItemType)) {
throw new IllegalArgumentException("Type " + type.getName() +
" is incompatible with item of type " + itemType.getName());
}
@SuppressWarnings("unchecked")
final ModuleItem typedItem = (ModuleItem) item;
return typedItem;
}
// -- Helper methods - lazy initialization --
/** Initializes data structures and parses parameters. */
private synchronized void initParameters() {
if (initialized) return; // already initialized
inputMap = new HashMap<>();
outputMap = new HashMap<>();
inputList = new ArrayList<>();
outputList = new ArrayList<>();
parseParameters();
initialized = true;
}
}