3
3
import com .github .binarywang .wxpay .bean .notify .WxPayOrderNotifyCoupon ;
4
4
import com .github .binarywang .wxpay .bean .notify .WxPayOrderNotifyResult ;
5
5
import com .google .common .base .Function ;
6
+ import com .google .common .collect .Lists ;
6
7
import com .google .common .collect .Maps ;
7
8
import com .thoughtworks .xstream .annotations .XStreamAlias ;
8
9
import com .thoughtworks .xstream .converters .MarshallingContext ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
23
24
25
+ /**
26
+ * @author aimilin
27
+ */
24
28
public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter {
25
29
26
30
public WxPayOrderNotifyResultConverter (Mapper mapper , ReflectionProvider reflectionProvider ) {
@@ -72,26 +76,26 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
72
76
fields .addAll (Arrays .asList (obj .getClass ().getSuperclass ().getDeclaredFields ()));
73
77
Map <String , Field > fieldMap = getFieldMap (fields );
74
78
75
- List < WxPayOrderNotifyCoupon > coupons = new ArrayList <>( 10 );
79
+ Map < Integer , WxPayOrderNotifyCoupon > coupons = Maps . newTreeMap ( );
76
80
while (reader .hasMoreChildren ()) {
77
81
reader .moveDown ();
78
82
if (fieldMap .containsKey (reader .getNodeName ())) {
79
83
Field field = fieldMap .get (reader .getNodeName ());
80
- setFieldValue (context , obj , field );
84
+ this . setFieldValue (context , obj , field );
81
85
} else if (StringUtils .startsWith (reader .getNodeName (), "coupon_id_" )) {
82
86
String id = (String ) context .convertAnother (obj , String .class );
83
- getIndex (coupons , reader .getNodeName ()).setCouponId (id );
87
+ this . getElement (coupons , reader .getNodeName ()).setCouponId (id );
84
88
} else if (StringUtils .startsWith (reader .getNodeName (), "coupon_type_" )) {
85
89
String type = (String ) context .convertAnother (obj , String .class );
86
- getIndex (coupons , reader .getNodeName ()).setCouponType (type );
90
+ this . getElement (coupons , reader .getNodeName ()).setCouponType (type );
87
91
} else if (StringUtils .startsWith (reader .getNodeName (), "coupon_fee_" )) {
88
92
Integer fee = (Integer ) context .convertAnother (obj , Integer .class );
89
- getIndex (coupons , reader .getNodeName ()).setCouponFee (fee );
93
+ this . getElement (coupons , reader .getNodeName ()).setCouponFee (fee );
90
94
}
91
95
reader .moveUp ();
92
96
}
93
97
94
- obj .setCouponList (coupons );
98
+ obj .setCouponList (Lists . newArrayList ( coupons . values ()) );
95
99
return obj ;
96
100
}
97
101
@@ -102,12 +106,12 @@ private void setFieldValue(UnmarshallingContext context, WxPayOrderNotifyResult
102
106
PropertyDescriptor pd = new PropertyDescriptor (field .getName (), obj .getClass ());
103
107
pd .getWriteMethod ().invoke (obj , val );
104
108
}
105
- } catch (Exception e ) {
109
+ } catch (Exception ignored ) {
106
110
}
107
111
}
108
112
109
113
private Map <String , Field > getFieldMap (List <Field > fields ) {
110
- Map < String , Field > fieldMap = Maps .uniqueIndex (fields , new Function <Field , String >() {
114
+ return Maps .uniqueIndex (fields , new Function <Field , String >() {
111
115
@ Override
112
116
public String apply (Field field ) {
113
117
if (field .isAnnotationPresent (XStreamAlias .class )) {
@@ -116,14 +120,14 @@ public String apply(Field field) {
116
120
return field .getName ();
117
121
}
118
122
});
119
- return fieldMap ;
120
123
}
121
124
122
- private WxPayOrderNotifyCoupon getIndex ( List < WxPayOrderNotifyCoupon > coupons , String nodeName ) {
123
- Integer index = Integer .valueOf (StringUtils .substring (nodeName , nodeName . lastIndexOf ( "_" ) + 1 ));
124
- if (index >= coupons . size () || coupons .get (index ) == null ) {
125
- coupons .add (index , new WxPayOrderNotifyCoupon ());
125
+ private WxPayOrderNotifyCoupon getElement ( Map < Integer , WxPayOrderNotifyCoupon > coupons , String nodeName ) {
126
+ Integer index = Integer .valueOf (StringUtils .substringAfterLast (nodeName , "_" ));
127
+ if (coupons .get (index ) == null ) {
128
+ coupons .put (index , new WxPayOrderNotifyCoupon ());
126
129
}
130
+
127
131
return coupons .get (index );
128
132
}
129
133
}
0 commit comments