1818
1919import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
2020import static java .util .Arrays .asList ;
21+ import static java .util .Optional .empty ;
22+ import static java .util .Optional .ofNullable ;
2123
24+ import java .lang .reflect .InvocationTargetException ;
2225import java .lang .reflect .Method ;
2326import java .lang .reflect .Modifier ;
24-
2527import java .util .Collections ;
2628import java .util .HashSet ;
2729import java .util .Optional ;
2830import java .util .Set ;
2931import java .util .stream .Stream ;
3032
33+ import io .appulse .epmd .java .core .mapper .deserializer .exception .DeserializationException ;
34+ import io .appulse .epmd .java .core .mapper .deserializer .exception .EnumUnknownValueException ;
3135import io .appulse .utils .Bytes ;
3236
33- import lombok .val ;
34-
3537/**
3638 *
3739 * @author Artem Labazin
@@ -57,44 +59,62 @@ class EnumDeserializer implements Deserializer {
5759
5860 @ Override
5961 @ SuppressWarnings ("unchecked" )
60- public <T > T deserialize (Bytes bytes , Class <T > type ) throws Exception {
61- val string = bytes .getString (ISO_8859_1 );
62-
63- val constants = type .getEnumConstants ();
64- Optional <Object > constant = Stream .of (constants )
65- .map (it -> (Enum <?>) it )
66- .filter (it -> it .name ().equals (string ))
67- .findAny ()
68- .map (it -> (Object ) it );
62+ public <T > T deserialize (Bytes bytes , Class <T > type ) throws DeserializationException {
63+ String string = bytes .getString (ISO_8859_1 );
64+ T [] constants = type .getEnumConstants ();
6965
66+ Optional <Object > constant = findConstant (constants , string );
7067 if (constant .isPresent ()) {
7168 return (T ) constant .get ();
7269 }
7370
74- Optional <Method > optional = Stream .of (type .getDeclaredMethods ())
75- .filter (it -> Modifier .isStatic (it .getModifiers ()))
76- .filter (it -> Modifier .isPublic (it .getModifiers ()))
77- .filter (it -> it .getParameterCount () == 1 )
78- .filter (it -> it .getParameterTypes ()[0 ] == String .class )
79- .filter (it -> it .getReturnType () == type )
80- .filter (it -> ENUM_CREATE_METHODS_NAMES .contains (it .getName ()))
81- .findAny ();
82-
83- if (optional .isPresent ()) {
84- Method method = optional .get ();
85- return (T ) method .invoke (null , string );
71+ Optional <Object > methodResult = getByMethod (type , string );
72+ if (methodResult .isPresent ()) {
73+ return (T ) methodResult .get ();
8674 }
8775
8876 Optional <T > undefined = Stream .of (constants )
8977 .filter (it -> ENUM_UNKNOWN_VALUE .contains (it .toString ()))
9078 .findAny ();
9179
9280 return undefined
93- .orElseThrow (() -> new RuntimeException ( "Unknown enum value " + string ));
81+ .orElseThrow (() -> new EnumUnknownValueException ( string ));
9482 }
9583
9684 @ Override
9785 public boolean isApplicable (Class <?> type ) {
9886 return type .isEnum ();
9987 }
88+
89+ private Optional <Object > findConstant (Object [] constants , String string ) {
90+ return Stream .of (constants )
91+ .map (it -> (Enum <?>) it )
92+ .filter (it -> it .name ().equals (string ))
93+ .findAny ()
94+ .map (it -> (Object ) it );
95+ }
96+
97+ private Optional <Object > getByMethod (Class <?> type , String string ) {
98+ Optional <Method > optional = Stream .of (type .getDeclaredMethods ())
99+ .filter (it -> Modifier .isStatic (it .getModifiers ()))
100+ .filter (it -> Modifier .isPublic (it .getModifiers ()))
101+ .filter (it -> it .getParameterCount () == 1 )
102+ .filter (it -> it .getParameterTypes ()[0 ] == String .class )
103+ .filter (it -> it .getReturnType () == type )
104+ .filter (it -> ENUM_CREATE_METHODS_NAMES .contains (it .getName ()))
105+ .findAny ();
106+
107+ if (!optional .isPresent ()) {
108+ return empty ();
109+ }
110+
111+ Method method = optional .get ();
112+ Object result = null ;
113+ try {
114+ result = method .invoke (null , string );
115+ } catch (IllegalAccessException | InvocationTargetException ex ) {
116+ throw new DeserializationException (ex );
117+ }
118+ return ofNullable (result );
119+ }
100120}
0 commit comments