Repository navigation
Expand file tree
/
Copy pathCastUtil.java
More file actions
197 lines (181 loc) · 5.15 KB
/
Copy pathCastUtil.java
File metadata and controls
197 lines (181 loc) · 5.15 KB
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
package bms.core.common.util;
/**
* 转型操作工具类
*
* @author huangyong
* @since 1.0
*/
public class CastUtil {
/**
* 转为 String 型
*/
public static String castString(Object obj) {
return CastUtil.castString(obj, "");
}
/**
* 转为 String 型(提供默认值)
*/
public static String castString(Object obj, String defaultValue) {
return obj != null ? String.valueOf(obj) : defaultValue;
}
/**
* 转为 double 型
*/
public static double castDouble(Object obj) {
return CastUtil.castDouble(obj, 0);
}
/**
* 转为 double 型(提供默认值)
*/
public static double castDouble(Object obj, double defaultValue) {
double doubleValue = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtil.isNotEmpty(strValue)) {
try {
doubleValue = Double.parseDouble(strValue);
} catch (NumberFormatException e) {
doubleValue = defaultValue;
}
}
}
return doubleValue;
}
/**
* 转为 long 型
*/
public static long castLong(Object obj) {
return CastUtil.castLong(obj, 0);
}
/**
* 转为 long 型(提供默认值)
*/
public static long castLong(Object obj, long defaultValue) {
long longValue = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtil.isNotEmpty(strValue)) {
try {
longValue = Long.parseLong(strValue);
} catch (NumberFormatException e) {
longValue = defaultValue;
}
}
}
return longValue;
}
/**
* 转为 int 型
*/
public static int castInt(Object obj) {
return CastUtil.castInt(obj, 0);
}
/**
* 转为 int 型(提供默认值)
*/
public static int castInt(Object obj, int defaultValue) {
int intValue = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtil.isNotEmpty(strValue)) {
try {
intValue = Integer.parseInt(strValue);
} catch (NumberFormatException e) {
intValue = defaultValue;
}
}
}
return intValue;
}
/**
* 转为 boolean 型
*/
public static boolean castBoolean(Object obj) {
return CastUtil.castBoolean(obj, false);
}
/**
* 转为 boolean 型(提供默认值)
*/
public static boolean castBoolean(Object obj, boolean defaultValue) {
boolean booleanValue = defaultValue;
if (obj != null) {
booleanValue = Boolean.parseBoolean(castString(obj));
}
return booleanValue;
}
/**
* 转为 String[] 型
*/
public static String[] castStringArray(Object[] objArray) {
if (objArray == null) {
objArray = new Object[0];
}
String[] strArray = new String[objArray.length];
if (ArrayUtil.isNotEmpty(objArray)) {
for (int i = 0; i < objArray.length; i++) {
strArray[i] = castString(objArray[i]);
}
}
return strArray;
}
/**
* 转为 double[] 型
*/
public static double[] castDoubleArray(Object[] objArray) {
if (objArray == null) {
objArray = new Object[0];
}
double[] doubleArray = new double[objArray.length];
if (!ArrayUtil.isEmpty(objArray)) {
for (int i = 0; i < objArray.length; i++) {
doubleArray[i] = castDouble(objArray[i]);
}
}
return doubleArray;
}
/**
* 转为 long[] 型
*/
public static long[] castLongArray(Object[] objArray) {
if (objArray == null) {
objArray = new Object[0];
}
long[] longArray = new long[objArray.length];
if (!ArrayUtil.isEmpty(objArray)) {
for (int i = 0; i < objArray.length; i++) {
longArray[i] = castLong(objArray[i]);
}
}
return longArray;
}
/**
* 转为 int[] 型
*/
public static int[] castIntArray(Object[] objArray) {
if (objArray == null) {
objArray = new Object[0];
}
int[] intArray = new int[objArray.length];
if (!ArrayUtil.isEmpty(objArray)) {
for (int i = 0; i < objArray.length; i++) {
intArray[i] = castInt(objArray[i]);
}
}
return intArray;
}
/**
* 转为 boolean[] 型
*/
public static boolean[] castBooleanArray(Object[] objArray) {
if (objArray == null) {
objArray = new Object[0];
}
boolean[] booleanArray = new boolean[objArray.length];
if (!ArrayUtil.isEmpty(objArray)) {
for (int i = 0; i < objArray.length; i++) {
booleanArray[i] = castBoolean(objArray[i]);
}
}
return booleanArray;
}
}