Java 类型转换 工具类

迷南。 2022-06-07 04:48 270阅读 0赞
  1. public class TypeCastHelper {
  2. private static final String DEFAULT_STRING = "";
  3. private static final int DEFAULT_INT = 0;
  4. private static final long DEFAULT_LONG = 0L;
  5. private static final double DEFAULT_DOUBLE = 0.0D;
  6. private static final float DEFAULT_FLOAT = 0.0F;
  7. private static final boolean DEFAULT_BOOLEAN = Boolean.FALSE;
  8. public static String toString(Object obj, String defaultValue) {
  9. return obj != null ? obj.toString() : defaultValue;
  10. }
  11. public static String toString(Object obj) {
  12. return toString(obj, DEFAULT_STRING);
  13. }
  14. public static String toString(Integer intValue, String defaultValue) {
  15. return intValue != null ? String.valueOf(intValue) : defaultValue;
  16. }
  17. public static String toString(Integer intValue) {
  18. return toString(intValue, DEFAULT_STRING);
  19. }
  20. public static String toString(Long longValue, String defaultValue) {
  21. return longValue != null ? String.valueOf(longValue) : defaultValue;
  22. }
  23. public static String toString(Long longValue) {
  24. return toString(longValue, DEFAULT_STRING);
  25. }
  26. public static String toString(Boolean booleanValue, String defaultValue) {
  27. return booleanValue != null ? String.valueOf(booleanValue) : defaultValue;
  28. }
  29. public static String toString(Boolean booleanValue) {
  30. return toString(booleanValue, DEFAULT_STRING);
  31. }
  32. public static String toString(Double doubleValue, String defaultValue) {
  33. return null != doubleValue ? String.valueOf(doubleValue) : defaultValue;
  34. }
  35. public static String toString(Double doubleValue) {
  36. return toString(doubleValue, DEFAULT_STRING);
  37. }
  38. public static String toString(Float floatValue, String defaultValue) {
  39. return null != floatValue ? String.valueOf(floatValue) : defaultValue;
  40. }
  41. public static String toString(Float floatValue) {
  42. return toString(floatValue, DEFAULT_STRING);
  43. }
  44. public static int toInt(Object objectValue, int defaultValue) {
  45. int intValue = defaultValue;
  46. if (null != objectValue) {
  47. try {
  48. intValue = Integer.parseInt(toString(objectValue, toString(defaultValue)));
  49. } catch (NumberFormatException numberFormatException) {
  50. System.err.println(numberFormatException.getMessage());
  51. }
  52. }
  53. return intValue;
  54. }
  55. public static int toInt(Object objectValue) {
  56. return toInt(objectValue, DEFAULT_INT);
  57. }
  58. public static int toInt(String stringValue, int defaultValue) {
  59. int intValue = defaultValue;
  60. if (null != stringValue && stringValue.length() > 0) {
  61. try {
  62. intValue = Integer.parseInt(stringValue);
  63. } catch (NumberFormatException numberFormatException) {
  64. System.err.println(numberFormatException.getMessage());
  65. }
  66. }
  67. return intValue;
  68. }
  69. public static int toInt(Long longValue, int defaultValue) {
  70. int intValue = defaultValue;
  71. if (null != longValue) {
  72. intValue = longValue.intValue();
  73. }
  74. return intValue;
  75. }
  76. public static int toInt(Long longValue) {
  77. return toInt(longValue, DEFAULT_INT);
  78. }
  79. public static int toInt(Boolean booleanValue, int defaultValue) {
  80. int intValue = defaultValue;
  81. if (null != booleanValue) {
  82. intValue = booleanValue ? 1 : 0;
  83. }
  84. return intValue;
  85. }
  86. public static int toInt(Boolean booleanValue) {
  87. return toInt(booleanValue, DEFAULT_INT);
  88. }
  89. public static int toInt(Double doubleValue, int defaultValue) {
  90. int intValue = defaultValue;
  91. if (null != doubleValue) {
  92. intValue = doubleValue.intValue();
  93. }
  94. return intValue;
  95. }
  96. public static int toInt(Double doubleValue) {
  97. return toInt(doubleValue, DEFAULT_INT);
  98. }
  99. public static int toInt(Float floatValue, int defaultValue) {
  100. int intValue = defaultValue;
  101. if (null != floatValue) {
  102. intValue = floatValue.intValue();
  103. }
  104. return intValue;
  105. }
  106. public static int toInt(Float floatValue) {
  107. return toInt(floatValue, DEFAULT_INT);
  108. }
  109. public static long toLong(Object objectValue, long defaultValue) {
  110. long longValue = defaultValue;
  111. if (null != objectValue) {
  112. try {
  113. longValue = Long.parseLong(toString(objectValue, toString(defaultValue)));
  114. } catch (NumberFormatException numberFormatException) {
  115. System.err.println(numberFormatException.getMessage());
  116. }
  117. }
  118. return longValue;
  119. }
  120. public static long toLong(Object objectValue) {
  121. return toLong(objectValue, DEFAULT_LONG);
  122. }
  123. public static long toLong(String stringValue, long defaultValue) {
  124. long longValue = defaultValue;
  125. if (null != stringValue && stringValue.length() > 0) {
  126. longValue = Long.parseLong(stringValue);
  127. }
  128. return longValue;
  129. }
  130. public static long toLong(String stringValue) {
  131. return toLong(stringValue, DEFAULT_LONG);
  132. }
  133. public static long toLong(Integer intValue, long defaultValue) {
  134. long longValue = defaultValue;
  135. if (null != intValue) {
  136. longValue = intValue.longValue();
  137. }
  138. return longValue;
  139. }
  140. public static long toLong(Integer intValue) {
  141. return toLong(intValue, DEFAULT_LONG);
  142. }
  143. public static long toLong(Boolean booleanValue, long defaultValue) {
  144. long longValue = defaultValue;
  145. if (null != booleanValue) {
  146. longValue = booleanValue ? 1L : 0L;
  147. }
  148. return longValue;
  149. }
  150. public static long toLong(Boolean booleanValue) {
  151. return toLong(booleanValue, DEFAULT_LONG);
  152. }
  153. public static long toLong(Double doubleValue, long defaultValue) {
  154. long longValue = defaultValue;
  155. if (null != doubleValue) {
  156. longValue = doubleValue.longValue();
  157. }
  158. return longValue;
  159. }
  160. public static long toLong(Double doubleValue) {
  161. return toLong(doubleValue, DEFAULT_LONG);
  162. }
  163. public static long toLong(Float floatValue, long defaultValue) {
  164. long longValue = defaultValue;
  165. if (null != floatValue) {
  166. longValue = floatValue.longValue();
  167. }
  168. return longValue;
  169. }
  170. public static long toLong(Float floatValue) {
  171. return toLong(floatValue, DEFAULT_LONG);
  172. }
  173. public static boolean toBoolean(Object objectValue, boolean defaultValue) {
  174. boolean booleanValue = defaultValue;
  175. if (null != objectValue) {
  176. booleanValue = Boolean.valueOf(toString(objectValue, toString(defaultValue)));
  177. }
  178. return booleanValue;
  179. }
  180. public static boolean toBoolean(Object objectValue) {
  181. return toBoolean(objectValue, DEFAULT_BOOLEAN);
  182. }
  183. public static boolean toBoolean(String stringValue, boolean defaultValue) {
  184. boolean booleanValue = defaultValue;
  185. if (null != stringValue && stringValue.length() > 0) {
  186. booleanValue = Boolean.valueOf(stringValue);
  187. }
  188. return booleanValue;
  189. }
  190. public static boolean toBoolean(String stringValue) {
  191. return toBoolean(stringValue, DEFAULT_BOOLEAN);
  192. }
  193. public static boolean toBoolean(Integer intValue, boolean defaultValue) {
  194. boolean booleanValue = defaultValue;
  195. if (null != intValue) {
  196. booleanValue = intValue == 1;
  197. }
  198. return booleanValue;
  199. }
  200. public static boolean toBoolean(Integer intValue) {
  201. return toBoolean(intValue, DEFAULT_BOOLEAN);
  202. }
  203. public static boolean toBoolean(Long longValue, boolean defaultValue) {
  204. boolean booleanValue = defaultValue;
  205. if (null != longValue) {
  206. booleanValue = longValue.longValue() == 1L;
  207. }
  208. return booleanValue;
  209. }
  210. public static boolean toBoolean(Long longValue) {
  211. return toBoolean(longValue, DEFAULT_BOOLEAN);
  212. }
  213. public static float toFloat(Object objectValue, float defaultValue) {
  214. float floatValue = defaultValue;
  215. if (null != objectValue) {
  216. try {
  217. floatValue = Float.valueOf(toString(objectValue, toString(defaultValue)));
  218. } catch (NumberFormatException numberFormatException) {
  219. System.err.println(numberFormatException);
  220. }
  221. }
  222. return floatValue;
  223. }
  224. public static float toFloat(Object objectValue) {
  225. return toFloat(objectValue, DEFAULT_FLOAT);
  226. }
  227. public static float toFloat(String stringValue, float defaultValue) {
  228. float floatValue = defaultValue;
  229. if (null != stringValue && stringValue.length() > 0) {
  230. try {
  231. floatValue = Float.valueOf(stringValue);
  232. } catch (NumberFormatException numberFormatException) {
  233. System.err.println(numberFormatException.getMessage());
  234. }
  235. }
  236. return floatValue;
  237. }
  238. public static float toFloat(String stringValue) {
  239. return toFloat(stringValue, DEFAULT_FLOAT);
  240. }
  241. public static float toFloat(Integer intValue, float defaultValue) {
  242. float floatValue = defaultValue;
  243. if (null != intValue) {
  244. floatValue = intValue.floatValue();
  245. }
  246. return floatValue;
  247. }
  248. public static float toFloat(Integer intValue) {
  249. return toFloat(intValue, DEFAULT_FLOAT);
  250. }
  251. public static float toFloat(Long longValue, float defaultValue) {
  252. float floatValue = defaultValue;
  253. if (null != longValue) {
  254. floatValue = longValue.floatValue();
  255. }
  256. return floatValue;
  257. }
  258. public static float toFloat(Long longValue) {
  259. return toFloat(longValue, DEFAULT_FLOAT);
  260. }
  261. public static float toFloat(Double doubleValue, float defaultValue) {
  262. float floatValue = defaultValue;
  263. if (null != doubleValue) {
  264. floatValue = doubleValue.floatValue();
  265. }
  266. return floatValue;
  267. }
  268. public static float toFloat(Double doubleValue) {
  269. return toFloat(doubleValue, DEFAULT_FLOAT);
  270. }
  271. public static double toDouble(Object objectValue, double defaultValue) {
  272. double doubleValue = defaultValue;
  273. if (null != objectValue) {
  274. doubleValue = Double.parseDouble(toString(objectValue, toString(defaultValue)));
  275. }
  276. return doubleValue;
  277. }
  278. public static double toDouble(Object objectValue) {
  279. return toDouble(objectValue, DEFAULT_DOUBLE);
  280. }
  281. public static double toDouble(String stringValue, double defaultValue) {
  282. double doubleValue = defaultValue;
  283. if (null != stringValue && stringValue.length() > 0) {
  284. doubleValue = Double.parseDouble(stringValue);
  285. }
  286. return doubleValue;
  287. }
  288. public static double toDouble(String stringValue) {
  289. return toDouble(stringValue, DEFAULT_DOUBLE);
  290. }
  291. public static double toDouble(Integer intValue, double defaultValue) {
  292. double doubleValue = defaultValue;
  293. if (null != intValue) {
  294. doubleValue = intValue.doubleValue();
  295. }
  296. return doubleValue;
  297. }
  298. public static double toDouble(Integer intValue) {
  299. return toDouble(intValue, DEFAULT_DOUBLE);
  300. }
  301. public static double toDouble(Long longValue, double defaultValue) {
  302. double doubleValue = defaultValue;
  303. if (null != longValue) {
  304. doubleValue = longValue.doubleValue();
  305. }
  306. return doubleValue;
  307. }
  308. public static double toDouble(Long longValue) {
  309. return toDouble(longValue, DEFAULT_DOUBLE);
  310. }
  311. public static double toDouble(Float floatValue, double defaultValue) {
  312. double doubleValue = defaultValue;
  313. if (null != floatValue) {
  314. doubleValue = floatValue.doubleValue();
  315. }
  316. return doubleValue;
  317. }
  318. public static double toDouble(Float floatValue) {
  319. return toDouble(floatValue, DEFAULT_DOUBLE);
  320. }
  321. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java类型转换工具

    不多说,直接上代码,亲们可以直接复制使用,里面包括了常用的类型转换,一般而言足够使用了! public class ObjectParser \{     publi