33import com .github .binarywang .wxpay .bean .notify .WxPayOrderNotifyCoupon ;
44import com .github .binarywang .wxpay .bean .notify .WxPayOrderNotifyResult ;
55import com .google .common .base .Function ;
6+ import com .google .common .collect .Lists ;
67import com .google .common .collect .Maps ;
78import com .thoughtworks .xstream .annotations .XStreamAlias ;
89import com .thoughtworks .xstream .converters .MarshallingContext ;
2122import java .util .List ;
2223import java .util .Map ;
2324
25+ /**
26+ * @author aimilin
27+ */
2428public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter {
2529
2630 public WxPayOrderNotifyResultConverter (Mapper mapper , ReflectionProvider reflectionProvider ) {
@@ -72,26 +76,26 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
7276 fields .addAll (Arrays .asList (obj .getClass ().getSuperclass ().getDeclaredFields ()));
7377 Map <String , Field > fieldMap = getFieldMap (fields );
7478
75- List < WxPayOrderNotifyCoupon > coupons = new ArrayList <>( 10 );
79+ Map < Integer , WxPayOrderNotifyCoupon > coupons = Maps . newTreeMap ( );
7680 while (reader .hasMoreChildren ()) {
7781 reader .moveDown ();
7882 if (fieldMap .containsKey (reader .getNodeName ())) {
7983 Field field = fieldMap .get (reader .getNodeName ());
80- setFieldValue (context , obj , field );
84+ this . setFieldValue (context , obj , field );
8185 } else if (StringUtils .startsWith (reader .getNodeName (), "coupon_id_" )) {
8286 String id = (String ) context .convertAnother (obj , String .class );
83- getIndex (coupons , reader .getNodeName ()).setCouponId (id );
87+ this . getElement (coupons , reader .getNodeName ()).setCouponId (id );
8488 } else if (StringUtils .startsWith (reader .getNodeName (), "coupon_type_" )) {
8589 String type = (String ) context .convertAnother (obj , String .class );
86- getIndex (coupons , reader .getNodeName ()).setCouponType (type );
90+ this . getElement (coupons , reader .getNodeName ()).setCouponType (type );
8791 } else if (StringUtils .startsWith (reader .getNodeName (), "coupon_fee_" )) {
8892 Integer fee = (Integer ) context .convertAnother (obj , Integer .class );
89- getIndex (coupons , reader .getNodeName ()).setCouponFee (fee );
93+ this . getElement (coupons , reader .getNodeName ()).setCouponFee (fee );
9094 }
9195 reader .moveUp ();
9296 }
9397
94- obj .setCouponList (coupons );
98+ obj .setCouponList (Lists . newArrayList ( coupons . values ()) );
9599 return obj ;
96100 }
97101
@@ -102,12 +106,12 @@ private void setFieldValue(UnmarshallingContext context, WxPayOrderNotifyResult
102106 PropertyDescriptor pd = new PropertyDescriptor (field .getName (), obj .getClass ());
103107 pd .getWriteMethod ().invoke (obj , val );
104108 }
105- } catch (Exception e ) {
109+ } catch (Exception ignored ) {
106110 }
107111 }
108112
109113 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 >() {
111115 @ Override
112116 public String apply (Field field ) {
113117 if (field .isAnnotationPresent (XStreamAlias .class )) {
@@ -116,14 +120,14 @@ public String apply(Field field) {
116120 return field .getName ();
117121 }
118122 });
119- return fieldMap ;
120123 }
121124
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 ());
126129 }
130+
127131 return coupons .get (index );
128132 }
129133}
0 commit comments