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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,5 @@
package cn.hutool.core.lang; 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.date.DateUtil;
import cn.hutool.core.exceptions.ValidateException; import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.NumberUtil; 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.ReUtil;
import cn.hutool.core.util.StrUtil; 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 * @since 3.3.0
*/ */
public static boolean isLetter(CharSequence value) { public static boolean isLetter(CharSequence value) {
return StrUtil.isAllCharMatch(value, new cn.hutool.core.lang.Matcher<Character>() { return StrUtil.isAllCharMatch(value, Character::isLetter);
@Override
public boolean match(Character t) {
return Character.isLetter(t);
}
});
} }
/** /**
@ -498,12 +493,7 @@ public class Validator {
* @since 3.3.0 * @since 3.3.0
*/ */
public static boolean isUpperCase(CharSequence value) { public static boolean isUpperCase(CharSequence value) {
return StrUtil.isAllCharMatch(value, new cn.hutool.core.lang.Matcher<Character>() { return StrUtil.isAllCharMatch(value, Character::isUpperCase);
@Override
public boolean match(Character t) {
return Character.isUpperCase(t);
}
});
} }
/** /**
@ -531,12 +521,7 @@ public class Validator {
* @since 3.3.0 * @since 3.3.0
*/ */
public static boolean isLowerCase(CharSequence value) { public static boolean isLowerCase(CharSequence value) {
return StrUtil.isAllCharMatch(value, new cn.hutool.core.lang.Matcher<Character>() { return StrUtil.isAllCharMatch(value, Character::isLowerCase);
@Override
public boolean match(Character t) {
return Character.isLowerCase(t);
}
});
} }
/** /**

View File

@ -1,13 +1,13 @@
package cn.hutool.core.thread; package cn.hutool.core.thread;
import cn.hutool.core.builder.Builder;
import cn.hutool.core.util.StrUtil;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import cn.hutool.core.builder.Builder;
import cn.hutool.core.util.StrUtil;
/** /**
* ThreadFactory创建器<br> * ThreadFactory创建器<br>
* 参考Guava的ThreadFactoryBuilder * 参考Guava的ThreadFactoryBuilder
@ -125,24 +125,21 @@ public class ThreadFactoryBuilder implements Builder<ThreadFactory>{
final Integer priority = builder.priority; final Integer priority = builder.priority;
final UncaughtExceptionHandler handler = builder.uncaughtExceptionHandler; final UncaughtExceptionHandler handler = builder.uncaughtExceptionHandler;
final AtomicLong count = (null == namePrefix) ? null : new AtomicLong(); final AtomicLong count = (null == namePrefix) ? null : new AtomicLong();
return new ThreadFactory() { return r -> {
@Override final Thread thread = backingThreadFactory.newThread(r);
public Thread newThread(Runnable r) { if (null != namePrefix) {
final Thread thread = backingThreadFactory.newThread(r); thread.setName(namePrefix + count.getAndIncrement());
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;
} }
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; 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.collection.CollectionUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException; 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.Validator;
import cn.hutool.core.lang.func.Func1; import cn.hutool.core.lang.func.Func1;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* 正则相关工具类<br> * 正则相关工具类<br>
* 常用正则请见 {@link Validator} * 常用正则请见 {@link Validator}
@ -174,12 +168,7 @@ public class ReUtil {
} }
//提取模板中的编号 //提取模板中的编号
final TreeSet<Integer> varNums = new TreeSet<>(new Comparator<Integer>() { final TreeSet<Integer> varNums = new TreeSet<>((o1, o2) -> ObjectUtil.compare(o2, o1));
@Override
public int compare(Integer o1, Integer o2) {
return ObjectUtil.compare(o2, o1);
}
});
final Matcher matcherForTemplate = PatternPool.GROUP_VAR.matcher(template); final Matcher matcherForTemplate = PatternPool.GROUP_VAR.matcher(template);
while (matcherForTemplate.find()) { while (matcherForTemplate.find()) {
varNums.add(Integer.parseInt(matcherForTemplate.group(1))); varNums.add(Integer.parseInt(matcherForTemplate.group(1)));

View File

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