Skip to content

Instantly share code, notes, and snippets.

// A Command Pattern example
// Oringinal example from https://dzone.com/articles/design-patterns-command and I modify it using java8
public class Client {
public static void main(String[] args) {
// Receiver
Light light = new Light();
// Invoker
RemoteControl remoteControl = new RemoteControl();
package interview;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import static java.util.Comparator.comparing;
@zhugw
zhugw / UnsafeTest.java
Created May 8, 2017 13:36 — forked from raphw/UnsafeTest.java
A demonstration of sun.misc.Unsafe
import org.junit.Before;
import org.junit.Test;
import sun.misc.Unsafe;
import sun.reflect.ReflectionFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static org.junit.Assert.assertEquals;
@zhugw
zhugw / gist:c470b5d77a617bbe0e34944ea0b9933a
Last active May 7, 2017 10:38
面试题:随机数生成器
package interview;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import static java.util.Optional.ofNullable;
/**
* <pre>
@zhugw
zhugw / gist:1d228a91bed1ab2cfe5a9a7f15c03014
Created April 13, 2017 07:14
Java8_grouping_by_mapping_and_join
Map<String,String> areaCode2AddrMap = new HashMap<>();
for(AreaCode areaCode:areaCodeList){
if(areaCode2AddrMap.get(areaCode.getCode())!=null){
String value = areaCode2AddrMap.get(areaCode.getCode())+","+areaCode.getAddr();
areaCode2AddrMap.put(areaCode.getCode(),value);
}else{
areaCode2AddrMap.put(areaCode.getCode(),areaCode.getAddr());
}
}
@zhugw
zhugw / gist:f38981bcb37f764b0b7087f8b238404f
Last active April 13, 2017 13:50
Java8重写拼接文件除去文件头尾的各行
public static String loadPemKey(String fileName) throws Exception {
// TODO: 可以考虑用Java8重写
BufferedReader br = new BufferedReader(new FileReader(PATH + fileName));
String s = br.readLine();
String str = "";
s = br.readLine();
while (s.charAt(0) != '-') {
//str += s + "\r";
str+=s;
s = br.readLine();
@zhugw
zhugw / MemoryOccupyTest
Created April 12, 2017 01:40
MemoryOccupyTest
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by zhuguowei on 4/12/17.
package com.tn.api.service;
import com.tn.api.dao.TestDao;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@zhugw
zhugw / WrappedBeanCopier.java
Last active September 11, 2016 12:18
对BeanCopier做了封装便于日常开发使用
package com.zhugw.common.util;
import com.esotericsoftware.reflectasm.ConstructorAccess;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@zhugw
zhugw / gist:1cdadc73358088a0989b5c3f36a92cac
Created August 14, 2016 13:44
验证Spring嵌套事务
package com.helijia.lottery.service;
import com.helijia.common.api.model.ApiException;
import com.helijia.lottery.dao.mapper.TestMapper;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;