Skip to content

Commit 4e74d2b

Browse files
committed
Migrate ConversionUtils.getNullValue to Types
1 parent 3f029c9 commit 4e74d2b

File tree

4 files changed

+90
-75
lines changed

4 files changed

+90
-75
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,6 @@ public static <T> Class<T> getNonprimitiveType(final Class<T> type) {
218218
return result;
219219
}
220220

221-
/**
222-
* Gets the "null" value for the given type. For non-primitives, this will
223-
* actually be null. For primitives, it will be zero for numeric types, false
224-
* for boolean, and the null character for char.
225-
*/
226-
public static <T> T getNullValue(final Class<T> type) {
227-
final Object defaultValue;
228-
if (type == boolean.class) defaultValue = false;
229-
else if (type == byte.class) defaultValue = (byte) 0;
230-
else if (type == char.class) defaultValue = '\0';
231-
else if (type == double.class) defaultValue = 0d;
232-
else if (type == float.class) defaultValue = 0f;
233-
else if (type == int.class) defaultValue = 0;
234-
else if (type == long.class) defaultValue = 0L;
235-
else if (type == short.class) defaultValue = (short) 0;
236-
else defaultValue = null;
237-
@SuppressWarnings("unchecked")
238-
final T result = (T) defaultValue;
239-
return result;
240-
}
241-
242221
// -- ConvertService setter --
243222

244223
/**
@@ -329,6 +308,12 @@ public static Class<?> getComponentClass(final Type type) {
329308
return Types.raw(Types.component(type));
330309
}
331310

311+
/** @deprecated Use {@link Types#nullValue} instead. */
312+
@Deprecated
313+
public static <T> T getNullValue(final Class<T> type) {
314+
return Types.nullValue(type);
315+
}
316+
332317
//-- Helper methods --
333318

334319
/**

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,27 @@ public static boolean isText(final Class<?> type) {
307307
return String.class.isAssignableFrom(type) || isCharacter(type);
308308
}
309309

310+
/**
311+
* Gets the "null" value for the given type. For non-primitives, this will
312+
* actually be null. For primitives, it will be zero for numeric types, false
313+
* for boolean, and the null character for char.
314+
*/
315+
public static <T> T nullValue(final Class<T> type) {
316+
final Object defaultValue;
317+
if (type == boolean.class) defaultValue = false;
318+
else if (type == byte.class) defaultValue = (byte) 0;
319+
else if (type == char.class) defaultValue = '\0';
320+
else if (type == double.class) defaultValue = 0d;
321+
else if (type == float.class) defaultValue = 0f;
322+
else if (type == int.class) defaultValue = 0;
323+
else if (type == long.class) defaultValue = 0L;
324+
else if (type == short.class) defaultValue = (short) 0;
325+
else defaultValue = null;
326+
@SuppressWarnings("unchecked")
327+
final T result = (T) defaultValue;
328+
return result;
329+
}
330+
310331
public static Field field(final Class<?> c, final String name) {
311332
if (c == null) throw new IllegalArgumentException("No such field: " + name);
312333
try {

src/test/java/org/scijava/util/ConversionUtilsTest.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -198,60 +198,6 @@ public void testGetNonprimitiveType() {
198198
}
199199
}
200200

201-
/** Tests {@link ConversionUtils#getNullValue(Class)}. */
202-
@Test
203-
public void testGetNullValue() {
204-
final boolean booleanNull = ConversionUtils.getNullValue(boolean.class);
205-
assertFalse(booleanNull);
206-
207-
final byte byteNull = ConversionUtils.getNullValue(byte.class);
208-
assertEquals(0, byteNull);
209-
210-
final char charNull = ConversionUtils.getNullValue(char.class);
211-
assertEquals('\0', charNull);
212-
213-
final double doubleNull = ConversionUtils.getNullValue(double.class);
214-
assertEquals(0.0, doubleNull, 0.0);
215-
216-
final float floatNull = ConversionUtils.getNullValue(float.class);
217-
assertEquals(0f, floatNull, 0f);
218-
219-
final int intNull = ConversionUtils.getNullValue(int.class);
220-
assertEquals(0, intNull);
221-
222-
final long longNull = ConversionUtils.getNullValue(long.class);
223-
assertEquals(0, longNull);
224-
225-
final short shortNull = ConversionUtils.getNullValue(short.class);
226-
assertEquals(0, shortNull);
227-
228-
final Void voidNull = ConversionUtils.getNullValue(void.class);
229-
assertNull(voidNull);
230-
231-
final Class<?>[] types = { //
232-
Boolean.class, Byte.class, Character.class, Double.class, //
233-
Float.class, Integer.class, Long.class, Short.class, //
234-
Void.class, //
235-
String.class, //
236-
Number.class, BigInteger.class, BigDecimal.class, //
237-
boolean[].class, byte[].class, char[].class, double[].class, //
238-
float[].class, int[].class, long[].class, short[].class, //
239-
Boolean[].class, Byte[].class, Character[].class, Double[].class, //
240-
Float[].class, Integer[].class, Long[].class, Short[].class, //
241-
Void[].class, //
242-
Object.class, Object[].class, String[].class, //
243-
Object[][].class, String[][].class, //
244-
Collection.class, //
245-
List.class, ArrayList.class, LinkedList.class, //
246-
Set.class, HashSet.class, //
247-
Map.class, HashMap.class, //
248-
Collection[].class, List[].class, Set[].class, Map[].class };
249-
for (final Class<?> c : types) {
250-
final Object nullValue = ConversionUtils.getNullValue(c);
251-
assertNull("Expected null for " + c.getName(), nullValue);
252-
}
253-
}
254-
255201
/**
256202
* Tests populating a primitive array.
257203
*/

src/test/java/org/scijava/util/TypesTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package org.scijava.util;
3434

3535
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.assertFalse;
3637
import static org.junit.Assert.assertNull;
3738
import static org.junit.Assert.assertSame;
3839
import static org.junit.Assert.assertTrue;
@@ -42,8 +43,16 @@
4243
import java.lang.reflect.Field;
4344
import java.lang.reflect.Type;
4445
import java.lang.reflect.TypeVariable;
46+
import java.math.BigDecimal;
47+
import java.math.BigInteger;
48+
import java.util.ArrayList;
49+
import java.util.Collection;
4550
import java.util.HashMap;
51+
import java.util.HashSet;
52+
import java.util.LinkedList;
4653
import java.util.List;
54+
import java.util.Map;
55+
import java.util.Set;
4756

4857
import org.junit.Test;
4958

@@ -182,6 +191,60 @@ public void testRaws() {
182191
Serializable.class, Cloneable.class);
183192
}
184193

194+
/** Tests {@link Types#nullValue(Class)}. */
195+
@Test
196+
public void testNullValue() {
197+
final boolean booleanNull = Types.nullValue(boolean.class);
198+
assertFalse(booleanNull);
199+
200+
final byte byteNull = Types.nullValue(byte.class);
201+
assertEquals(0, byteNull);
202+
203+
final char charNull = Types.nullValue(char.class);
204+
assertEquals('\0', charNull);
205+
206+
final double doubleNull = Types.nullValue(double.class);
207+
assertEquals(0.0, doubleNull, 0.0);
208+
209+
final float floatNull = Types.nullValue(float.class);
210+
assertEquals(0f, floatNull, 0f);
211+
212+
final int intNull = Types.nullValue(int.class);
213+
assertEquals(0, intNull);
214+
215+
final long longNull = Types.nullValue(long.class);
216+
assertEquals(0, longNull);
217+
218+
final short shortNull = Types.nullValue(short.class);
219+
assertEquals(0, shortNull);
220+
221+
final Void voidNull = Types.nullValue(void.class);
222+
assertNull(voidNull);
223+
224+
final Class<?>[] types = { //
225+
Boolean.class, Byte.class, Character.class, Double.class, //
226+
Float.class, Integer.class, Long.class, Short.class, //
227+
Void.class, //
228+
String.class, //
229+
Number.class, BigInteger.class, BigDecimal.class, //
230+
boolean[].class, byte[].class, char[].class, double[].class, //
231+
float[].class, int[].class, long[].class, short[].class, //
232+
Boolean[].class, Byte[].class, Character[].class, Double[].class, //
233+
Float[].class, Integer[].class, Long[].class, Short[].class, //
234+
Void[].class, //
235+
Object.class, Object[].class, String[].class, //
236+
Object[][].class, String[][].class, //
237+
Collection.class, //
238+
List.class, ArrayList.class, LinkedList.class, //
239+
Set.class, HashSet.class, //
240+
Map.class, HashMap.class, //
241+
Collection[].class, List[].class, Set[].class, Map[].class };
242+
for (final Class<?> c : types) {
243+
final Object nullValue = Types.nullValue(c);
244+
assertNull("Expected null for " + c.getName(), nullValue);
245+
}
246+
}
247+
185248
/** Tests {@link Types#field}. */
186249
@Test
187250
public void testField() {

0 commit comments

Comments
 (0)