com.github.uuidcode.util.CoreUtil Maven / Gradle / Ivy
package com.github.uuidcode.util;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import com.github.uuidcode.adapter.DateTypeAdapter;
import com.github.uuidcode.adapter.LongTypeAdapter;
import com.github.uuidcode.adapter.StringTypeAdapter;
import com.google.common.base.CaseFormat;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import static java.util.Optional.ofNullable;
public class CoreUtil {
public static final String SPACE4 = " ";
public static final String SPACE = " ";
public static final String EMPTY = "";
public static final String LINE_FEED = "\n";
public static final String COMMA = ",";
public static final String HYPHEN = "_";
public static final String VERTICAL_BAR = "|";
public static final String SLASH = "/";
public static final String BACK_SLASH = "\\";
public static final String QUESTION_MARK = "?";
public static final String DOT = ".";
public static final String AMPERSAND = "&";
public static final String EQUAL = "=";
public static final String SHARP = "#";
public static final String UNDERSCORE = "_";
public static final String SEMICOLON = ";";
public static final String LEFT_BRACE = "{";
public static final String RIGHT_BRACE = "}";
public static final String LEFT_PARENTHESIS = "(";
public static final String RIGHT_PARENTHESIS = ")";
public static final String LEFT_ANGLE_BRACKET = "<";
public static final String RIGHT_ANGLE_BRACKET = ">";
public static final String UTF8 = "UTF-8";
protected static Logger logger = LoggerFactory.getLogger(CoreUtil.class);
private static DateTimeParser[] dateTimeParsers = {
DateTimeFormat.forPattern("yyyyMMdd").getParser(),
DateTimeFormat.forPattern("yyyyMMddHH").getParser(),
DateTimeFormat.forPattern("yyyyMMddHHmm").getParser(),
DateTimeFormat.forPattern("yyyyMMddHHmmss").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH:mm").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").getParser(),
DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
DateTimeFormat.forPattern("MM/dd/yyyy HH").getParser(),
DateTimeFormat.forPattern("MM/dd/yyyy HH:mm").getParser(),
DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss").getParser(),
DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss").withLocale(Locale.US).getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH:mm").getParser(),
DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:ss").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss,SSS").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss,SSSZ").getParser()
};
private static DateTimeFormatter dateTimeFormatter =
new DateTimeFormatterBuilder()
.append(null, dateTimeParsers)
.toFormatter();
private static Gson gson = getGsonBuilder().create();
public static GsonBuilder getGsonBuilder() {
return new GsonBuilder()
.registerTypeAdapter(Long.class, new LongTypeAdapter())
.registerTypeAdapter(String.class, new StringTypeAdapter())
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.addSerializationExclusionStrategy(new CoreExclusionStrategy())
.disableHtmlEscaping()
.setPrettyPrinting();
}
public static String createUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
public static String formatDatetime(Date date, String pattern) {
if (date == null) {
return null;
}
FastDateFormat format = FastDateFormat.getInstance(pattern);
return format.format(date);
}
public static String formatDatetime(Date date) {
return formatDatetime(date, "yyyy-MM-dd HH:mm:ss");
}
public static String formatDate(Date date) {
if (date == null) {
return null;
}
FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
return format.format(date);
}
public static Date parseDateTime(String text) {
if (text == null) {
return null;
}
try {
return dateTimeFormatter.parseDateTime(text).toDate();
} catch (Throwable t) {
}
try {
long time = new Date().parse(text);
return new Date(time);
} catch (Throwable t) {
}
return null;
}
public static boolean isEmpty(String value) {
return value == null || value.trim().length() == 0;
}
public static boolean isNotEmpty(String value) {
return !isEmpty(value);
}
public static String wrapWithDoubleQuote(String text) {
return wrap(text, "\"");
}
public static String wrapWithBrace(String text) {
return wrap(text, LEFT_BRACE, RIGHT_BRACE);
}
public static String wrapWithAngleBracket(String text) {
return wrap(text, LEFT_ANGLE_BRACKET, RIGHT_ANGLE_BRACKET);
}
public static String wrap(String text, String left, String right) {
return left + text + right;
}
public static String wrap(String text, String left) {
return wrap(text, left, left);
}
public static String toFirstCharUpperCase(String name) {
if (isEmpty(name)) {
return EMPTY;
}
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
public static String toFirstCharLowerCase(String name) {
if (isEmpty(name)) {
return EMPTY;
}
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
public static List splitListWithUnderscore(String text) {
return split(text, UNDERSCORE);
}
public static List splitListWithDot(String text) {
return split(text, DOT);
}
public static List split(String text, String delimiter) {
if (text == null) {
return new ArrayList<>();
}
return Arrays.stream(text.split(delimiter))
.collect(Collectors.toList());
}
public static String generate(String text, int size) {
return IntStream.range(0, size)
.mapToObj(i -> text)
.collect(Collectors.joining());
}
public static String joining(List list) {
return CoreUtil.joining(list, COMMA);
}
public static String joiningWithCommaAndSpace(List list) {
return CoreUtil.joining(list, CoreUtil.commaAndSpace());
}
public static String joining(List list, String delimiter) {
if (list == null) {
return EMPTY;
}
return list.stream().collect(Collectors.joining(delimiter));
}
public static > List toList(Class enumClass) {
return new ArrayList(EnumSet.allOf(enumClass));
}
public static String getCanonicalPath(File file) {
try {
return file.getCanonicalPath();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public static void setContent(File file, String data) {
try {
FileUtils.writeStringToFile(file, data, Charset.defaultCharset());
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(">>> error setContent", e);
}
}
}
public static String getContent(File file) {
try {
return FileUtils.readFileToString(file, Charset.defaultCharset());
} catch (Exception e) {
logger.error("error", e);
}
return EMPTY;
}
public static String getContentFromResource(String name) {
String fileName = CoreUtil.class.getClassLoader().getResource(name).getFile();
File file = new File(fileName);
return getContent(file);
}
public static T fromJson(String json, Class clazz) {
return gson.fromJson(json, clazz);
}
public static Map fromJsonToMap(String json) {
return gson.fromJson(json, Map.class);
}
public static List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy