|
21 | 21 | package com.dtstack.flink.sql.util;
|
22 | 22 |
|
23 | 23 | import java.math.BigDecimal;
|
| 24 | +import java.math.BigInteger; |
| 25 | +import java.sql.Date; |
| 26 | +import java.sql.Timestamp; |
| 27 | +import java.text.ParseException; |
| 28 | +import java.text.SimpleDateFormat; |
24 | 29 |
|
25 | 30 | /**
|
| 31 | + * Convert val to specified numeric type |
26 | 32 | * Date: 2017/4/21
|
27 | 33 | * Company: www.dtstack.com
|
28 | 34 | * @author xuchao
|
29 | 35 | */
|
30 | 36 |
|
31 | 37 | public class MathUtil {
|
32 | 38 |
|
33 |
| - public static Long getLongVal(Object obj){ |
34 |
| - if(obj == null){ |
| 39 | + public static Long getLongVal(Object obj) { |
| 40 | + if (obj == null) { |
35 | 41 | return null;
|
36 | 42 | }
|
37 | 43 |
|
38 |
| - if(obj instanceof String){ |
| 44 | + if (obj instanceof String) { |
39 | 45 | return Long.valueOf((String) obj);
|
40 |
| - }else if(obj instanceof Long){ |
| 46 | + } else if (obj instanceof Long) { |
41 | 47 | return (Long) obj;
|
42 |
| - }else if(obj instanceof Integer){ |
| 48 | + } else if (obj instanceof Integer) { |
43 | 49 | return Long.valueOf(obj.toString());
|
| 50 | + } else if (obj instanceof BigDecimal) { |
| 51 | + return ((BigDecimal) obj).longValue(); |
| 52 | + } else if (obj instanceof BigInteger) { |
| 53 | + return ((BigInteger) obj).longValue(); |
44 | 54 | }
|
| 55 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Long."); |
| 56 | + } |
45 | 57 |
|
46 |
| - throw new RuntimeException("not support type of " + obj.getClass() + " convert to Long." ); |
| 58 | + public static Long getLongVal(Object obj, long defaultVal) { |
| 59 | + if (obj == null) { |
| 60 | + return defaultVal; |
| 61 | + } |
| 62 | + |
| 63 | + return getLongVal(obj); |
47 | 64 | }
|
48 | 65 |
|
49 |
| - public static Integer getIntegerVal(Object obj){ |
50 |
| - if(obj == null){ |
| 66 | + public static Integer getIntegerVal(Object obj) { |
| 67 | + if (obj == null) { |
51 | 68 | return null;
|
52 | 69 | }
|
53 | 70 |
|
54 |
| - if(obj instanceof String){ |
| 71 | + if (obj instanceof String) { |
55 | 72 | return Integer.valueOf((String) obj);
|
56 |
| - } else if (obj instanceof Integer){ |
| 73 | + } else if (obj instanceof Integer) { |
57 | 74 | return (Integer) obj;
|
58 |
| - } else if (obj instanceof Long){ |
59 |
| - return ((Long)obj).intValue(); |
60 |
| - } else if(obj instanceof Double){ |
61 |
| - return ((Double)obj).intValue(); |
62 |
| - } else if(obj instanceof BigDecimal){ |
63 |
| - return ((BigDecimal)obj).intValue(); |
| 75 | + } else if (obj instanceof Long) { |
| 76 | + return ((Long) obj).intValue(); |
| 77 | + } else if (obj instanceof Double) { |
| 78 | + return ((Double) obj).intValue(); |
| 79 | + } else if (obj instanceof BigDecimal) { |
| 80 | + return ((BigDecimal) obj).intValue(); |
| 81 | + } else if (obj instanceof BigInteger) { |
| 82 | + return ((BigInteger) obj).intValue(); |
64 | 83 | }
|
65 | 84 |
|
66 |
| - throw new RuntimeException("not support type of " + obj.getClass() + " convert to Integer." ); |
| 85 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Integer."); |
67 | 86 | }
|
68 | 87 |
|
69 |
| - public static Boolean getBoolean(Object obj, boolean defaultVal){ |
70 |
| - if(obj == null){ |
| 88 | + public static Integer getIntegerVal(Object obj, int defaultVal) { |
| 89 | + if (obj == null) { |
71 | 90 | return defaultVal;
|
72 | 91 | }
|
73 | 92 |
|
74 |
| - return getBoolean(obj); |
| 93 | + return getIntegerVal(obj); |
| 94 | + } |
| 95 | + |
| 96 | + public static Float getFloatVal(Object obj) { |
| 97 | + if (obj == null) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + if (obj instanceof String) { |
| 102 | + return Float.valueOf((String) obj); |
| 103 | + } else if (obj instanceof Float) { |
| 104 | + return (Float) obj; |
| 105 | + } else if (obj instanceof BigDecimal) { |
| 106 | + return ((BigDecimal) obj).floatValue(); |
| 107 | + } |
| 108 | + |
| 109 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Float."); |
| 110 | + } |
| 111 | + |
| 112 | + public static Float getFloatVal(Object obj, float defaultVal) { |
| 113 | + if (obj == null) { |
| 114 | + return defaultVal; |
| 115 | + } |
| 116 | + |
| 117 | + return getFloatVal(obj); |
75 | 118 | }
|
76 | 119 |
|
77 |
| - public static Boolean getBoolean(Object obj){ |
78 |
| - if(obj == null){ |
| 120 | + public static Double getDoubleVal(Object obj) { |
| 121 | + if (obj == null) { |
79 | 122 | return null;
|
80 | 123 | }
|
81 | 124 |
|
82 |
| - if(obj instanceof String){ |
| 125 | + if (obj instanceof String) { |
| 126 | + return Double.valueOf((String) obj); |
| 127 | + } else if (obj instanceof Float) { |
| 128 | + return ((Float) obj).doubleValue(); |
| 129 | + } else if (obj instanceof Double){ |
| 130 | + return (Double) obj; |
| 131 | + }else if (obj instanceof BigDecimal) { |
| 132 | + return ((BigDecimal) obj).doubleValue(); |
| 133 | + }else if (obj instanceof Integer){ |
| 134 | + return ((Integer)obj).doubleValue(); |
| 135 | + } |
| 136 | + |
| 137 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Double."); |
| 138 | + } |
| 139 | + |
| 140 | + public static Double getDoubleVal(Object obj, double defaultVal) { |
| 141 | + if (obj == null) { |
| 142 | + return defaultVal; |
| 143 | + } |
| 144 | + |
| 145 | + return getDoubleVal(obj); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + public static Boolean getBoolean(Object obj) { |
| 150 | + if (obj == null) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + if (obj instanceof String) { |
83 | 155 | return Boolean.valueOf((String) obj);
|
84 |
| - }else if(obj instanceof Boolean){ |
| 156 | + } else if (obj instanceof Boolean) { |
85 | 157 | return (Boolean) obj;
|
86 | 158 | }
|
87 | 159 |
|
88 |
| - throw new RuntimeException("not support type of " + obj.getClass() + " convert to Boolean." ); |
| 160 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Boolean."); |
| 161 | + } |
| 162 | + |
| 163 | + public static Boolean getBoolean(Object obj, boolean defaultVal) { |
| 164 | + if (obj == null) { |
| 165 | + return defaultVal; |
| 166 | + } |
| 167 | + |
| 168 | + return getBoolean(obj); |
89 | 169 | }
|
90 | 170 |
|
91 |
| - public static String getString(Object obj){ |
92 |
| - if(obj == null){ |
| 171 | + public static String getString(Object obj) { |
| 172 | + if (obj == null) { |
93 | 173 | return null;
|
94 | 174 | }
|
95 | 175 |
|
96 |
| - if(obj instanceof String){ |
| 176 | + if (obj instanceof String) { |
97 | 177 | return (String) obj;
|
98 |
| - }else { |
99 |
| - return obj.toString(); |
100 | 178 | }
|
| 179 | + |
| 180 | + return obj.toString(); |
| 181 | + } |
| 182 | + |
| 183 | + public static Byte getByte(Object obj) { |
| 184 | + if (obj == null) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + if (obj instanceof String) { |
| 189 | + return Byte.valueOf((String) obj); |
| 190 | + } else if (obj instanceof Byte) { |
| 191 | + return (Byte) obj; |
| 192 | + } |
| 193 | + |
| 194 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Byte."); |
| 195 | + } |
| 196 | + |
| 197 | + public static Short getShort(Object obj) { |
| 198 | + if (obj == null) { |
| 199 | + return null; |
| 200 | + } |
| 201 | + |
| 202 | + if (obj instanceof String) { |
| 203 | + return Short.valueOf((String) obj); |
| 204 | + } else if (obj instanceof Short) { |
| 205 | + return (Short) obj; |
| 206 | + } |
| 207 | + |
| 208 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Short."); |
| 209 | + } |
| 210 | + |
| 211 | + public static BigDecimal getBigDecimal(Object obj) { |
| 212 | + if (obj == null) { |
| 213 | + return null; |
| 214 | + } |
| 215 | + if (obj instanceof String) { |
| 216 | + return new BigDecimal((String) obj); |
| 217 | + } else if (obj instanceof BigDecimal) { |
| 218 | + return (BigDecimal) obj; |
| 219 | + } else if (obj instanceof BigInteger) { |
| 220 | + return new BigDecimal((BigInteger) obj); |
| 221 | + } else if (obj instanceof Number) { |
| 222 | + return new BigDecimal(((Number) obj).doubleValue()); |
| 223 | + } |
| 224 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to BigDecimal."); |
| 225 | + } |
| 226 | + |
| 227 | + public static Date getDate(Object obj) { |
| 228 | + if (obj == null) { |
| 229 | + return null; |
| 230 | + } |
| 231 | + if (obj instanceof String) { |
| 232 | + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 233 | + try { |
| 234 | + return new Date(format.parse((String) obj).getTime()); |
| 235 | + } catch (ParseException e) { |
| 236 | + throw new RuntimeException("String convert to Date fail."); |
| 237 | + } |
| 238 | + } else if (obj instanceof Timestamp) { |
| 239 | + return new Date(((Timestamp) obj).getTime()); |
| 240 | + } else if (obj instanceof Date) { |
| 241 | + return (Date) obj; |
| 242 | + } |
| 243 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date."); |
| 244 | + } |
| 245 | + |
| 246 | + public static Timestamp getTimestamp(Object obj) { |
| 247 | + if (obj == null) { |
| 248 | + return null; |
| 249 | + } |
| 250 | + if (obj instanceof Timestamp) { |
| 251 | + return (Timestamp) obj; |
| 252 | + } else if (obj instanceof Date) { |
| 253 | + return new Timestamp(((Date) obj).getTime()); |
| 254 | + } else if (obj instanceof String) { |
| 255 | + return new Timestamp(getDate(obj).getTime()); |
| 256 | + } |
| 257 | + throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date."); |
101 | 258 | }
|
102 | 259 | }
|
0 commit comments