匿名类替换为lambda表达式

This commit is contained in:
liuhuan 2020-01-03 09:46:03 +08:00
parent d77b8d43bf
commit decd6a1460
11 changed files with 76 additions and 151 deletions

View File

@ -1,12 +1,12 @@
package cn.hutool.cache.impl;
import cn.hutool.cache.GlobalPruneTimer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import cn.hutool.cache.GlobalPruneTimer;
/**
* 定时缓存<br>
* 此缓存没有容量限制对象只有在过期后才会被移除
@ -72,12 +72,7 @@ public class TimedCache<K, V> extends AbstractCache<K, V> {
* @param delay 间隔时长单位毫秒
*/
public void schedulePrune(long delay) {
this.pruneJobFuture = GlobalPruneTimer.INSTANCE.schedule(new Runnable() {
@Override
public void run() {
prune();
}
}, delay);
this.pruneJobFuture = GlobalPruneTimer.INSTANCE.schedule(this::prune, delay);
}
/**

View File

@ -1,19 +1,14 @@
package cn.hutool.core.collection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import java.util.*;
import java.util.Map.Entry;
/**
* {@link Iterable} {@link Iterator} 相关工具类
*
@ -511,12 +506,7 @@ public class IterUtil {
* @return {@link Iterable}
*/
public static <E> Iterable<E> asIterable(final Iterator<E> iter) {
return new Iterable<E>() {
@Override
public Iterator<E> iterator() {
return iter;
}
};
return () -> iter;
}
/**

View File

@ -1,9 +1,9 @@
package cn.hutool.core.date;
import java.util.Date;
import cn.hutool.core.lang.Range;
import java.util.Date;
/**
* 日期范围
*
@ -47,16 +47,12 @@ public class DateRange extends Range<DateTime> {
* @param isIncludeEnd 是否包含结束的时间
*/
public DateRange(Date start, Date end, final DateField unit, final int step, boolean isIncludeStart, boolean isIncludeEnd) {
super(DateUtil.date(start), DateUtil.date(end), new Steper<DateTime>() {
@Override
public DateTime step(DateTime current, DateTime end, int index) {
DateTime dt = current.offsetNew(unit, step);
if (dt.isAfter(end)) {
return null;
}
return current.offsetNew(unit, step);
super(DateUtil.date(start), DateUtil.date(end), (current, end1, index) -> {
DateTime dt = current.offsetNew(unit, step);
if (dt.isAfter(end1)) {
return null;
}
return current.offsetNew(unit, step);
}, isIncludeStart, isIncludeEnd);
}

View File

@ -3,7 +3,6 @@ package cn.hutool.core.date;
import java.sql.Timestamp;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
@ -37,20 +36,12 @@ public class SystemClock {
* 开启计时器线程
*/
private void scheduleClockUpdating() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory(){
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, "System Clock");
thread.setDaemon(true);
return thread;
}
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "System Clock");
thread.setDaemon(true);
return thread;
});
scheduler.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
now = System.currentTimeMillis();
}
}, period, period, TimeUnit.MILLISECONDS);
scheduler.scheduleAtFixedRate(() -> now = System.currentTimeMillis(), period, period, TimeUnit.MILLISECONDS);
}
/**

View File

@ -1,16 +1,16 @@
package cn.hutool.core.io.watch.watchers;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.thread.ThreadUtil;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchService;
import java.util.Set;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.thread.ThreadUtil;
/**
* 延迟观察者<br>
* 使用此观察者通过定义一定的延迟时间解决{@link WatchService}多个modify的问题<br>
@ -95,13 +95,10 @@ public class DelayWatcher implements Watcher {
* @param currentPath 事件发生的当前Path路径
*/
private void startHandleModifyThread(final WatchEvent<?> event, final Path currentPath) {
ThreadUtil.execute(new Runnable(){
@Override
public void run() {
ThreadUtil.sleep(delay);
eventSet.remove(Paths.get(currentPath.toString(), event.context().toString()));
watcher.onModify(event, currentPath);
}
ThreadUtil.execute(() -> {
ThreadUtil.sleep(delay);
eventSet.remove(Paths.get(currentPath.toString(), event.context().toString()));
watcher.onModify(event, currentPath);
});
}
//---------------------------------------------------------------------------------------------------------- Private method end

View File

@ -1,12 +1,12 @@
package cn.hutool.core.lang;
import cn.hutool.core.util.HashUtil;
import java.io.Serializable;
import java.util.Collection;
import java.util.SortedMap;
import java.util.TreeMap;
import cn.hutool.core.util.HashUtil;
/**
* 一致性Hash算法
* 算法详解http://blog.csdn.net/sparkliang/article/details/5279393
@ -32,13 +32,9 @@ public class ConsistentHash<T> implements Serializable{
*/
public ConsistentHash(int numberOfReplicas, Collection<T> nodes) {
this.numberOfReplicas = numberOfReplicas;
this.hashFunc = new HashFunc() {
@Override
public Integer hash(Object key) {
//默认使用FNV1hash算法
return HashUtil.fnvHash(key.toString());
}
this.hashFunc = key -> {
//默认使用FNV1hash算法
return HashUtil.fnvHash(key.toString());
};
//初始化节点
for (T node : nodes) {

View File

@ -1,19 +1,18 @@
package cn.hutool.core.lang;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.URLUtil;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
/**
* 外部Jar的类加载器
*
@ -143,12 +142,7 @@ public class JarClassLoader extends URLClassLoader {
* @return jar文件列表
*/
private static List<File> loopJar(File file) {
return FileUtil.loopFiles(file, new FileFilter() {
@Override
public boolean accept(File file) {
return isJarFile(file);
}
});
return FileUtil.loopFiles(file, JarClassLoader::isJarFile);
}
/**

View File

@ -1,9 +1,5 @@
package cn.hutool.core.lang;
import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.NumberUtil;
@ -11,6 +7,10 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字段验证器
*
@ -465,12 +465,7 @@ public class Validator {
* @since 3.3.0
*/
public static boolean isLetter(CharSequence value) {
return StrUtil.isAllCharMatch(value, new cn.hutool.core.lang.Matcher<Character>() {
@Override
public boolean match(Character t) {
return Character.isLetter(t);
}
});
return StrUtil.isAllCharMatch(value, Character::isLetter);
}
/**
@ -498,12 +493,7 @@ public class Validator {
* @since 3.3.0
*/
public static boolean isUpperCase(CharSequence value) {
return StrUtil.isAllCharMatch(value, new cn.hutool.core.lang.Matcher<Character>() {
@Override
public boolean match(Character t) {
return Character.isUpperCase(t);
}
});
return StrUtil.isAllCharMatch(value, Character::isUpperCase);
}
/**
@ -531,12 +521,7 @@ public class Validator {
* @since 3.3.0
*/
public static boolean isLowerCase(CharSequence value) {
return StrUtil.isAllCharMatch(value, new cn.hutool.core.lang.Matcher<Character>() {
@Override
public boolean match(Character t) {
return Character.isLowerCase(t);
}
});
return StrUtil.isAllCharMatch(value, Character::isLowerCase);
}
/**

View File

@ -1,13 +1,13 @@
package cn.hutool.core.thread;
import cn.hutool.core.builder.Builder;
import cn.hutool.core.util.StrUtil;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import cn.hutool.core.builder.Builder;
import cn.hutool.core.util.StrUtil;
/**
* ThreadFactory创建器<br>
* 参考Guava的ThreadFactoryBuilder
@ -125,24 +125,21 @@ public class ThreadFactoryBuilder implements Builder<ThreadFactory>{
final Integer priority = builder.priority;
final UncaughtExceptionHandler handler = builder.uncaughtExceptionHandler;
final AtomicLong count = (null == namePrefix) ? null : new AtomicLong();
return new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
final Thread thread = backingThreadFactory.newThread(r);
if (null != namePrefix) {
thread.setName(namePrefix + count.getAndIncrement());
}
if (null != daemon) {
thread.setDaemon(daemon);
}
if (null != priority) {
thread.setPriority(priority);
}
if (null != handler) {
thread.setUncaughtExceptionHandler(handler);
}
return thread;
return r -> {
final Thread thread = backingThreadFactory.newThread(r);
if (null != namePrefix) {
thread.setName(namePrefix + count.getAndIncrement());
}
if (null != daemon) {
thread.setDaemon(daemon);
}
if (null != priority) {
thread.setPriority(priority);
}
if (null != handler) {
thread.setUncaughtExceptionHandler(handler);
}
return thread;
};
}
}

View File

@ -1,15 +1,5 @@
package cn.hutool.core.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException;
@ -18,6 +8,10 @@ import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.lang.func.Func1;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则相关工具类<br>
* 常用正则请见 {@link Validator}
@ -174,12 +168,7 @@ public class ReUtil {
}
//提取模板中的编号
final TreeSet<Integer> varNums = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return ObjectUtil.compare(o2, o1);
}
});
final TreeSet<Integer> varNums = new TreeSet<>((o1, o2) -> ObjectUtil.compare(o2, o1));
final Matcher matcherForTemplate = PatternPool.GROUP_VAR.matcher(template);
while (matcherForTemplate.find()) {
varNums.add(Integer.parseInt(matcherForTemplate.group(1)));

View File

@ -1,13 +1,12 @@
package cn.hutool.dfa;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import java.util.Collection;
import java.util.List;
/**
* 敏感词工具类
* @author Looly
@ -33,13 +32,9 @@ public final class SensitiveUtil {
*/
public static void init(final Collection<String> sensitiveWords, boolean isAsync){
if(isAsync){
ThreadUtil.execAsync(new Callable<Boolean>(){
@Override
public Boolean call() throws Exception {
init(sensitiveWords);
return true;
}
ThreadUtil.execAsync(() -> {
init(sensitiveWords);
return true;
});
}else{
init(sensitiveWords);