java公用方法_java部分util公用方法

获取Ip地址

/**

* 获取Ip地址

* @param request

* @return

*/

public String getIpAddr(HttpServletRequest request) {

String ip = request.getHeader("x-forwarded-for");

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){

ip = request.getHeader("Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){

ip = request.getHeader("WL-Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){

ip = request.getRemoteAddr();

}

return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;

}

懒加载模式

public class Lazy {

private transient Supplier supplier;

private volatile T value;

private Lazy(){}

private Lazy(Supplier supplier) {

this.supplier = Objects.requireNonNull(supplier);

}

public T get() {

if (value == null) {

synchronized (this) {

if (value == null) {

value = Objects.requireNonNull(supplier.get());

supplier = null;

}

}

}

return value;

}

public Lazy map(Function mapper) {

return new Lazy<>(() -> mapper.apply(this.get()));

}

public Lazy flatMap(Function> mapper) {

return new Lazy<>(() -> mapper.apply(this.get()).get());

}

public Lazy> filter(Predicate predicate) {

return new Lazy<>(() -> Optional.of(get()).filter(predicate));

}

public static Lazy of(Supplier supplier) {

return new Lazy<>(supplier);

}

}

try-catch 通用模式

public class CatchUtil {

public static R tryDo(T t, Function func) {

try {

return func.apply(t);

} catch (Exception e) {

e.printStackTrace(); // for log

throw new RuntimeException(e.getCause());

}

}

public static void tryDo(T t, Consumer func) {

try {

func.accept(t);

} catch (Exception e) {

e.printStackTrace(); // for log

throw new RuntimeException(e.getCause());

}

}

}

多线程执行

public class ExecutorUtil {

private ExecutorUtil() {}

private static final int CORE_CPUS = Runtime.getRuntime().availableProcessors();

private static final int TASK_SIZE = 1000;

// a throol pool may be managed by spring

private static ExecutorService executor = new ThreadPoolExecutor(

CORE_CPUS, 10, 60L, TimeUnit.SECONDS,

new ArrayBlockingQueue<>(60));

/**

* 根据指定的列表关键数据及列表数据处理器,并发地处理并返回处理后的列表数据集合

* @param allKeys 列表关键数据

* @param handleBizDataFunc 列表数据处理器

* @param 待处理的数据参数类型

* @param 待返回的数据结果类型

* @return 处理后的列表数据集合

*

* NOTE: 类似实现了 stream.par.map 的功能,不带延迟计算

*/

public static List exec(List allKeys, Function, List> handleBizDataFunc) {

List parts = TaskUtil.divide(allKeys.size(), TASK_SIZE);

//获取

CompletionService>

completionService = new ExecutorCompletionService<>(executor);

ForeachUtil.foreachDone(parts, (part) -> {

final List tmpRowkeyList = TaskUtil.getSubList(allKeys, part);

completionService.submit(

// lambda replace inner class

() -> handleBizDataFunc.apply(tmpRowkeyList));

});

// foreach code refining

List result = ForeachUtil.foreachAddWithReturn(parts.size(), (ind) -> get(ind, completionService));

return result;

}

/**

* 根据指定的列表关键数据及列表数据处理器,并发地处理

* @param allKeys 列表关键数据

* @param handleBizDataFunc 列表数据处理器

* @param 待处理的数据参数类型

*

* NOTE: foreachDone 的并发版

*/

public static void exec(List allKeys, Consumer> handleBizDataFunc) {

List parts = TaskUtil.divide(allKeys.size(), TASK_SIZE);

ForeachUtil.foreachDone(parts, (part) -> {

final List tmpRowkeyList = TaskUtil.getSubList(allKeys, part);

// lambda replace inner class

executor.execute(

() -> handleBizDataFunc.accept(tmpRowkeyList));

});

}

public static List get(int ind, CompletionService> completionService) {

// lambda cannot handler checked exception

try {

return completionService.take().get();

} catch (Exception e) {

e.printStackTrace(); // for log

throw new RuntimeException(e.getCause());

}

}

}

//循环通用

public class ForeachUtil {

public static List foreachAddWithReturn(int num, Function> getFunc) {

List result = new ArrayList();

for (int i=0; i< num; i++) {

result.addAll(CatchUtil.tryDo(i, getFunc));

}

return result;

}

public static Lazy> foreachAddReturn(int num, Function getFunc){

List result = new ArrayList<>();

IntStream.of(num).forEach( i->

result.add(CatchUtil.tryDo(i,getFunc))

);

return Lazy.of(()-> result);

}

public static void foreachDone(List data, Consumer doFunc) {

for (T part: data) {

CatchUtil.tryDo(part, doFunc);

}

}

}

List stream转换List

public class StreamUtil {

public static List map(List data, Function mapFunc) {

// stream replace foreach

return data.stream().map(mapFunc).collect(Collectors.toList());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值