cn.com.antcloud.api.common.SDKUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of antcloud-api-sdk Show documentation
Show all versions of antcloud-api-sdk Show documentation
Ant Fin Tech API SDK For Java
Copyright (c) 2015-present Alipay.com, https://www.alipay.com
The newest version!
/*
* Copyright (c) 2015-present Alipay.com, https://www.alipay.com
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 cn.com.antcloud.api.common;
import cn.com.antcloud.api.acapi.StringUtils;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SDKUtils {
private static Random random = new Random();
public static T checkNotNull(T arg) {
if (arg == null) {
throw new NullPointerException();
}
return arg;
}
public static T checkNotNull(T arg, String message) {
if (arg == null) {
throw new NullPointerException(message);
}
return arg;
}
public static void checkArgument(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
public static void checkArgument(boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}
public static String generateReqBizId() {
StringBuffer sb = new StringBuffer();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
dateFormat.format(new Date(), sb, new FieldPosition(0));
sb.append(String.format("%09d", random.nextInt(1000000000)));
sb.append(String.format("%06d", random.nextInt(1000000)));
return sb.toString();
}
public static String formatDate(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(date);
}
public static String formatNullableDate(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(date);
}
public static String generateReqMsgId() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 下划线转驼峰法
*
* @param line 源字符串
* @param smallCamel 大小驼峰,是否为小驼峰
* @return 转换后的字符串
*/
public static String underline2Camel(String line, boolean smallCamel) {
if (StringUtils.isEmpty(line)) {
return "";
}
StringBuilder stringBuffer = new StringBuilder();
Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String word = matcher.group();
stringBuffer
.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0))
: Character.toUpperCase(word.charAt(0)));
int index = word.lastIndexOf('_');
if (index > 0) {
stringBuffer.append(word.substring(1, index).toLowerCase());
} else {
stringBuffer.append(word.substring(1).toLowerCase());
}
}
return stringBuffer.toString();
}
/**
* 驼峰法转下划线
*
* @param line 源字符串
* @return 转换后的字符串
*/
public static String camel2Underline(String line) {
if (StringUtils.isEmpty(line)) {
return "";
}
line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
StringBuilder stringBuilder = new StringBuilder();
Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String word = matcher.group();
stringBuilder.append(word.toLowerCase());
stringBuilder.append(matcher.end() == line.length() ? "" : "_");
}
return stringBuilder.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy