IT-KIMI_SHI\SINOIT.KIMI
2018-12-07 50eb1d766c470dc6ff927199eaee934f972a8b70
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/************************************************************************
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ************************************************************************/
package common.util;
 
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
 
import java.io.IOException;
import java.io.StringWriter;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * <p>
 * 模板工具类
 *
 * @author CSJ
 */
public final class TemplateUtil {
    private TemplateUtil() {
    }
 
    /**
     * 生成随机32位UUID号
     *
     * @return UUID
     */
    public static final String genUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
    /**
     * 生成指定对象JSON 格式
     *
     * @return JSON String
     */
    public static final String genJsonStr4Obj(Object obj) {
        return genJsonStr4Obj(obj, false);
    }
 
    /**
     * 生成指定对象JSON格式是否包含NULL字段
     *
     * @param obj
     * @param removeNull
     * @return
     */
    public static final String genJsonStr4Obj(Object obj, boolean removeNull) {
        StringWriter sw = new StringWriter();
        String jsonStr = "{}";
        JsonGenerator jsonGenerator = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            if (removeNull) {
                mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            }
            jsonGenerator = mapper.getFactory().createGenerator(sw);
            jsonGenerator.writeObject(obj);
            jsonStr = sw.toString();
        } catch (Exception e) {
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e) {
                } finally {
                    sw = null;
                }
            }
            if (jsonGenerator != null) {
                try {
                    jsonGenerator.close();
                } catch (IOException e) {
                } finally {
                    jsonGenerator = null;
                }
            }
        }
        return jsonStr;
    }
 
    /**
     * 反序列化JSON 格式对象
     *
     * @param jsonString
     * @param clazz
     * @return
     */
    public static final <T> T genObjFormJson(String jsonString, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        try {
            return new ObjectMapper().readValue(jsonString, clazz);
        } catch (IOException e) {
            return null;
        }
    }
 
    /**
     * 反序列化JSON 格式对象(复杂嵌套)
     *
     * @param jsonString
     * @param reference
     * @return
     */
    public static final <T> T genObjFormJson(String jsonString, TypeReference<T> reference) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        try {
            return new ObjectMapper().readValue(jsonString, reference);
        } catch (IOException e) {
            return null;
        }
    }
 
    /**
     * 中文转换成汉语拼音
     *
     * @param chinese
     * @return
     */
    public static final String getPinYin(String chinese) {
        if (StringUtils.isEmpty(chinese)) {
            throw new RuntimeException("请输入要转换中文,不能为空");
        }
 
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 音标支持HanyuPinyinToneType.WITH_TONE_MARK
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
 
        char[] input = chinese.trim().toCharArray();
        StringBuffer output = new StringBuffer("");
 
        try {
            for (int i = 0; i < input.length; i++) {
                if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    output.append(temp[0]);
                    output.append(" ");
                } else {
                    output.append(Character.toString(input[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return output.toString();
    }
 
    /**
     * 中文字符转换成首字母拼音
     *
     * @param chinese
     * @return
     */
    public static final String getPinYin4FirstSpell(String chinese, String split) {
        if (StringUtils.isEmpty(chinese)) {
            throw new RuntimeException("请输入要转换中文,不能为空");
        }
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 音标支持HanyuPinyinToneType.WITH_TONE_MARK
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
 
        char[] input = chinese.trim().toCharArray();
        StringBuffer output = new StringBuffer("");
 
        try {
            for (int i = 0; i < input.length; i++) {
                if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    output.append(temp[0].charAt(0));
                    if (StringUtils.isNotEmpty(split)) {
                        output.append(split);
                    }
                } else {
                    output.append(Character.toString(input[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return output.toString();
    }
 
    public static final String getPinYin4FirstSpell(String chinese) {
        return getPinYin4FirstSpell(chinese, "");
    }
 
    /**
     * 检测文本是否包含中文
     *
     * @param chinese
     * @return
     */
    public static final boolean isChinese(String chinese) {
        if (StringUtils.isEmpty(chinese)) {
            return false;
        }
        boolean res = false;
        char[] input = chinese.trim().toCharArray();
        for (int i = 0; i < input.length; i++) {
            if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) {
                res = true;
                break;
            }
        }
        return res;
    }
 
    /**
     * 检测文本是否只含有英文字符
     *
     * @param inputStr
     * @return
     */
    public static final boolean isLetter(String inputStr) {
        if (StringUtils.isEmpty(inputStr)) {
            return false;
        }
        String regx_letter = "[a-zA-Z]+";
        Pattern p = Pattern.compile(regx_letter);
        Matcher m = p.matcher(inputStr);
        return m.matches();
    }
 
    /**
     * 生成encache spring key 可变对象参数方法
     *
     * @param keys
     * @return
     */
    public static final String genCacheKey4Array(String... keys) {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        for (String key : keys) {
            builder.append(key);
        }
        builder.append("]");
        return builder.toString();
    }
 
    /**
     * 将下划线名称风格替换为驼峰风格
     *
     * @param underlineStr
     * @return
     */
    public static String underlineToCamelCase(String underlineStr) {
        Matcher matcher = Pattern.compile("[a-z]_[a-z]").matcher(underlineStr);
        StringBuilder builder = new StringBuilder(underlineStr);
        for (int i = 0; matcher.find(); i++) {
            String group = matcher.group();
            String upperCase = group.replaceAll("_[a-z]", group.substring(2).toUpperCase());
            builder.replace(matcher.start() - i, matcher.end() - i, upperCase);
        }
        if (Character.isUpperCase(builder.charAt(0))) {
            builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
        }
        return builder.toString();
    }
}