Skip to content

Commit 70ee8ef

Browse files
committed
Migrate convertToEnum method to Types.enumValue
1 parent 2bbf597 commit 70ee8ef

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,6 @@ private ConversionUtils() {
6161
// prevent instantiation of utility class
6262
}
6363

64-
// -- Type casting --
65-
66-
/**
67-
* Converts the given string value to an enumeration constant of the specified
68-
* type.
69-
*
70-
* @param src The value to convert.
71-
* @param dest The type of the enumeration constant.
72-
* @return The converted enumeration constant, or null if the type is not an
73-
* enumeration type or has no such constant.
74-
*/
75-
public static <T> T convertToEnum(final String src, final Class<T> dest) {
76-
if (src == null || !dest.isEnum()) return null;
77-
try {
78-
@SuppressWarnings({ "rawtypes", "unchecked" })
79-
final Enum result = Enum.valueOf((Class) dest, src);
80-
@SuppressWarnings("unchecked")
81-
final T typedResult = (T) result;
82-
return typedResult;
83-
}
84-
catch (final IllegalArgumentException exc) {
85-
// no such enum constant
86-
return null;
87-
}
88-
}
89-
9064
// -- ConvertService setter --
9165

9266
/**
@@ -165,6 +139,17 @@ public static boolean canConvert(final Object src, final Class<?> dest) {
165139
return (handler == null ? false : handler.canConvert(src, dest));
166140
}
167141

142+
/** @deprecated use {@link Types#enumValue} */
143+
@Deprecated
144+
public static <T> T convertToEnum(final String src, final Class<T> dest) {
145+
try {
146+
return Types.enumValue(src, dest);
147+
}
148+
catch (final IllegalArgumentException exc) {
149+
return null;
150+
}
151+
}
152+
168153
/** @deprecated use {@link Types#raw} */
169154
@Deprecated
170155
public static Class<?> getClass(final Type type) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,27 @@ public static <T> T cast(final Object src, final Class<T> dest) {
623623
return result;
624624
}
625625

626+
/**
627+
* Converts the given string value to an enumeration constant of the specified
628+
* type.
629+
*
630+
* @param name The value to convert.
631+
* @param dest The type of the enumeration constant.
632+
* @return The converted enumeration constant.
633+
* @throws IllegalArgumentException if the type is not an enumeration type, or
634+
* has no such constant.
635+
*/
636+
public static <T> T enumValue(final String name, final Class<T> dest) {
637+
if (!dest.isEnum()) {
638+
throw new IllegalArgumentException("Not an enum type: " + name(dest));
639+
}
640+
@SuppressWarnings({ "rawtypes", "unchecked" })
641+
final Enum result = Enum.valueOf((Class) dest, name);
642+
@SuppressWarnings("unchecked")
643+
final T typedResult = (T) result;
644+
return typedResult;
645+
}
646+
626647
/**
627648
* Creates a new {@link ParameterizedType} of the given class together with
628649
* the specified type arguments.

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

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

3535
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertFalse;
3736
import static org.junit.Assert.assertNotNull;
3837
import static org.junit.Assert.assertNull;
39-
import static org.junit.Assert.assertSame;
4038
import static org.junit.Assert.assertTrue;
4139

4240
import java.util.ArrayList;
@@ -57,22 +55,6 @@
5755
*/
5856
public class ConversionUtilsTest {
5957

60-
/** Tests {@link ConversionUtils#convertToEnum(String, Class)}. */
61-
@Test
62-
public void testConvertToEnum() {
63-
final Words foo = ConversionUtils.convertToEnum("FOO", Words.class);
64-
assertSame(Words.FOO, foo);
65-
final Words bar = ConversionUtils.convertToEnum("BAR", Words.class);
66-
assertSame(Words.BAR, bar);
67-
final Words fubar = ConversionUtils.convertToEnum("FUBAR", Words.class);
68-
assertSame(Words.FUBAR, fubar);
69-
final Words noConstant = ConversionUtils.convertToEnum("NONE", Words.class);
70-
assertNull(noConstant);
71-
final String notAnEnum =
72-
ConversionUtils.convertToEnum("HOOYAH", String.class);
73-
assertNull(notAnEnum);
74-
}
75-
7658
/**
7759
* Tests populating a primitive array.
7860
*/
@@ -457,9 +439,4 @@ public static class StringListExtension extends ArrayList<String> {
457439
// NB: No implementation needed.
458440
}
459441

460-
/** Enumeration for testing conversion to enum types. */
461-
public static enum Words {
462-
FOO, BAR, FUBAR
463-
}
464-
465442
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,29 @@ public void testCast() {
462462
assertEquals(5, intToNumber.intValue());
463463
}
464464

465+
/** Tests {@link Types#enumValue(String, Class)}. */
466+
@Test
467+
public void testEnumValue() {
468+
final Words foo = Types.enumValue("FOO", Words.class);
469+
assertSame(Words.FOO, foo);
470+
final Words bar = Types.enumValue("BAR", Words.class);
471+
assertSame(Words.BAR, bar);
472+
final Words fubar = Types.enumValue("FUBAR", Words.class);
473+
assertSame(Words.FUBAR, fubar);
474+
}
475+
476+
/** Tests {@link Types#enumValue(String, Class)} for invalid value. */
477+
@Test(expected = IllegalArgumentException.class)
478+
public void testEnumValueNoConstant() {
479+
Types.enumValue("NONE", Words.class);
480+
}
481+
482+
/** Tests {@link Types#enumValue(String, Class)} for non-enum class. */
483+
@Test(expected = IllegalArgumentException.class)
484+
public void testEnumValueNonEnum() {
485+
Types.enumValue("HOOYAH", String.class);
486+
}
487+
465488
// -- Helper classes --
466489

467490
private static class Thing<T> {
@@ -483,6 +506,11 @@ private static class ComplexThing<T extends Serializable & Cloneable> extends
483506
// NB: No implementation needed.
484507
}
485508

509+
/** Enumeration for testing conversion to enum types. */
510+
public static enum Words {
511+
FOO, BAR, FUBAR
512+
}
513+
486514
// -- Helper methods --
487515

488516
/** Convenience method to get the {@link Type} of a field. */

0 commit comments

Comments
 (0)