Skip to content

Instantly share code, notes, and snippets.

View HoweChen's full-sized avatar
🎯
Focusing

Yuhao.Chen (Howe) - 陈雨豪 HoweChen

🎯
Focusing
View GitHub Profile
@HoweChen
HoweChen / LinuxCommandExecutor.java
Created April 7, 2020 09:49
[LinuxCommandExecutor] #Java
/**
* @author howechen
*/
@Component
public class SystemCommandExecutor {
private static final Logger logger = LoggerFactory.getLogger(SystemCommandExecutor.class);
private ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
private PluginExecuteResultHandler resultHandler = new PluginExecuteResultHandler();
@HoweChen
HoweChen / Pojo2JsonStr.java
Last active April 3, 2020 08:54
[POJO to json String] #Java
public String toString() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String result;
try {
result = mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new Exception("Cannot transfer backend server info to json string");
}
@HoweChen
HoweChen / ZipToMap.java
Last active April 3, 2020 08:54
[两个list合并为一个map] #Java
public static <K, V> Map<K, V> zipToMap(List<K> keyList, List<V> valueList) throws Exception {
Map<K, V> result = new HashMap<>();
Iterator<K> keyIterator = keyList.iterator();
Iterator<V> valueIterator = valueList.iterator();
while (keyIterator.hasNext() && valueIterator.hasNext()) {
result.put(keyIterator.next(), valueIterator.next());
}
if (keyIterator.hasNext() || valueIterator.hasNext()) {
throw new Exception("The keyList and valueList size doesn't match.");
}
@HoweChen
HoweChen / PKCS7Padding.Java
Created April 1, 2020 06:58
[AES/CBC/PKCS7Padding加密解密] #Java
// https://blog.csdn.net/java_zjh/article/details/80915655
public final class AesUtils {
private static final String CHARSET_NAME = "UTF-8";
private static final String AES_NAME = "AES";
public static final String ALGORITHM = "AES/CBC/PKCS7Padding";
static {
Security.addProvider(new BouncyCastleProvider());
}
@HoweChen
HoweChen / ListZipToMap.java
Last active January 1, 2020 09:14
[zip two lists to map]#Java
// both are good to use, but cannot tolerate the situation when key or value inside the list is null
public static <K, V> Map<K, V> zipToMap(List<K> keys, List<V> values) {
return IntStream.range(0, keys.size()).boxed()
.collect(Collectors.toMap(keys::get, values::get));
}
public static <K, V> Map<K, V> zipToMap(List<K> keys, List<V> values) {
@HoweChen
HoweChen / IsValidEnum.java
Created December 18, 2019 08:27
[字符串是否是合法enum] #Java
// should use org.apache.commons.lang3
EnumUtils.isValidEnum(enumxxx.class,str);
@HoweChen
HoweChen / TransferKeysInMapToUppercase.java
Created December 18, 2019 07:02
[转换Map里的所有key变为大写]#Java
Map<String, List<Long>> capitalKeyToValueMap = procDefInfoDto.getPermissionToRole()
.entrySet()
.stream()
.collect(Collectors.toMap(stringListEntry -> stringListEntry.getKey().toUpperCase(), Map.Entry::getValue));
@HoweChen
HoweChen / ListOperation.java
Created December 10, 2019 07:23
[Java List 交并差集运算] #Java
public class Test {
public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
@HoweChen
HoweChen / byte2HumanReadableSize.java
Created December 8, 2019 08:03
[文件大小转换byte to kb, mb, gb, pb, eb, zb ] #Java
public static strictfp String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
long absBytes = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
if (absBytes < unit) return bytes + " B";
int exp = (int) (Math.log(absBytes) / Math.log(unit));
long th = (long) (Math.pow(unit, exp) * (unit - 0.05));
if (exp < 6 && absBytes >= th - ((th & 0xfff) == 0xd00 ? 52 : 0)) exp++;
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
if (exp > 4) {
bytes /= unit;
@HoweChen
HoweChen / queue2list.md
Created November 7, 2019 06:51
[Queue to List]#Java
List<?> list = new ArrayList<>( myQueue );

? can be be the class you want to transfer