Add EnumUtils.getEnumMap(Class, Function). #730.
Provides a different implementation than #730.
This commit is contained in:
parent
70abafc95d
commit
8a4d1b82f8
|
@ -101,6 +101,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.toStream(Collection).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(Collection) and deprecate misnamed stream(Collection).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(Stream) and deprecate misnamed stream(Stream).</action>
|
||||
<action type="add" dev="ggregory" due-to="Maxwell Cody, Gary Gregory">Add EnumUtils.getEnumMap(Class, Function). #730</action>
|
||||
<!-- UPDATE -->
|
||||
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.4.2.2 #735, #808, #822.</action>
|
||||
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764.</action>
|
||||
|
|
|
@ -20,10 +20,11 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* <p>Utility library to provide helper methods for Java enums.</p>
|
||||
|
@ -281,11 +282,27 @@ public class EnumUtils {
|
|||
* @return the modifiable map of enum names to enums, never null
|
||||
*/
|
||||
public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
|
||||
final Map<String, E> map = new LinkedHashMap<>();
|
||||
for (final E e: enumClass.getEnumConstants()) {
|
||||
map.put(e.name(), e);
|
||||
}
|
||||
return map;
|
||||
return getEnumMap(enumClass, E::name);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Gets the {@code Map} of enums by name.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This method is useful when you need a map of enums by name.
|
||||
* </p>
|
||||
*
|
||||
* @param <E> the type of enumeration
|
||||
* @param <K> the type of the map key
|
||||
* @param enumClass the class of the enum to query, not null
|
||||
* @param keyFunction the function to query for the key, not null
|
||||
* @return the modifiable map of enums, never null
|
||||
* @since 3.13.0
|
||||
*/
|
||||
public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumClass, final Function<E, K> keyFunction) {
|
||||
return Stream.of(enumClass.getEnumConstants()).collect(Collectors.toMap(keyFunction::apply, Function.identity()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -342,6 +342,40 @@ public class EnumUtilsTest {
|
|||
assertFalse(test.containsKey("PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumMap_keyFunction() {
|
||||
final Map<Integer, Month> test = EnumUtils.getEnumMap(Month.class, Month::getId);
|
||||
assertEquals("{1=JAN, 2=FEB, 3=MAR, 4=APR, 5=MAY, 6=JUN, 7=JUL, 8=AUG, 9=SEP, 10=OCT, 11=NOV, 12=DEC}", test.toString(),
|
||||
"getEnumMap not created correctly");
|
||||
assertEquals(12, test.size());
|
||||
assertFalse(test.containsKey(0));
|
||||
assertTrue(test.containsKey(1));
|
||||
assertEquals(Month.JAN, test.get(1));
|
||||
assertTrue(test.containsKey(2));
|
||||
assertEquals(Month.FEB, test.get(2));
|
||||
assertTrue(test.containsKey(3));
|
||||
assertEquals(Month.MAR, test.get(3));
|
||||
assertTrue(test.containsKey(4));
|
||||
assertEquals(Month.APR, test.get(4));
|
||||
assertTrue(test.containsKey(5));
|
||||
assertEquals(Month.MAY, test.get(5));
|
||||
assertTrue(test.containsKey(6));
|
||||
assertEquals(Month.JUN, test.get(6));
|
||||
assertTrue(test.containsKey(7));
|
||||
assertEquals(Month.JUL, test.get(7));
|
||||
assertTrue(test.containsKey(8));
|
||||
assertEquals(Month.AUG, test.get(8));
|
||||
assertTrue(test.containsKey(9));
|
||||
assertEquals(Month.SEP, test.get(9));
|
||||
assertTrue(test.containsKey(10));
|
||||
assertEquals(Month.OCT, test.get(10));
|
||||
assertTrue(test.containsKey(11));
|
||||
assertEquals(Month.NOV, test.get(11));
|
||||
assertTrue(test.containsKey(12));
|
||||
assertEquals(Month.DEC, test.get(12));
|
||||
assertFalse(test.containsKey(13));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumSystemProperty() {
|
||||
final String key = getClass().getName();
|
||||
|
@ -531,6 +565,20 @@ public class EnumUtilsTest {
|
|||
|
||||
}
|
||||
|
||||
enum Month {
|
||||
JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6), JUL(7), AUG(8), SEP(9), OCT(10), NOV(11), DEC(12);
|
||||
|
||||
private final int id;
|
||||
|
||||
Month(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
}
|
||||
|
||||
enum TooMany {
|
||||
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, A1, B1, C1, D1, E1, F1, G1, H1, I1,
|
||||
J1, K1, L1, M1, N1, O1, P1, Q1, R1, S1, T1, U1, V1, W1, X1, Y1, Z1, A2, B2, C2, D2, E2, F2, G2, H2, I2, J2, K2, L2,
|
||||
|
|
Loading…
Reference in New Issue