Skip to content

Commit a9b393c

Browse files
committed
Migrate PTService hierarchy from ImageJ
This is the same code previously merged to imagej.git with c15a02888b8e5065c8730381324c154a9f66e134, viewable online at: imagej/ImageJ@c15a028 These classes are of general utility beyond ImageJ; e.g., SCIFIO and even SciJava Common itself will be able to make use of them. Here is a reposted narrative describing the work: This code introduces additional layers into the service and plugin type hierarchies. It was done because many services and plugins were starting to express very similar patterns, and include copy-and-pasted boilerplate code. The following layers are now available: 1) A PTService manages a particular type of plugin. It uses the PluginService to discover available plugins of that type, and provides accessor methods to access the list easily. Examples: CommandService, OptionsService 2) A SingletonService is a PTService that creates and maintains a list of SingletonPlugins -- one singleton instance per plugin discovered. Examples: CalculatorService, ThresholdService, PlatformService, ToolService 3) A TypedService is a PTService whose plugins are typed on a particular generic parameter, which defines a type of data object relevant to those plugins. Both TypedService and TypedPlugin implement the Typed interface, which provides a supports method, so you can inquire whether a particular data object is supported by a specific plugin, as well as by the TypedService as a whole. Examples: None (but see SingletonService and WrapperService below) 4) A WrapperService is a TypedService whose plugins wrap data objects of their given type. These WrapperPlugins each know how to "wrap" (via the WrapperPlugin#set method) a subset of data objects of their given type. The WrapperService can thus provide a master create method which takes a data object and returns a new instance of the most appropriate plugin, determined by iterating over the available WrapperPlugins in priority order. Example: WidgetService 5) A HandlerService is both a SingletonService and TypedService, which maintains a list of HandlerPlugins (which are typed singletons, of course). These plugins each know how to "handle" a subset of data objects of their given type. The HandlerService can thus handle data objects of that type by iterating over the available HandlerPlugins in priority order. The exact handling mechanism is unspecified, however, and will depend on the specific type of HandlerPlugin. Examples: TextService, DragAndDropService
1 parent acb31d0 commit a9b393c

17 files changed

Lines changed: 1145 additions & 0 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava;
37+
38+
/**
39+
* An object with an associated type (i.e., {@link Class}), which can be queried
40+
* at runtime.
41+
*
42+
* @author Curtis Rueden
43+
*/
44+
public interface Typed<T> {
45+
46+
/**
47+
* Gets whether this object is compatible with the given data object.
48+
* <p>
49+
* Typically, this will be the case when {@code data.getClass()} is assignable
50+
* to the type associated with the object (i.e., the one returned by
51+
* {@link #getType()}). But individual implementations may have other
52+
* requirements beyond class assignability.
53+
* </p>
54+
*/
55+
boolean supports(T data);
56+
57+
/** Gets the type associated with the object. */
58+
Class<T> getType();
59+
60+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.plugin;
37+
38+
/**
39+
* Abstract base class for {@link HandlerService}s.
40+
*
41+
* @author Curtis Rueden
42+
* @param <DT> Base data type handled by the handlers.
43+
* @param <PT> Plugin type of the handlers.
44+
*/
45+
public abstract class AbstractHandlerService<DT, PT extends HandlerPlugin<DT>>
46+
extends AbstractSingletonService<PT> implements HandlerService<DT, PT>
47+
{
48+
49+
// -- HandlerService methods --
50+
51+
@Override
52+
public PT getHandler(final DT data) {
53+
for (final PT handler : getInstances()) {
54+
if (handler.supports(data)) return handler;
55+
}
56+
return null;
57+
}
58+
59+
// -- Typed methods --
60+
61+
@Override
62+
public boolean supports(final DT data) {
63+
return getHandler(data) != null;
64+
}
65+
66+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.plugin;
37+
38+
import java.util.List;
39+
40+
import org.scijava.service.AbstractService;
41+
42+
/**
43+
* Abstract base class for {@link PTService}s.
44+
*
45+
* @author Curtis Rueden
46+
* @param <PT> Plugin type of the {@link SciJavaPlugin}s being managed.
47+
*/
48+
public abstract class AbstractPTService<PT extends SciJavaPlugin> extends
49+
AbstractService implements PTService<PT>
50+
{
51+
52+
@Parameter
53+
private PluginService pluginService;
54+
55+
// -- PTService methods --
56+
57+
@Override
58+
public PluginService getPluginService() {
59+
return pluginService;
60+
}
61+
62+
@Override
63+
public List<PluginInfo<PT>> getPlugins() {
64+
return pluginService.getPluginsOfType(getPluginType());
65+
}
66+
67+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.plugin;
37+
38+
import java.util.Collections;
39+
import java.util.List;
40+
41+
import org.scijava.log.LogService;
42+
43+
/**
44+
* Abstract base class for {@link SingletonService}s.
45+
*
46+
* @author Curtis Rueden
47+
* @param <PT> Plugin type of the {@link SciJavaPlugin}s being managed.
48+
*/
49+
public abstract class AbstractSingletonService<PT extends SingletonPlugin>
50+
extends AbstractPTService<PT> implements SingletonService<PT>
51+
{
52+
53+
@Parameter
54+
private LogService log;
55+
56+
// TODO: Listen for PluginsAddedEvent and PluginsRemovedEvent
57+
// and update the list of singletons accordingly.
58+
59+
/** List of singleton plugin instances. */
60+
private List<PT> instances;
61+
62+
// -- SingletonService methods --
63+
64+
@Override
65+
public List<PT> getInstances() {
66+
if (instances == null) {
67+
createInstances();
68+
}
69+
return instances;
70+
}
71+
72+
// -- Helper methods --
73+
74+
private void createInstances() {
75+
instances =
76+
Collections.unmodifiableList(getPluginService().createInstancesOfType(
77+
getPluginType()));
78+
79+
log.info("Found " + instances.size() + " " +
80+
getPluginType().getSimpleName() + " plugins.");
81+
}
82+
83+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.plugin;
37+
38+
import org.scijava.Typed;
39+
40+
/**
41+
* Abstract base class for {@link TypedPlugin}s.
42+
*
43+
* @author Curtis Rueden
44+
* @param <D> Data type associated with the plugin.
45+
* @see Typed
46+
*/
47+
public abstract class AbstractTypedPlugin<D> extends SortablePlugin implements
48+
TypedPlugin<D>
49+
{
50+
51+
// -- Typed methods --
52+
53+
@Override
54+
public boolean supports(final D data) {
55+
return true;
56+
}
57+
58+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.plugin;
37+
38+
/**
39+
* Abstract base class for {@link TypedService}s.
40+
*
41+
* @author Curtis Rueden
42+
* @param <DT> Base data type of the {@link TypedPlugin}s.
43+
* @param <PT> Plugin type of the {@link TypedPlugin}s.
44+
*/
45+
public abstract class AbstractTypedService<DT, PT extends TypedPlugin<DT>>
46+
extends AbstractPTService<PT> implements TypedService<DT, PT>
47+
{
48+
// NB: No implementation needed.
49+
}

0 commit comments

Comments
 (0)