DataUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. package com.behosoft.util;
  2. import java.lang.reflect.Array;
  3. import java.text.ParseException;
  4. import java.util.Date;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import com.behosoft.lis.bc.routeConfig.service.RouteConfigService;
  8. import com.behosoft.lis.data.dictionary.service.DictionaryService;
  9. import com.behosoft.lis.model.vo.DictionaryVO;
  10. import com.behosoft.lis.model.vo.RouteConfigVO;
  11. import com.wondersgroup.framework.util.preloadutil.AppContextUtils;
  12. /**
  13. *
  14. * <pre>
  15. * 数据转换工具类
  16. * </pre>
  17. *
  18. * <br>
  19. * JDK版本:1.6
  20. *
  21. * @author
  22. * @version 1.0
  23. * @see The author for more details
  24. * @since 1.0
  25. */
  26. public class DataUtils {
  27. private static Log log = (Log) LogFactory.getLog(DataUtils.class);
  28. /**
  29. * 返回指定对象的int值, 如果无法解析成int, 返回0
  30. *
  31. * @param obj
  32. * @return
  33. */
  34. public static int asInt(Object obj) {
  35. return asInt(obj, 0);
  36. }
  37. /**
  38. *
  39. * @param param
  40. * @return 字符串
  41. */
  42. public static String asString(Object param) {
  43. return asString(param, "");
  44. }
  45. /**
  46. * 返回指定对象的String形式,如果对象为null返回默认值,否则返回对象的toString()方法
  47. *
  48. * @param obj
  49. * @param defaultValue
  50. * @return
  51. */
  52. public static String asString(Object obj, String defaultValue) {
  53. String value = defaultValue;
  54. if (obj instanceof String) {
  55. value = (String) obj;
  56. } else if (obj != null) {
  57. value = obj.toString();
  58. }
  59. return value;
  60. }
  61. /**
  62. * 返回指定对象的String形式,如果对象为null返回默认值,否则返回对象的toString()方法
  63. *
  64. * @param obj
  65. * @param defaultValue
  66. * @return
  67. */
  68. public static String asStringExt(Object obj, String defaultValue) {
  69. String value = defaultValue;
  70. if (obj instanceof String) {
  71. value = (String) obj;
  72. } else if (obj != null) {
  73. value = obj.toString();
  74. }
  75. if (value.endsWith(".0"))
  76. {
  77. value = value.substring(0, value.indexOf("."));
  78. }
  79. return value;
  80. }
  81. /**
  82. * 返回指定对象的int值,如果无法解析成int值,返回defaultValue
  83. *
  84. * @param obj
  85. * @param defaultValue
  86. * @return
  87. */
  88. public static int asInt(Object obj, int defaultValue) {
  89. int value = defaultValue;
  90. if (obj instanceof String) {
  91. String str = (String) obj;
  92. if (StringUtils.isNotBlank(str)) {
  93. try {
  94. value = Integer.parseInt(str);
  95. } catch (NumberFormatException e) {
  96. }
  97. }
  98. } else if (obj instanceof Number) {
  99. value = ((Number) obj).intValue();
  100. }
  101. return value;
  102. }
  103. /**
  104. * 返回指定对象的long值,如果无法解析成long值,返回defaultValue
  105. *
  106. * @param obj
  107. * @param defaultValue
  108. * @return
  109. */
  110. public static long asLong(Object obj, long defaultValue) {
  111. long value = defaultValue;
  112. if (obj instanceof String) {
  113. String str = (String) obj;
  114. if (StringUtils.isNotBlank(str)) {
  115. try {
  116. value = Long.parseLong(str);
  117. } catch (NumberFormatException e) {
  118. }
  119. }
  120. } else if (obj instanceof Number) {
  121. value = ((Number) obj).longValue();
  122. }
  123. return value;
  124. }
  125. /** 求差
  126. * @param a
  127. * @param b
  128. * @return
  129. * @see [类、类#方法、类#成员]
  130. */
  131. public static double subDouble(Object a,Object b)
  132. {
  133. return asDouble(a, 0d) - asDouble(b, 0d);
  134. }
  135. /** 求差
  136. * @param a
  137. * @param b
  138. * @return
  139. * @see [类、类#方法、类#成员]
  140. */
  141. public static int subInt(int a,int b)
  142. {
  143. return asInt(a, 0) - asInt(b, 0);
  144. }
  145. /**
  146. * 返回指定对象的long值,如果无法解析成long值,返回0L
  147. *
  148. * @param obj
  149. * @return
  150. */
  151. public static long asLong(Object obj) {
  152. return asLong(obj, 0L);
  153. }
  154. /**
  155. * 返回指定对象的float值,如果无法解析成float值,返回defaultValue
  156. *
  157. * @param obj
  158. * @param defaultValue
  159. * @return
  160. */
  161. public static float asFloat(Object obj, float defaultValue) {
  162. float value = defaultValue;
  163. if (obj instanceof String) {
  164. String str = (String) obj;
  165. if (StringUtils.isNotBlank(str)) {
  166. try {
  167. value = Float.parseFloat(str);
  168. } catch (NumberFormatException e) {
  169. }
  170. }
  171. } else if (obj instanceof Number) {
  172. value = ((Number) obj).floatValue();
  173. }
  174. return value;
  175. }
  176. /**
  177. * 返回指定对象的float值,如果无法解析成float值,返回0f
  178. *
  179. * @param obj
  180. * @return
  181. */
  182. public static float asFlost(Object obj) {
  183. return asFloat(obj, 0f);
  184. }
  185. /**
  186. * 返回指定对象的double值,如果无法解析成double值,返回0.0
  187. *
  188. * @param obj
  189. * @return
  190. */
  191. public static Object asDouble(Object obj) {
  192. return asDouble(obj, 0.0);
  193. }
  194. /**
  195. * 返回指定对象的double值,如果无法解析成double值,返回defaultValue
  196. *
  197. * @param obj
  198. * @param defaultValue
  199. * @return
  200. */
  201. public static double asDouble(Object obj, double defaultValue) {
  202. double value = defaultValue;
  203. if (obj instanceof String) {
  204. String str = (String) obj;
  205. if (StringUtils.isNotBlank(str)) {
  206. try {
  207. value = Double.parseDouble(str);
  208. } catch (NumberFormatException e) {
  209. }
  210. }
  211. } else if (obj instanceof Number) {
  212. value = ((Number) obj).doubleValue();
  213. }
  214. return value;
  215. }
  216. /**
  217. * 返回指定对象的boolean值
  218. *
  219. * @param obj
  220. * @return
  221. */
  222. public static boolean asBoolean(Object obj) {
  223. return asBoolean(obj, false);
  224. }
  225. /**
  226. *
  227. * 返回指定对象的boolean值<br>
  228. * 示例:DataUtils.asBoolean("TRUE",defaultValue) 返回true<br>
  229. * 示例:DataUtils.asBoolean("yes",defaultValue) 返回defaultValue<br>
  230. * 示例:DataUtils.asBoolean("False",defaultValue) 返回false<br>
  231. * 示例:DataUtils.asBoolean((Integer)1,defaultValue) 返回true<br>
  232. * 示例:DataUtils.asBoolean((Integer)0,defaultValue) 返回false<br>
  233. * 示例:DataUtils.asBoolean(Boolean.TRUE,defaultValue) 返回true<br>
  234. *
  235. * @param obj
  236. * @param defaultValue
  237. * 如果无法正确转换,返回的默认值
  238. * @return
  239. */
  240. public static boolean asBoolean(Object obj, boolean defaultValue) {
  241. if (obj instanceof Boolean) {
  242. return (Boolean) obj;
  243. } else if (obj instanceof Number) {
  244. if (((Number) obj).intValue() == 0)
  245. return false;
  246. else
  247. return true;
  248. } else if (obj != null) {
  249. String str = obj.toString();
  250. if (str.equalsIgnoreCase("true"))
  251. return true;
  252. else if (str.equalsIgnoreCase("false"))
  253. return false;
  254. else
  255. return defaultValue;
  256. } else {
  257. return defaultValue;
  258. }
  259. }
  260. /**
  261. * 返回指定对象的Date值,obj有有效的数据格式为:java.util.Date,Long和满足yyyy-MM-dd HH:mm:ss的字符串
  262. *
  263. * @param obj
  264. * @param defaultValue
  265. * 如果无法转换,返回的默认值
  266. * @return
  267. */
  268. public static Date asDate(Object obj, Date defaultValue) {
  269. if (obj == null)
  270. return defaultValue;
  271. if (obj instanceof Date) {
  272. return (Date) obj;
  273. }
  274. if (obj instanceof Number) {
  275. return new Date(((Number) obj).longValue());
  276. }
  277. String str = obj.toString();
  278. try {
  279. return DateUtils.getDate(str, "yyyy-MM-dd HH:mm:ss");
  280. } catch (ParseException e) {
  281. try {
  282. return DateUtils.getDate(str, "yyyy-MM-dd");
  283. } catch (ParseException e1) {
  284. log.warn("解析对象[" + obj + "]成时间格式错误");
  285. }
  286. return defaultValue;
  287. }
  288. }
  289. /**
  290. * 返回指定对象的Date值,obj有有效的数据格式为:java.util.Date,Long和满足yyyy-MM-dd HH:mm:ss的字符串
  291. *
  292. * @param obj
  293. * @return
  294. */
  295. public static Date asDate(Object obj) {
  296. return asDate(obj, null);
  297. }
  298. /**
  299. * 根据指定的数据类型转换对象
  300. *
  301. * @param obj
  302. * @param targetClass
  303. * 目标数据类型
  304. * @return
  305. */
  306. @SuppressWarnings("unchecked")
  307. public static Object as(Object obj, Class targetClass) {
  308. if (targetClass == String.class) {
  309. return asString(obj);
  310. }
  311. if (targetClass == Boolean.class || targetClass == Boolean.TYPE) {
  312. return asBoolean(obj);
  313. }
  314. if (targetClass == Integer.class || targetClass == Integer.TYPE) {
  315. return asInt(obj);
  316. }
  317. if (Date.class == targetClass) {
  318. return asDate(obj);
  319. }
  320. if (targetClass == Double.class || targetClass == Double.TYPE) {
  321. return asDouble(obj);
  322. }
  323. if (targetClass == Float.class || targetClass == Float.TYPE) {
  324. return asFlost(obj);
  325. }
  326. if (targetClass == Long.class || targetClass == Long.TYPE) {
  327. return asLong(obj);
  328. }
  329. if (targetClass.isEnum()) {
  330. return asEnum(obj, (Class<? extends Enum>) targetClass);
  331. }
  332. return obj;
  333. }
  334. /**
  335. * 返回枚举类型的数据,如果无法转换,返回null
  336. *
  337. * @param <T>
  338. * @param obj
  339. * @param enumClass
  340. * @return
  341. */
  342. public static <T extends Enum<T>> T asEnum(Object obj, Class<T> enumClass) {
  343. if (obj == null)
  344. return null;
  345. String name = null;
  346. int i = -1;
  347. if (obj instanceof String && StringUtils.isNotBlank((String) obj)) {
  348. String str = (String) obj;
  349. if (Character.isDigit(str.charAt(0))) {
  350. i = asInt(str, -1);
  351. } else {
  352. name = str;
  353. }
  354. } else {
  355. i = asInt(obj, -1);
  356. }
  357. if (i >= 0) {
  358. T[] arr = enumClass.getEnumConstants();
  359. if (i < arr.length) {
  360. return arr[i];
  361. } else
  362. return null;
  363. } else if (name != null)
  364. return Enum.valueOf(enumClass, name);
  365. else
  366. return null;
  367. }
  368. public static int[] asIntArray(Object obj, int defaultValue) {
  369. if (obj == null)
  370. return null;
  371. if (obj.getClass().isArray()) {
  372. if (obj.getClass().getComponentType() == Integer.TYPE)
  373. return (int[]) obj;
  374. else {
  375. int l = Array.getLength(obj);
  376. int[] array = new int[l];
  377. for (int i = 0; i < l; i++) {
  378. array[i] = asInt(Array.get(obj, i), defaultValue);
  379. }
  380. return array;
  381. }
  382. } else {
  383. return new int[] { asInt(obj, defaultValue) };
  384. }
  385. }
  386. public static int[] asIntArray(Object obj) {
  387. return asIntArray(obj, 0);
  388. }
  389. public static double[] asDoubleArray(Object obj, double defaultValue) {
  390. if (obj == null)
  391. return null;
  392. if (obj.getClass().isArray()) {
  393. if (obj.getClass().getComponentType() == Double.TYPE)
  394. return (double[]) obj;
  395. else {
  396. int l = Array.getLength(obj);
  397. double[] array = new double[l];
  398. for (int i = 0; i < l; i++) {
  399. array[i] = asDouble(Array.get(obj, i), defaultValue);
  400. }
  401. return array;
  402. }
  403. } else {
  404. return new double[] { asDouble(obj, defaultValue) };
  405. }
  406. }
  407. public static double[] asDoubleArray(Object obj) {
  408. return asDoubleArray(obj, 0.0);
  409. }
  410. public static long[] asLongArray(Object obj, long defaultValue) {
  411. if (obj == null)
  412. return null;
  413. if (obj.getClass().isArray()) {
  414. if (obj.getClass().getComponentType() == Long.TYPE)
  415. return (long[]) obj;
  416. else {
  417. int l = Array.getLength(obj);
  418. long[] array = new long[l];
  419. for (int i = 0; i < l; i++) {
  420. array[i] = asLong(Array.get(obj, i), defaultValue);
  421. }
  422. return array;
  423. }
  424. } else {
  425. return new long[] { asLong(obj, defaultValue) };
  426. }
  427. }
  428. public static long[] asLongArray(Object obj) {
  429. return asLongArray(obj, 0l);
  430. }
  431. public static float[] asFloatArray(Object obj, float defaultValue) {
  432. if (obj == null)
  433. return null;
  434. if (obj.getClass().isArray()) {
  435. if (obj.getClass().getComponentType() == Float.TYPE)
  436. return (float[]) obj;
  437. else {
  438. int l = Array.getLength(obj);
  439. float[] array = new float[l];
  440. for (int i = 0; i < l; i++) {
  441. array[i] = asFloat(Array.get(obj, i), defaultValue);
  442. }
  443. return array;
  444. }
  445. } else {
  446. return new float[] { asFloat(obj, defaultValue) };
  447. }
  448. }
  449. public static float[] asFloatArray(Object obj) {
  450. return asFloatArray(obj, 0f);
  451. }
  452. public static Date[] asDateArray(Object obj) {
  453. if (obj == null)
  454. return null;
  455. if (obj.getClass().isArray()) {
  456. if (obj.getClass().getComponentType() == Date.class)
  457. return (Date[]) obj;
  458. else {
  459. int l = Array.getLength(obj);
  460. Date[] array = new Date[l];
  461. for (int i = 0; i < l; i++) {
  462. array[i] = asDate(Array.get(obj, i));
  463. }
  464. return array;
  465. }
  466. } else {
  467. return new Date[] { asDate(obj) };
  468. }
  469. }
  470. /**
  471. * 从类名称获取对应的Class类型,有效的类名称可以是标准Class.getName()的名称或是java编码习惯上定义类型的文本,<br>
  472. * 如以下内容都是有效的typeStr,如果类名称是java.lang.开头的,可以省略java.lang.:<br>
  473. * java.lang.String <br>
  474. * String <br>
  475. * Integer <br>
  476. * int <br>
  477. * int[] <br>
  478. * long[][] <br>
  479. * [Ljava.lang.String; (Class名称标准写法,等同String[]) <br>
  480. * [[I (Class名称标准写法,等同int[][])
  481. *
  482. * @param typeStr
  483. * @return
  484. * @throws ClassNotFoundException
  485. */
  486. public static Class toClass(String typeStr) throws ClassNotFoundException {
  487. Class type;
  488. if ("boolean".equals(typeStr)) {
  489. type = Boolean.TYPE;
  490. } else if ("int".equals(typeStr)) {
  491. type = Integer.TYPE;
  492. } else if ("long".equals(typeStr)) {
  493. type = Long.TYPE;
  494. } else if ("double".equals(typeStr)) {
  495. type = Double.TYPE;
  496. } else if ("float".equals(typeStr)) {
  497. type = Float.TYPE;
  498. } else if ("byte".equals(typeStr)) {
  499. type = Byte.TYPE;
  500. } else if ("short".equals(typeStr)) {
  501. type = Short.TYPE;
  502. } else if ("char".equals(typeStr)) {
  503. type = Character.TYPE;
  504. } else {
  505. int i = typeStr.indexOf("[]");
  506. String str = typeStr;
  507. if (i > 0) {
  508. str = typeStr.substring(0, i);
  509. int d = 1;
  510. while ((i = typeStr.indexOf("[]", i + 2)) > 0) {
  511. d++;
  512. }
  513. Class compType = toClass(str);
  514. type = Array.newInstance(compType, new int[d]).getClass();
  515. } else {
  516. try {
  517. type = Class.forName(str);
  518. } catch (ClassNotFoundException e) {
  519. str = "java.lang." + str;
  520. try {
  521. type = Class.forName(str);
  522. } catch (ClassNotFoundException e1) {
  523. throw e;
  524. }
  525. }
  526. }
  527. }
  528. return type;
  529. }
  530. /**
  531. * 字典ID转换成字典名称
  532. * @param id
  533. * @return
  534. */
  535. public static String asDictionary(String id){
  536. String ret ="";
  537. if( !StringUtils.isBlank(id)){
  538. DictionaryService dictionaryService = AppContextUtils.getApplicationContext().getBean(DictionaryService.class);
  539. DictionaryVO vo = dictionaryService.getDictionaryVoById(id);
  540. if( vo!=null ){
  541. ret = vo.getDictionaryName();
  542. }
  543. }
  544. return ret;
  545. }
  546. /**
  547. * 路向ID转换成路向名称
  548. * @param routeId
  549. * @return
  550. */
  551. public static String asRoute(String routeId){
  552. String ret ="";
  553. if( !StringUtils.isBlank(routeId)){
  554. RouteConfigService routeConfigService = AppContextUtils.getApplicationContext().getBean(RouteConfigService.class);
  555. RouteConfigVO vo = routeConfigService.getRouteConfigVOById(routeId);
  556. if( vo!=null ){
  557. ret = vo.getRouteName();
  558. }
  559. }
  560. return ret;
  561. }
  562. }