shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package com.walker.common.util;
 
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.walker.infrastructure.ApplicationRuntimeException;
import com.walker.infrastructure.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.NotReadablePropertyException;
 
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * 转换工具类
 *
 * @author 时克英
 */
public class ConvertUtil {
    private static final Logger logger = LoggerFactory.getLogger(ConvertUtil.class);
 
    /**
     * 根据对象属性的类型转换value为目标对象
     *
     * @param value 输入数据
     * @param clazz 类型
     * @return 转换后的对象
     */
    public static <T> Object convert(String value, Class<T> clazz) {
        try {
            String typeName = clazz.getCanonicalName();
            if (typeName.equals("java.lang.String")) {
                return value;
            }
            if (value == null || value.equals("") || value.equals("null") || value.equals("undefined")) {
                return null;
            } else if (typeName.equals("boolean") || typeName.equals("java.lang.Boolean")) {
                return Boolean.parseBoolean(value);
            } else if (typeName.equals("int") || typeName.equals("java.lang.Integer")) {
                return Integer.parseInt(value);
            } else if (typeName.equals("short") || typeName.equals("java.lang.Short")) {
                return Short.parseShort(value);
            } else if (typeName.equals("long") || typeName.equals("java.lang.Long")) {
                return Long.parseLong(value);
            } else if (typeName.equals("double") || typeName.equals("java.lang.Double")) {
                return Double.parseDouble(value);
            } else if (typeName.equals("float") || typeName.equals("java.lang.Float")) {
                return Float.parseFloat(value);
            } else if (typeName.equals("java.math.BigInteger")) {
                return new java.math.BigInteger(value);
            } else if (typeName.equals("java.math.BigDecimal")) {
                return new BigDecimal(value);
            } else if (typeName.equals("java.sql.Date")) {
                return java.sql.Date.valueOf(value.substring(0, 10));    //yyyy-mm-dd
            } else if (typeName.equals("java.sql.Time")) {
                return java.sql.Time.valueOf(value);
            } else if (typeName.equals("java.sql.Timestamp")) {
                if (value.trim().length() == 10) {
                    return java.sql.Timestamp.valueOf(value.trim() + " 00:00:00");
                }
                return java.sql.Timestamp.valueOf(value);
            }
            return null;
        } catch (Exception e) {
            logger.error("convert exception : value -> {}, type -> {}", new Object[]{value, clazz});
            logger.error(e.getMessage(), e);
            return null;
        }
    }
 
    /**
     * 根据对象属性的类型转换value为目标对象
     *
     * @param value 输入数据
     * @param clazz 类型
     * @return 转换后的对象
     */
    public static <T> Object convert(Object value, Class<T> clazz) {
        return convert(createString(value), clazz);
    }
 
    /**
     * 将map的key转成小写
     *
     * @param sourceMap
     * @return
     */
    public static Map<String, Object> convertKeyToLowerCase(Map<String, Object> sourceMap) {
        if (sourceMap == null) {
            return null;
        } else {
            try {
                Map mapDest = sourceMap.getClass().newInstance();
                for (Map.Entry<String, Object> entry : sourceMap.entrySet()) {
                    mapDest.put(entry.getKey().toLowerCase(), entry.getValue());
                }
                return mapDest;
            } catch (Exception e) {
                throw new ApplicationRuntimeException(e.getMessage(), e);
            }
        }
    }
 
    /**
     * obj转换为 int, 默认值为Integer.MIN_VALUE
     *
     * @param obj 待转换对象
     * @return int
     */
    public static int toInt(Object obj) {
        if (obj == null) {
            return Integer.MIN_VALUE;
        } else {
            return createInteger(obj);
        }
    }
 
    /**
     * obj转换为 int, 默认值为defaultValue
     *
     * @param obj 待转换对象
     * @return int
     */
    public static int toInt(Object obj, int defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            return createInteger(obj);
        }
    }
 
    /**
     * obj转换为 long, 默认值为Long.MIN_VALUE
     *
     * @param obj 待转换对象
     * @return long
     */
    public static long toLong(Object obj) {
        if (obj == null) {
            return Long.MIN_VALUE;
        } else {
            return createLong(obj);
        }
    }
 
    /**
     * obj转换为 long, 默认值为defaultValue
     *
     * @param obj 待转换对象
     * @return long
     */
    public static long toLong(Object obj, long defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            return createLong(obj);
        }
    }
 
    /**
     * obj转换为 double, 默认值为Double.MIN_VALUE
     *
     * @param obj 待转换对象
     * @return double
     */
    public static double toDouble(Object obj) {
        if (obj == null) {
            return Double.MIN_VALUE;
        } else {
            return createDouble(obj);
        }
    }
 
    /**
     * obj转换为 double, 默认值为defaultValue
     *
     * @param obj 待转换对象
     * @return double
     */
    public static double toDouble(Object obj, double defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            return createDouble(obj);
        }
    }
 
    /**
     * obj转换为 boolean, 默认值为false
     *
     * @param obj 待转换对象
     * @return boolean
     */
    public static boolean toBoolean(Object obj) {
        return obj != null && createBoolean(obj);
    }
 
    /**
     * obj转换为 boolean, 默认值为defaultValue
     *
     * @param obj 待转换对象
     * @return boolean
     */
    public static boolean toBoolean(Object obj, boolean defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            return createBoolean(obj);
        }
    }
 
    /**
     * 将obj转换为String
     *
     * @param obj 待转换对象
     * @return string
     */
    public static String createString(Object obj) {
        if (obj == null) {
            return null;
        } else {
            if (obj instanceof String) {
                return (String) obj;
            } else {
                return obj.toString();
            }
        }
    }
 
    /**
     * 将obj转换为Integer
     *
     * @param obj 待转换对象
     * @return Integer
     */
    public static Integer createInteger(Object obj) {
        if (obj == null) {
            return null;
        } else {
            if (obj instanceof Integer) {
                return (Integer) obj;
            } else {
                return Integer.valueOf(createString(obj));
            }
        }
    }
 
    /**
     * 将obj转换为Long
     *
     * @param obj 待转换对象
     * @return Long
     */
    public static Long createLong(Object obj) {
        if (obj == null) {
            return null;
        } else {
            if (obj instanceof Long) {
                return (Long) obj;
            } else {
                return Long.valueOf(createString(obj));
            }
        }
    }
 
    /**
     * 将obj转换为Double
     *
     * @param obj 待转换对象
     * @return Double
     */
    public static Double createDouble(Object obj) {
        if (obj == null) {
            return null;
        } else {
            if (obj instanceof Double) {
                return (Double) obj;
            } else {
                return Double.valueOf(createString(obj));
            }
        }
    }
 
    /**
     * 将obj转换为BigDecimal
     *
     * @param obj 待转换对象
     * @return BigDecimal
     */
    public static BigDecimal createBigDecimal(Object obj) {
        if (obj == null) {
            return null;
        } else {
            if (obj instanceof BigDecimal) {
                return (BigDecimal) obj;
            } else {
                return new BigDecimal(createString(obj));
            }
        }
    }
 
    /**
     * 将obj转换为Boolean
     *
     * @param obj 待转换对象
     * @return Boolean
     */
    public static Boolean createBoolean(Object obj) {
        if (obj == null) {
            return null;
        } else {
            if (obj instanceof Boolean) {
                return (Boolean) obj;
            } else {
                return Boolean.parseBoolean(createString(obj));
            }
        }
    }
 
    /**
     * 将源对象转化为targetClass类型的目标对象
     *
     * @param source      源对象
     * @param targetClass 目标对象
     * @param <T>         返回对象泛型
     * @return 转化后的对象
     */
    public static <T> T toObject(Object source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        try {
            String typeName = targetClass.getCanonicalName();
            if (typeName.equals("java.util.Map") || typeName.equals("java.util.HashMap")) {
                Map t = new HashMap();
                copyProperties(source, t);
                return (T) t;
            } else if (typeName.equals("java.util.LinkedHashMap")) {
                Map t = new LinkedHashMap();
                copyProperties(source, t);
                return (T) t;
            } else {
                T t = targetClass.newInstance();
                copyProperties(source, t);
                return t;
            }
        } catch (Exception e) {
            throw new ApplicationRuntimeException(e);
        }
    }
 
    /**
     * 将源对象的属性拷贝到目标对象
     *
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyProperties(Object source, Object target) {
        if (source == null || target == null) {
            return;
        }
        if (source instanceof Map) {
            PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(target.getClass());
            Map map = (Map) source;
            try {
                for (PropertyDescriptor descriptor : propertyDescriptors) {
                    if (map.containsKey(descriptor.getName())) {
                        Method writeMethod = descriptor.getWriteMethod();
                        Object value = map.get(descriptor.getName());
                        writeMethod.invoke(target, convert(value, writeMethod.getParameterTypes()[0]));
                    }
                }
            } catch (Exception e) {
                throw new ApplicationRuntimeException(e);
            }
        } else if (target instanceof Map) {
            try {
                PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(source.getClass());
                Map<String, Object> map = (Map) target;
                for (PropertyDescriptor descriptor : propertyDescriptors) {
                    Method readMethod = descriptor.getReadMethod();
                    map.put(descriptor.getName(), readMethod.invoke(source, new Object[]{}));
                }
            } catch (Exception e) {
                throw new ApplicationRuntimeException(e);
            }
        } else {
            BeanUtils.copyProperties(source, target);
        }
    }
 
 
    /**
     * 将源对象的属性拷贝到目标对象,忽略为空的属性
     *
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyPropertiesIgnoreNull(Object source, Object target) {
        if (source == null || target == null) {
            return;
        }
        if (source instanceof Map) {
            PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(target.getClass());
            Map map = (Map) source;
            try {
                for (PropertyDescriptor descriptor : propertyDescriptors) {
                    if (map.containsKey(descriptor.getName())) {
                        Method writeMethod = descriptor.getWriteMethod();
                        Object value = map.get(descriptor.getName());
                        writeMethod.invoke(target, convert(value, writeMethod.getParameterTypes()[0]));
                    }
                }
            } catch (Exception e) {
                throw new ApplicationRuntimeException(e);
            }
        } else if (target instanceof Map) {
            try {
                PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(source.getClass());
                Map<String, Object> map = (Map) target;
                for (PropertyDescriptor descriptor : propertyDescriptors) {
                    Method readMethod = descriptor.getReadMethod();
                    map.put(descriptor.getName(), readMethod.invoke(source, new Object[]{}));
                }
            } catch (Exception e) {
                throw new ApplicationRuntimeException(e);
            }
        } else {
            BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
        }
    }
 
    /**
     * 获取为空的属性名
     *
     * @param source 原对象
     * @return 源对象为空的属性名
     */
    private static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
 
        Set<String> emptyNames = new HashSet<>();
        for (PropertyDescriptor pd : pds) {
            Object srcValue = null;
            try {
                srcValue = src.getPropertyValue(pd.getName());
            } catch (NotReadablePropertyException e) {
                logger.error(pd.getName() + "属性获取出错", e);
            }
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
 
    /**
     * 将源对象列表转化为目标对象列表
     *
     * @param list        源对象列表
     * @param targetClass 目标对象类型
     * @param <T>         返回对象泛型
     * @return
     */
    public static <T> List<T> toList(List<?> list, Class<T> targetClass) {
        if (list == null) {
            return null;
        }
        List<T> result = new ArrayList<>();
        for (Object obj : list) {
            result.add(toObject(obj, targetClass));
        }
        return result;
    }
 
    /**
     * 将list 按 chunkSize 切片
     *
     * @param list      待切片数据
     * @param chunkSize 分片大小
     * @param <T>       返回对象泛型
     * @return
     */
    public static <T> List<List<T>> partition(List<T> list, int chunkSize) {
        if (list == null) {
            return null;
        }
        if (chunkSize <= 0) {
            throw new ApplicationRuntimeException("chunkSize 必须大于0.");
        }
        List<List<T>> result = new ArrayList<>();
        int index = 0;
        while (index < list.size()) {
            int nextInc = Math.min(list.size() - index, chunkSize);
            result.add(list.subList(index, index + nextInc));
            index += nextInc;
        }
        return result;
    }
 
    /**
     * 将多个业务信息转换为json数组
     *
     * @param datas "项目编号","BH001","项目预算","500w"
     * @return [{"name":"项目编号","value":"BH001"},{"name":"项目预算","value":"500w"}]
     * @throws Exception
     */
    public static String convert(String... datas) throws Exception {
        if (datas == null || datas.length < 0 || datas.length % 2 == 1) {
            throw new ApplicationRuntimeException("请确保参数数量大于0且为偶数!");
        }
//        String jsonData = null;
        int dataSize = datas.length / 2;
//        JSONArray jsonArray = new JSONArray();
//        for (int i = 0; i < dataSize; i++) {
//            int dataIndex = i * 2;
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("name", datas[dataIndex]);
//            jsonObject.put("value", datas[dataIndex + 1]);
//            jsonArray.put(jsonObject);
//        }
//        jsonData = jsonArray.toString();
        List<ObjectNode> resultList = new ArrayList<>(4);
        int dataIndex = -1;
        Map<String, Object> map = null;
        for (int i = 0; i < dataSize; i++){
            dataIndex = i * 2;
            map = new HashMap<>(8);
            map.put("name", datas[dataIndex]);
            map.put("value", datas[dataIndex + 1]);
            resultList.add(JsonUtils.mapToObjectNode(map));
        }
        return JsonUtils.toJsonArray(resultList);
    }
 
    /**
     * 将多个业务信息转换为json数组
     *
     * @param datas "xmid","1001",0,"项目编号","BH001",1,"项目预算","500w",1
     * @return [{"name":"xmid","value":"1001","show":0},{"name":"项目编号","value":"BH001","show":1},{"name":"项目预算","value":"500w","show":1}]
     * @throws Exception
     */
    public static String convert3(String... datas) throws Exception {
        if (datas == null || datas.length < 0 || datas.length % 3 != 0) {
            throw new ApplicationRuntimeException("请确保参数数量大于0且为3的倍数!");
        }
        String jsonData = null;
        int dataSize = datas.length / 3;
//        JSONArray jsonArray = new JSONArray();
//        for (int i = 0; i < dataSize; i++) {
//            int dataIndex = i * 3;
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("name", datas[dataIndex]);
//            jsonObject.put("value", datas[dataIndex + 1]);
//            jsonObject.put("show", datas[dataIndex + 2]);
//            jsonArray.put(jsonObject);
//        }
//        jsonData = jsonArray.toString();
        List<ObjectNode> resultList = new ArrayList<>(4);
        int dataIndex = -1;
        Map<String, Object> map = null;
        for (int i = 0; i < dataSize; i++){
            dataIndex = i * 3;
            map = new HashMap<>(8);
            map.put("name", datas[dataIndex]);
            map.put("value", datas[dataIndex + 1]);
            map.put("show", datas[dataIndex + 2]);
            resultList.add(JsonUtils.mapToObjectNode(map));
        }
        return JsonUtils.toJsonArray(resultList);
    }
 
    /**
     * 将bean的Stirng类型赋值为""
     *
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> T convertStrPropBlank(T bean) {
        if (bean != null) {
            PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(bean.getClass());
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (propertyDescriptor.getPropertyType().getName().equals("java.lang.String")) {
                    try {
                        if (propertyDescriptor.getReadMethod().invoke(bean) == null) {
                            propertyDescriptor.getWriteMethod().invoke(bean, "");
                        }
                    } catch (IllegalAccessException e) {
                        logger.error(e.getMessage(), e);
                    } catch (InvocationTargetException e) {
                        logger.error(e.getMessage(), e);
                    }
                }
            }
        }
        return bean;
    }
 
    /**
     * 把JavaBean转化为map
     *
     * @param bean
     * @return
     */
    public static Map<String, Object> beanTomap(Object bean) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //获取JavaBean的描述器
        BeanInfo b = Introspector.getBeanInfo(bean.getClass(), Object.class);
        //获取属性描述器
        PropertyDescriptor[] pds = b.getPropertyDescriptors();
        //对属性迭代
        for (PropertyDescriptor pd : pds) {
            //属性名称
            String propertyName = pd.getName();
            //属性值,用getter方法获取
            Method m = pd.getReadMethod();
            Object properValue = m.invoke(bean);//用对象执行getter方法获得属性值
 
            //把属性名-属性值 存到Map中
            map.put(propertyName, properValue);
        }
        return map;
    }
 
    /**
     * 把Map转化为JavaBean
     *
     * @param map
     * @return
     */
    /*public static <T> T mapTobean(Map<String,Object> map,Class<T> clz) throws Exception{
        //创建一个需要转换为的类型的对象
        T obj = clz.newInstance();
        //从Map中获取和属性名称一样的值,把值设置给对象(setter方法)
 
        //得到属性的描述器
        BeanInfo b = Introspector.getBeanInfo(clz,Object.class);
        PropertyDescriptor[] pds = b.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            //得到属性的setter方法
            Method setter = pd.getWriteMethod();
            //得到key名字和属性名字相同的value设置给属性
            setter.invoke(obj, map.get(pd.getName()));
        }
        return obj;
    }*/
    public static <T> T mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
        Object obj = clazz.newInstance();
        if (map != null && !map.isEmpty() && map.size() > 0) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String propertyName = entry.getKey();    // 属性名
                Object value = entry.getValue();        // 属性值
                String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
                Field field = getClassField(clazz, propertyName);    //获取和map的key匹配的属性名称
                if (field == null) {
                    continue;
                }
                Class<?> fieldTypeClass = field.getType();
                value = convertValType(value, fieldTypeClass);
                try {
                    clazz.getMethod(setMethodName, field.getType()).invoke(obj, value);
                } catch (NoSuchMethodException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        return (T) obj;
    }
 
    /**
     * 根据给定对象类匹配对象中的特定字段
     *
     * @param clazz
     * @param fieldName
     * @return
     */
    private static Field getClassField(Class<?> clazz, String fieldName) {
        if (Object.class.getName().equals(clazz.getName())) {
            return null;
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }
        Class<?> superClass = clazz.getSuperclass();    //如果该类还有父类,将父类对象中的字段也取出
        if (superClass != null) {                        //递归获取
            return getClassField(superClass, fieldName);
        }
        return null;
    }
 
    /**
     * 将map的value值转为实体类中字段类型匹配的方法
     *
     * @param value
     * @param fieldTypeClass
     * @return
     */
    private static Object convertValType(Object value, Class<?> fieldTypeClass) {
        Object retVal = null;
 
        if (Long.class.getName().equals(fieldTypeClass.getName())
                || long.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Long.parseLong(value.toString());
        } else if (Integer.class.getName().equals(fieldTypeClass.getName())
                || int.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Integer.parseInt(value.toString());
        } else if (Float.class.getName().equals(fieldTypeClass.getName())
                || float.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Float.parseFloat(value.toString());
        } else if (Double.class.getName().equals(fieldTypeClass.getName())
                || double.class.getName().equals(fieldTypeClass.getName())) {
            retVal = Double.parseDouble(value.toString());
        } else {
            retVal = value;
        }
        return retVal;
    }
}