Skip to content

Commit 973713f

Browse files
committed
Merge pull request #108 from scijava/create-conversion-service
Refactors the conversion methods of ConversionUtils to an extensible, Handler-patterned ConvertService.
2 parents f76c9e3 + 8215e54 commit 973713f

14 files changed

+1152
-379
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import java.lang.reflect.Type;
35+
36+
import org.scijava.plugin.AbstractHandlerService;
37+
import org.scijava.util.ConversionUtils;
38+
39+
/**
40+
* Abstract superclass for {@link ConvertService} implementations. Sets
41+
* this service as the active delegate service in {@link ConversionUtils}.
42+
*
43+
* @author Mark Hiner
44+
*/
45+
public abstract class AbstractConvertService extends
46+
AbstractHandlerService<ConversionRequest, Converter> implements
47+
ConvertService
48+
{
49+
50+
// -- ConversionService methods --
51+
52+
@Override
53+
public Object convert(Object src, Type dest) {
54+
return convert(new ConversionRequest(src, dest));
55+
}
56+
57+
@Override
58+
public <T> T convert(Object src, Class<T> dest) {
59+
// NB: repeated code with convert(ConversionRequest), because the
60+
// handler's convert method respects the T provided
61+
Converter handler = getHandler(src, dest);
62+
return handler == null ? null : handler.convert(src, dest);
63+
}
64+
65+
@Override
66+
public Object convert(ConversionRequest request) {
67+
Converter handler = getHandler(request);
68+
return handler == null ? null : handler.convert(request);
69+
}
70+
71+
// -- Service methods --
72+
73+
@Override
74+
public void initialize() {
75+
ConversionUtils.setDelegateService(this, getPriority());
76+
}
77+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import org.scijava.plugin.AbstractHandlerPlugin;
35+
36+
/**
37+
* Abstract superclass for {@link Converter} plugins. Performs
38+
* appropriate dispatching of {@link #canConvert(ConversionRequest)} and
39+
* {@link #convert(ConversionRequest)} calls based on the actual state of the
40+
* given {@link ConversionRequest}.
41+
* <p>
42+
* Note that the {@link #supports(ConversionRequest)} method is overridden as
43+
* well, to delegate to the appropriate {@link #canConvert}.
44+
* </p>
45+
*
46+
* @author Mark Hiner
47+
*/
48+
public abstract class AbstractConverter extends
49+
AbstractHandlerPlugin<ConversionRequest> implements Converter
50+
{
51+
52+
// -- ConversionHandler methods --
53+
54+
@Override
55+
public boolean canConvert(final ConversionRequest request) {
56+
final Class<?> src = request.sourceClass();
57+
if (src == null) return true;
58+
59+
if (request.destClass() != null) return canConvert(src, request.destClass());
60+
if (request.destType() != null) return canConvert(src, request.destType());
61+
62+
return false;
63+
}
64+
65+
@Override
66+
public Object convert(final ConversionRequest request) {
67+
if (request.sourceObject() != null) {
68+
if (request.destClass() != null) return convert(request.sourceObject(),
69+
request.destClass());
70+
71+
if (request.destType() != null) return convert(request.sourceObject(),
72+
request.destType());
73+
}
74+
return null;
75+
}
76+
77+
// -- Typed methods --
78+
79+
@Override
80+
public boolean supports(final ConversionRequest request) {
81+
return canConvert(request);
82+
}
83+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import java.lang.reflect.Type;
35+
36+
/**
37+
* Currency for use in {@link Converter} and {@link ConvertService}
38+
* methods.
39+
* <p>
40+
* {@link #ConversionRequest} provides a variety of constructors. Note that only
41+
* one destination type needs to be set (e.g. either a {@link Type} or a
42+
* {@link Class}).
43+
* </p>
44+
* <p>
45+
* Only the {@link Class} source needs to be set for {@link Converter}
46+
* lookup, such as through
47+
* {@link ConvertService#getHandler(ConversionRequest)}. However, to perform
48+
* an actual conversion, e.g. using
49+
* {@link Converter#convert(ConversionRequest)}, you must provide an
50+
* {@link Object} source.
51+
* </p>
52+
* <p>
53+
* NB: once a {@link Converter} has been acquired, the
54+
* {@code ConversionRequest} used for lookup can be reused to cast to the same
55+
* destination type, simply by updating the source object using the
56+
* {@link #setSourceObject(Object)} method.
57+
* </p>
58+
*
59+
* @author Mark Hiner
60+
*/
61+
public class ConversionRequest {
62+
63+
// -- Fields --
64+
65+
private final Class<?> srcClass;
66+
private Object srcObject;
67+
private Class<?> destClass;
68+
private Type destType;
69+
70+
// -- Constructors --
71+
72+
public ConversionRequest(final Object s, final Class<?> d) {
73+
this(s == null ? null : s.getClass(), d);
74+
srcObject = s;
75+
}
76+
77+
public ConversionRequest(final Class<?> s, final Class<?> d) {
78+
srcClass = s;
79+
destClass = d;
80+
}
81+
82+
public ConversionRequest(final Object s, final Type d) {
83+
this(s == null ? null : s.getClass(), d);
84+
srcObject = s;
85+
}
86+
87+
public ConversionRequest(final Class<?> s, final Type d) {
88+
srcClass = s;
89+
destType = d;
90+
}
91+
92+
// -- Accessors --
93+
94+
/**
95+
* @return Source class for conversion or lookup.
96+
*/
97+
public Class<?> sourceClass() {
98+
return srcClass;
99+
}
100+
101+
/**
102+
* @return Source object for conversion.
103+
*/
104+
public Object sourceObject() {
105+
return srcObject;
106+
}
107+
108+
/**
109+
* @return Destination type for conversion.
110+
*/
111+
public Type destType() {
112+
return destType;
113+
}
114+
115+
/**
116+
* @return Destination class for conversion.
117+
*/
118+
public Class<?> destClass() {
119+
return destClass;
120+
}
121+
122+
// -- Setters --
123+
124+
/**
125+
* Sets the source object for this {@link ConversionRequest}.
126+
*
127+
* @throws IllegalArgumentException If the class of the provided object does
128+
* not match {@link #sourceClass()}.
129+
*/
130+
public void setSourceObject(final Object o) {
131+
if (!srcClass.isAssignableFrom(o.getClass())) {
132+
throw new IllegalArgumentException("Object of type: " + o.getClass() +
133+
" provided. Expected: " + srcClass);
134+
}
135+
136+
srcObject = o;
137+
}
138+
}

0 commit comments

Comments
 (0)