Skip to content

Commit cd4e80b

Browse files
committed
util
1 parent dc95bb5 commit cd4e80b

File tree

1 file changed

+351
-0
lines changed

1 file changed

+351
-0
lines changed
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
package info.xiaomo.core.untils;
2+
3+
import java.io.*;
4+
import java.nio.ByteBuffer;
5+
import java.util.HashMap;
6+
import java.util.Hashtable;
7+
import java.util.Iterator;
8+
9+
/**
10+
* 把今天最好的表现当作明天最新的起点..~
11+
* いま 最高の表現 として 明日最新の始発..~
12+
* Today the best performance as tomorrow newest starter!
13+
* Created by IntelliJ IDEA.
14+
*
15+
* @author: xiaomo
16+
* @github: https://github.com/qq83387856
17+
* @email: hupengbest@163.com
18+
* @QQ_NO: 83387856
19+
* @Date: 2016/11/22 14:55
20+
* @Copyright(©) 2015 by xiaomo.
21+
**/
22+
23+
public class CastUtil {
24+
protected static final ByteArrayOutputStream out = new ByteArrayOutputStream();
25+
protected static ObjectOutputStream oos;
26+
27+
public CastUtil() {
28+
}
29+
30+
public static int toInteger(Object str) {
31+
return str == null ? 0 : str instanceof Number ? ((Number) str).intValue() : toInteger(str.toString());
32+
}
33+
34+
public static double toDouble(Object number) {
35+
if (number == null) {
36+
return 0.0D;
37+
} else if (number instanceof Number) {
38+
return ((Number) number).doubleValue();
39+
} else if (number instanceof String) {
40+
String str = (String) number;
41+
return isNumeric(str) > 0 ? Double.valueOf(str) : 0.0D;
42+
} else {
43+
return 0.0D;
44+
}
45+
}
46+
47+
public static long toLong(Object number) {
48+
if (number == null) {
49+
return 0L;
50+
} else if (number instanceof Number) {
51+
return ((Number) number).longValue();
52+
} else if (number instanceof String) {
53+
String str = (String) number;
54+
int isNumber = isNumeric(str);
55+
return isNumber == 1 ? Long.parseLong(str) : (isNumber == 2 ? Double.valueOf(str).longValue() : 0L);
56+
} else {
57+
return 0L;
58+
}
59+
}
60+
61+
public static int toInteger(String str) {
62+
if (str == null) {
63+
return 0;
64+
} else {
65+
str = str.trim();
66+
if (str.length() == 0) {
67+
return 0;
68+
} else {
69+
int i = isNumeric(str);
70+
return i == 1 ? Integer.parseInt(str) : (i == 2 ? Double.valueOf(str).intValue() : 0);
71+
}
72+
}
73+
}
74+
75+
public static int isNumeric(String str) {
76+
if (str == null) {
77+
return 0;
78+
} else {
79+
boolean isdouble = false;
80+
boolean hasE = false;
81+
int i = str.length();
82+
83+
while (true) {
84+
while (true) {
85+
char c;
86+
do {
87+
--i;
88+
if (i < 0) {
89+
if (isdouble) {
90+
return 2;
91+
}
92+
93+
return 1;
94+
}
95+
96+
c = str.charAt(i);
97+
} while (i == 0 && c == 45);
98+
99+
if (c == 46) {
100+
if (isdouble) {
101+
return 0;
102+
}
103+
104+
isdouble = true;
105+
} else if (c != 69 && c != 101) {
106+
if (!Character.isDigit(str.charAt(i))) {
107+
return 0;
108+
}
109+
} else {
110+
if (hasE) {
111+
return 0;
112+
}
113+
114+
hasE = true;
115+
}
116+
}
117+
}
118+
}
119+
}
120+
121+
public static HashMap copyMap(HashMap map) {
122+
HashMap<Object, Object> newmap = new HashMap<>();
123+
124+
for (Object key : map.keySet()) {
125+
newmap.put(key, map.get(key));
126+
}
127+
128+
return newmap;
129+
}
130+
131+
public static void destroy(Hashtable map) {
132+
for (Iterator it = map.keySet().iterator(); it.hasNext(); it.remove()) {
133+
Object key = it.next();
134+
Object value = map.get(key);
135+
if (value instanceof HashMap) {
136+
HashMap valueMap = (HashMap) value;
137+
destroy(valueMap);
138+
}
139+
}
140+
141+
}
142+
143+
public static void destroy(HashMap map) {
144+
for (Iterator keyit = map.keySet().iterator(); keyit.hasNext(); keyit.remove()) {
145+
Object key = keyit.next();
146+
Object value = map.get(key);
147+
if (value instanceof HashMap) {
148+
HashMap valueMap = (HashMap) value;
149+
destroy(valueMap);
150+
}
151+
}
152+
153+
}
154+
155+
public static String objectToString(Object obj) {
156+
if (obj.getClass().equals(String.class)) {
157+
return obj.toString();
158+
} else {
159+
ByteArrayOutputStream out = new ByteArrayOutputStream();
160+
161+
try {
162+
ObjectOutputStream e = new ObjectOutputStream(out);
163+
e.writeObject(obj);
164+
byte[] bytes = out.toByteArray();
165+
return new String(bytes, "ISO-8859-1");
166+
} catch (IOException var4) {
167+
var4.printStackTrace();
168+
return null;
169+
}
170+
}
171+
}
172+
173+
public static Object stringToObject(String string) {
174+
try {
175+
byte[] e = string.getBytes("ISO-8859-1");
176+
ByteArrayInputStream in = new ByteArrayInputStream(e);
177+
ObjectInputStream ois = new ObjectInputStream(in);
178+
return ois.readObject();
179+
} catch (IOException | ClassNotFoundException var4) {
180+
var4.printStackTrace();
181+
}
182+
183+
return null;
184+
}
185+
186+
public static int bytesToInt(byte[] bytes) {
187+
byte length = 4;
188+
int intValue = 0;
189+
190+
for (int i = length - 1; i >= 0; --i) {
191+
int offset = i * 8;
192+
intValue |= (bytes[i] & 255) << offset;
193+
}
194+
195+
return intValue;
196+
}
197+
198+
public static Object bytesToObject(byte[] bytes) {
199+
try {
200+
ByteArrayInputStream e = new ByteArrayInputStream(bytes);
201+
ObjectInputStream ois = new ObjectInputStream(e);
202+
return ois.readObject();
203+
} catch (IOException | ClassNotFoundException var3) {
204+
var3.printStackTrace();
205+
}
206+
207+
return null;
208+
}
209+
210+
public static byte[] objectToBytes(Object obj) throws IOException {
211+
out.reset();
212+
213+
byte[] var2;
214+
try {
215+
if (oos == null) {
216+
oos = new ObjectOutputStream(out);
217+
} else {
218+
oos.reset();
219+
}
220+
221+
oos.writeObject(obj);
222+
var2 = out.toByteArray();
223+
} finally {
224+
out.close();
225+
}
226+
227+
return var2;
228+
}
229+
230+
public static byte[] stringToBytes(String str) {
231+
StringBuffer sb = new StringBuffer(str);
232+
char c = sb.charAt(0);
233+
ByteBuffer buffer = ByteBuffer.allocate(sb.length() * 2);
234+
int index = 0;
235+
236+
while (index < sb.length()) {
237+
buffer.putChar(sb.charAt(index++));
238+
}
239+
240+
return buffer.array();
241+
}
242+
243+
public static String bytesToString(byte[] bytes) {
244+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
245+
StringBuffer sb = new StringBuffer();
246+
247+
while (buffer.hasRemaining()) {
248+
sb.append(buffer.getChar());
249+
}
250+
251+
return sb.toString();
252+
}
253+
254+
public static long combineInt2Long(int low, int high) {
255+
return (long) low & 4294967295L | (long) high << 32 & -4294967296L;
256+
}
257+
258+
public static int[] separateLong2int(Long val) {
259+
return new int[]{(int) (4294967295L & val), (int) ((-4294967296L & val) >> 32)};
260+
}
261+
262+
public static int getLongLowInt(Long val) {
263+
return val == null ? 0 : (int) (4294967295L & val);
264+
}
265+
266+
public static int getLongHighInt(Long val) {
267+
return val == null ? 0 : (int) ((-4294967296L & val) >> 32);
268+
}
269+
270+
public static boolean isIntInList(int i, int[] list) {
271+
for (int aList : list) {
272+
if (aList == i) {
273+
return true;
274+
}
275+
}
276+
277+
return false;
278+
}
279+
280+
public static int[] stringToInts(String str, String regex) {
281+
String[] arr = str.split(regex);
282+
int length = arr.length;
283+
int[] ret = new int[length];
284+
285+
for (int i = 0; i < length; ++i) {
286+
ret[i] = toInteger(arr[i]);
287+
}
288+
289+
return ret;
290+
}
291+
292+
public static String bytesToHexString(byte[] src) {
293+
StringBuilder stringBuilder = new StringBuilder("");
294+
if (src != null && src.length > 0) {
295+
for (byte aSrc : src) {
296+
int v = aSrc & 255;
297+
String hv = Integer.toHexString(v);
298+
if (hv.length() < 2) {
299+
stringBuilder.append(0);
300+
}
301+
302+
stringBuilder.append(hv);
303+
}
304+
305+
return stringBuilder.toString();
306+
} else {
307+
return null;
308+
}
309+
}
310+
311+
public static byte[] hexStringToBytes(String hexString) {
312+
if (hexString != null && !hexString.equals("")) {
313+
hexString = hexString.toUpperCase();
314+
int length = hexString.length() / 2;
315+
char[] hexChars = hexString.toCharArray();
316+
byte[] d = new byte[length];
317+
318+
for (int i = 0; i < length; ++i) {
319+
int pos = i * 2;
320+
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
321+
}
322+
323+
return d;
324+
} else {
325+
return null;
326+
}
327+
}
328+
329+
public static double strToDouble(String str) {
330+
if (str != null && !str.isEmpty()) {
331+
int len = str.length();
332+
int p = str.indexOf(37);
333+
return p == len - 1 ? Double.valueOf(str.substring(0, len - 1)) / 100.0D : (p > -1 ? 0.0D : (str.equals("true") ? 1.0D : toDouble(str)));
334+
} else {
335+
return 0.0D;
336+
}
337+
}
338+
339+
private static byte charToByte(char c) {
340+
return (byte) "0123456789ABCDEF".indexOf(c);
341+
}
342+
343+
public static String cacheString(int i) {
344+
return String.valueOf(i);
345+
}
346+
347+
public static void main(String[] args) {
348+
System.out.println(toInteger("2.147483647E9"));
349+
}
350+
351+
}

0 commit comments

Comments
 (0)