Skip to content

Commit cddd255

Browse files
committed
Merge pull request #158 from scijava/converters
Converters
2 parents d84ab12 + 839d3ed commit cddd255

38 files changed

+2416
-17
lines changed

src/main/java/org/scijava/convert/AbstractConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ public boolean canConvert(final Object src, final Class<?> dest) {
108108
@Override
109109
public boolean canConvert(final Class<?> src, final Class<?> dest) {
110110
if (src == null) return false;
111-
return ConversionUtils.canCast(src, getInputType()) &&
112-
ConversionUtils.canCast(getOutputType(), dest);
111+
final Class<?> saneSrc = ConversionUtils.getNonprimitiveType(src);
112+
final Class<?> saneDest = ConversionUtils.getNonprimitiveType(dest);
113+
return ConversionUtils.canCast(saneSrc, getInputType()) &&
114+
ConversionUtils.canCast(getOutputType(), saneDest);
113115
}
114116

115117
@Override

src/main/java/org/scijava/convert/CastingConverter.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*
4242
* @author Mark Hiner hinerm at gmail.com
4343
*/
44-
@Plugin(type = Converter.class, priority = Priority.FIRST_PRIORITY - 1)
44+
@Plugin(type = Converter.class, priority = Priority.FIRST_PRIORITY)
4545
public class CastingConverter extends AbstractConverter<Object, Object> {
4646

4747
@SuppressWarnings("deprecation")
@@ -63,17 +63,14 @@ public boolean canConvert(final Class<?> src, final Class<?> dest) {
6363
@Override
6464
public <T> T convert(final Object src, final Class<T> dest) {
6565
// NB: Regardless of whether the destination type is an array or
66-
// collection,
67-
// we still want to cast directly if doing so is possible. But note that
68-
// in
69-
// general, this check does not detect cases of incompatible generic
70-
// parameter types. If this limitation becomes a problem in the future
71-
// we
72-
// can extend the logic here to provide additional signatures of canCast
73-
// which operate on Types in general rather than only Classes. However,
74-
// the
75-
// logic could become complex very quickly in various subclassing cases,
76-
// generic parameters resolved vs. propagated, etc.
66+
// collection, we still want to cast directly if doing so is possible.
67+
// But note that in general, this check does not detect cases of
68+
// incompatible generic parameter types. If this limitation becomes a
69+
// problem in the future we can extend the logic here to provide
70+
// additional signatures of canCast which operate on Types in general
71+
// rather than only Classes. However, the logic could become complex
72+
// very quickly in various subclassing cases, generic parameters
73+
// resolved vs. propagated, etc.
7774
final Class<?> c = GenericUtils.getClass(dest);
7875
return (T) ConversionUtils.cast(src, c);
7976
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 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.math.BigInteger;
35+
36+
import org.scijava.plugin.Plugin;
37+
38+
/**
39+
* Converter plugins that convert from primitive numeric types to other
40+
* primitive numeric types.
41+
*
42+
* @author Alison Walter
43+
*/
44+
public final class NumberConverters {
45+
46+
private NumberConverters() {
47+
// prevent instantiation of container class
48+
}
49+
50+
//convert to short
51+
@Plugin(type = Converter.class)
52+
public static class ByteToShortConverter extends NumberToShortConverter<Byte> {
53+
@Override public Class<Byte> getInputType() { return Byte.class; }
54+
}
55+
56+
//convert to int
57+
@Plugin(type = Converter.class)
58+
public static class ByteToIntegerConverter extends NumberToIntegerConverter<Byte> {
59+
@Override public Class<Byte> getInputType() { return Byte.class; }
60+
}
61+
62+
@Plugin(type = Converter.class)
63+
public static class ShortToIntegerConverter extends NumberToIntegerConverter<Short> {
64+
@Override public Class<Short> getInputType() { return Short.class; }
65+
}
66+
67+
//convert to long
68+
@Plugin(type = Converter.class)
69+
public static class ByteToLongConverter extends NumberToLongConverter<Byte> {
70+
@Override public Class<Byte> getInputType() { return Byte.class; }
71+
}
72+
73+
@Plugin(type = Converter.class)
74+
public static class ShortToLongConverter extends NumberToLongConverter<Short> {
75+
@Override public Class<Short> getInputType() { return Short.class; }
76+
}
77+
78+
@Plugin(type = Converter.class)
79+
public static class IntegerToLongConverter extends NumberToLongConverter<Integer> {
80+
@Override public Class<Integer> getInputType() { return Integer.class; }
81+
}
82+
83+
//convert to float
84+
@Plugin(type = Converter.class)
85+
public static class ByteToFloatConverter extends NumberToFloatConverter<Byte> {
86+
@Override public Class<Byte> getInputType() { return Byte.class; }
87+
}
88+
89+
@Plugin(type = Converter.class)
90+
public static class ShortToFloatConverter extends NumberToFloatConverter<Short> {
91+
@Override public Class<Short> getInputType() { return Short.class; }
92+
}
93+
94+
95+
//convert to double
96+
@Plugin(type = Converter.class)
97+
public static class ByteToDoubleConverter extends NumberToDoubleConverter<Byte> {
98+
@Override public Class<Byte> getInputType() { return Byte.class; }
99+
}
100+
101+
@Plugin(type = Converter.class)
102+
public static class ShortToDoubleConverter extends NumberToDoubleConverter<Short> {
103+
@Override public Class<Short> getInputType() { return Short.class; }
104+
}
105+
106+
@Plugin(type = Converter.class)
107+
public static class IntegerToDoubleConverter extends NumberToDoubleConverter<Integer> {
108+
@Override public Class<Integer> getInputType() { return Integer.class; }
109+
}
110+
111+
@Plugin(type = Converter.class)
112+
public static class FloatToDoubleConverter extends NumberToDoubleConverter<Float> {
113+
@Override public Class<Float> getInputType() { return Float.class; }
114+
}
115+
116+
//convert to BigInteger
117+
@Plugin(type = Converter.class)
118+
public static class ByteToBigIntegerConverter extends NumberToBigIntegerConverter<Byte> {
119+
@Override public Class<Byte> getInputType() { return Byte.class; }
120+
}
121+
122+
@Plugin(type = Converter.class)
123+
public static class ShortToBigIntegerConverter extends NumberToBigIntegerConverter<Short> {
124+
@Override public Class<Short> getInputType() { return Short.class; }
125+
}
126+
127+
@Plugin(type = Converter.class)
128+
public static class IntegerToBigIntegerConverter extends NumberToBigIntegerConverter<Integer> {
129+
@Override public Class<Integer> getInputType() { return Integer.class; }
130+
}
131+
132+
@Plugin(type = Converter.class)
133+
public static class LongToBigIntegerConverter extends NumberToBigIntegerConverter<Long> {
134+
@Override public Class<Long> getInputType() { return Long.class; }
135+
}
136+
137+
//convert to BigDecimal
138+
@Plugin(type = Converter.class)
139+
public static class ByteToBigDecimalConverter extends NumberToBigDecimalConverter<Byte> {
140+
@Override public Class<Byte> getInputType() { return Byte.class; }
141+
}
142+
143+
@Plugin(type = Converter.class)
144+
public static class ShortToBigDecimalConverter extends NumberToBigDecimalConverter<Short> {
145+
@Override public Class<Short> getInputType() { return Short.class; }
146+
}
147+
148+
@Plugin(type = Converter.class)
149+
public static class IntegerToBigDecimalConverter extends NumberToBigDecimalConverter<Integer> {
150+
@Override public Class<Integer> getInputType() { return Integer.class; }
151+
}
152+
153+
@Plugin(type = Converter.class)
154+
public static class LongToBigDecimalConverter extends NumberToBigDecimalConverter<Long> {
155+
@Override public Class<Long> getInputType() { return Long.class; }
156+
}
157+
158+
@Plugin(type = Converter.class)
159+
public static class FloatToBigDecimalConverter extends NumberToBigDecimalConverter<Float> {
160+
@Override public Class<Float> getInputType() { return Float.class; }
161+
}
162+
163+
@Plugin(type = Converter.class)
164+
public static class DoubleToBigDecimalConverter extends NumberToBigDecimalConverter<Double> {
165+
@Override public Class<Double> getInputType() { return Double.class; }
166+
}
167+
168+
@Plugin(type = Converter.class)
169+
public static class BigIntegerToBigDecimalConverter extends NumberToBigDecimalConverter<BigInteger> {
170+
@Override public Class<BigInteger> getInputType() { return BigInteger.class; }
171+
}
172+
173+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 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.math.BigDecimal;
35+
36+
import org.scijava.util.NumberUtils;
37+
38+
/**
39+
* Converts numbers to BigDecimals.
40+
*
41+
* @author Alison Walter
42+
*/
43+
public abstract class NumberToBigDecimalConverter<N extends Number> extends NumberToNumberConverter<N, BigDecimal> {
44+
45+
@Override
46+
public BigDecimal convert(Number n) {
47+
return NumberUtils.asBigDecimal(n);
48+
}
49+
50+
@Override
51+
public Class<BigDecimal> getOutputType() {
52+
return BigDecimal.class;
53+
}
54+
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 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.math.BigInteger;
35+
36+
import org.scijava.util.NumberUtils;
37+
38+
/**
39+
* Converts numbers to BigIntegers.
40+
*
41+
* @author Alison Walter
42+
*/
43+
public abstract class NumberToBigIntegerConverter<N extends Number> extends
44+
NumberToNumberConverter<N, BigInteger>
45+
{
46+
47+
@Override
48+
public BigInteger convert(final Number n) {
49+
return NumberUtils.asBigInteger(n);
50+
}
51+
52+
@Override
53+
public Class<BigInteger> getOutputType() {
54+
return BigInteger.class;
55+
}
56+
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 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+
/**
35+
* Converts numbers to doubles.
36+
*
37+
* @author Alison Walter
38+
*/
39+
public abstract class NumberToDoubleConverter<N extends Number> extends NumberToNumberConverter<N, Double> {
40+
41+
@Override
42+
public Double convert(Number n) {
43+
return n.doubleValue();
44+
}
45+
46+
@Override
47+
public Class<Double> getOutputType() {
48+
return Double.class;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)