Skip to content

Commit 8894402

Browse files
awalter17ctrueden
authored andcommitted
Added getPrimitiveType method
1 parent e7a35c9 commit 8894402

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/main/java/org/scijava/util/ConversionUtils.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,41 @@ public static boolean canCast(final Object src, final Class<?> dest) {
122122
return src == null || canCast(src.getClass(), dest);
123123
}
124124

125+
/**
126+
* Returns the primitive {@link Class} closest to the given type.
127+
* <p>
128+
* Specifically, the following type conversions are done:
129+
* <ul>
130+
* <li>Boolean.class becomes boolean.class</li>
131+
* <li>Byte.class becomes byte.class</li>
132+
* <li>Character.class becomes char.class</li>
133+
* <li>Double.class becomes double.class</li>
134+
* <li>Float.class becomes float.class</li>
135+
* <li>Integer.class becomes int.class</li>
136+
* <li>Long.class becomes long.class</li>
137+
* <li>Short.class becomes short.class</li>
138+
* <li>Void.class becomes void.class</li>
139+
* </ul>
140+
* All other types are unchanged.
141+
* </p>
142+
*/
143+
public static <T> Class<T> getPrimitiveType(final Class<T> type) {
144+
final Class<?> destType;
145+
if (type == Boolean.class) destType = boolean.class;
146+
else if (type == Byte.class) destType = byte.class;
147+
else if (type == Character.class) destType = char.class;
148+
else if (type == Double.class) destType = double.class;
149+
else if (type == Float.class) destType = float.class;
150+
else if (type == Integer.class) destType = int.class;
151+
else if (type == Long.class) destType = long.class;
152+
else if (type == Short.class) destType = short.class;
153+
else if (type == Void.class) destType = void.class;
154+
else destType = type;
155+
@SuppressWarnings("unchecked")
156+
final Class<T> result = (Class<T>) destType;
157+
return result;
158+
}
159+
125160
/**
126161
* Returns the non-primitive {@link Class} closest to the given type.
127162
* <p>

0 commit comments

Comments
 (0)