11package org .hswebframework .ezorm .rdb .codec ;
22
33import lombok .Getter ;
4+ import org .hswebframework .ezorm .core .GlobalConfig ;
45import org .hswebframework .ezorm .core .ValueCodec ;
6+ import org .hswebframework .ezorm .rdb .mapping .annotation .EnumCodec ;
7+ import org .hswebframework .ezorm .rdb .utils .PropertyUtils ;
58
69import java .lang .reflect .Array ;
710import java .util .Arrays ;
811import java .util .List ;
12+ import java .util .Objects ;
913import java .util .function .Function ;
1014import java .util .stream .Collector ;
1115import java .util .stream .Collectors ;
1216import java .util .stream .Stream ;
1317
1418public class EnumValueCodec implements ValueCodec <Object , Object > {
1519
20+ @ SuppressWarnings ("rawtypes" )
1621 private static final Collector collector = Collectors .joining ("," );
1722
1823 private static final Function <String , String []> splitter = str -> str .split ("[,]" );
1924
25+ static final String PROPERTY_NAME = EnumCodec .NAME ,
26+ PROPERTY_ORDINAL = EnumCodec .ORDINAL ;
27+
2028 @ SuppressWarnings ("all" )
2129 private final Class type ;
2230 @ SuppressWarnings ("all" )
@@ -28,7 +36,14 @@ public class EnumValueCodec implements ValueCodec<Object, Object> {
2836 @ Getter
2937 private boolean toMask ;
3038
39+ private final String property ;
40+
3141 public EnumValueCodec (Class <?> type ) {
42+ this (type , PROPERTY_NAME );
43+ }
44+
45+ public EnumValueCodec (Class <?> type , String property ) {
46+ this .property = property ;
3247 if (type .isArray ()) {
3348 this .type = type .getComponentType ();
3449 this .isArray = true ;
@@ -41,11 +56,31 @@ public EnumValueCodec(Class<?> type) {
4156 }
4257 }
4358
59+ public EnumValueCodec (Class <?> type , String property , boolean toMask ) {
60+ this (type , property );
61+ this .toMask = toMask ;
62+ }
63+
4464 public EnumValueCodec (Class <?> type , boolean toMask ) {
4565 this (type );
4666 this .toMask = toMask ;
4767 }
4868
69+ private Object getValue (Enum <?> e ) {
70+ switch (property ) {
71+ case PROPERTY_NAME :
72+ return e .name ();
73+ case PROPERTY_ORDINAL :
74+ return e .ordinal ();
75+ default :
76+ return GlobalConfig
77+ .getPropertyOperator ()
78+ .getProperty (e , property )
79+ .orElseThrow (() -> new IllegalArgumentException ("no property [" + property + "] found in enum " + e .getDeclaringClass ()));
80+ }
81+
82+ }
83+
4984 @ Override
5085 @ SuppressWarnings ("all" )
5186 public Object encode (Object value ) {
@@ -57,17 +92,18 @@ public Object encode(Object value) {
5792
5893 if (value instanceof Enum ) {
5994 if (!toMask ) {
60- return (( Enum ) value ). name ( );
95+ return getValue ((( Enum ) value ));
6196 } else {
6297 return enumToMask (((Enum ) value ));
6398 }
6499 }
65100
66101 if (value instanceof Enum []) {
67102 if (!toMask ) {
68- return Stream .of (((Enum []) value ))
69- .map (Enum ::name )
70- .collect (collector );
103+ return Stream
104+ .of (((Enum []) value ))
105+ .map (this ::getValue )
106+ .collect (collector );
71107 } else {
72108 return enumToMask (((Enum []) value ));
73109 }
@@ -77,53 +113,76 @@ public Object encode(Object value) {
77113 }
78114
79115 @ Override
116+ @ SuppressWarnings ("rawtypes" )
80117 public Object decode (Object data ) {
118+ // 字符串
81119 if (data instanceof String ) {
82120 if (!isArray ) {
83121 for (Object value : values ) {
84- if (((Enum <?>) value ).name ().equalsIgnoreCase (String .valueOf (data ))) {
122+ Enum <?> e = ((Enum <?>) value );
123+ if (eq (e , data )) {
85124 return value ;
86125 }
87126 }
88127 return null ;
89128 } else {
90129 List <String > arr = Arrays .asList (splitter .apply (((String ) data )));
91130 return Stream
92- .of (type . getEnumConstants () )
93- .map (Enum .class ::cast )
94- .filter (e -> arr .contains (e . name ( )))
95- .toArray (l -> (Enum []) Array .newInstance (type , l ));
131+ .of (values )
132+ .map (Enum .class ::cast )
133+ .filter (e -> arr .contains (String . valueOf ( getValue ( e ) )))
134+ .toArray (l -> (Enum []) Array .newInstance (type , l ));
96135 }
97136 }
98- if (data instanceof Number ) {
137+ // 数字类型 toMask
138+ if (data instanceof Number && toMask ) {
99139 long val = ((Number ) data ).longValue ();
100-
101140 Stream <Enum > stream = Stream
102- .of (type . getEnumConstants () )
103- .map (Enum .class ::cast )
104- .filter (e -> toMask ? enumInMask (val , e ) : e . ordinal () == val );
141+ .of (values )
142+ .map (Enum .class ::cast )
143+ .filter (e -> enumInMask (val , e ));
105144
106145 if (isArray ) {
107- return stream .toArray (l -> (Enum []) Array .newInstance (type , l ));
146+ return stream .toArray (l -> (Enum <?> []) Array .newInstance (type , l ));
108147 } else {
109148 return stream .findFirst ().orElse (null );
110149 }
150+ }
111151
152+ Stream <Enum > stream = Stream
153+ .of (values )
154+ .map (Enum .class ::cast )
155+ .filter (e -> eq (e , data ));
156+
157+ if (isArray ) {
158+ return stream .toArray (l -> (Enum <?>[]) Array .newInstance (type , l ));
159+ } else {
160+ return stream .findFirst ().orElse (null );
112161 }
113162
114- return data ;
115163 }
116164
117- private boolean enumInMask (long mask , Enum e ) {
165+ protected boolean eq (Enum <?> e , Object value ) {
166+ Object val = getValue (e );
167+ // 忽略大小写对比字符串
168+ if (val instanceof String ) {
169+ return ((String ) val ).equalsIgnoreCase (String .valueOf (value ));
170+ }
171+ return GlobalConfig
172+ .getPropertyOperator ()
173+ .compare (getValue (e ), value ) == 0 ;
174+ }
175+
176+ private boolean enumInMask (long mask , Enum <?> e ) {
118177 return (mask & (1L << e .ordinal ())) != 0 ;
119178 }
120179
121- private long enumToMask (Enum ... enums ) {
180+ private long enumToMask (Enum <?> ... enums ) {
122181 if (enums == null ) {
123182 return 0L ;
124183 }
125184 long value = 0L ;
126- for (Enum e : enums ) {
185+ for (Enum <?> e : enums ) {
127186 value |= (1L << e .ordinal ());
128187 }
129188 return value ;
0 commit comments