This commit is contained in:
Looly 2022-09-29 18:20:19 +08:00
parent e4d7d6b7d4
commit ce11e7147d
23 changed files with 135 additions and 189 deletions

View File

@ -1,6 +1,7 @@
package cn.hutool.core.collection; package cn.hutool.core.collection;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.hash.Hash32;
import cn.hutool.core.collection.iter.IterUtil; import cn.hutool.core.collection.iter.IterUtil;
import cn.hutool.core.collection.iter.IteratorEnumeration; import cn.hutool.core.collection.iter.IteratorEnumeration;
import cn.hutool.core.comparator.CompareUtil; import cn.hutool.core.comparator.CompareUtil;
@ -9,7 +10,8 @@ import cn.hutool.core.comparator.PropertyComparator;
import cn.hutool.core.convert.CompositeConverter; import cn.hutool.core.convert.CompositeConverter;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.codec.hash.Hash32; import cn.hutool.core.lang.func.SerBiConsumer;
import cn.hutool.core.lang.func.SerConsumer3;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.reflect.ClassUtil; import cn.hutool.core.reflect.ClassUtil;
import cn.hutool.core.reflect.ConstructorUtil; import cn.hutool.core.reflect.ConstructorUtil;
@ -21,7 +23,6 @@ import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
import java.io.Serializable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.AbstractCollection; import java.util.AbstractCollection;
import java.util.ArrayList; import java.util.ArrayList;
@ -1942,14 +1943,14 @@ public class CollUtil {
// ------------------------------------------------------------------------------------------------- forEach // ------------------------------------------------------------------------------------------------- forEach
/** /**
* 循环遍历 {@link Iterable}使用{@link IndexedConsumer} 接受遍历的每条数据并针对每条数据做处理 * 循环遍历 {@link Iterable}使用{@link SerBiConsumer} 接受遍历的每条数据并针对每条数据做处理
* *
* @param <T> 集合元素类型 * @param <T> 集合元素类型
* @param iterable {@link Iterable} * @param iterable {@link Iterable}
* @param consumer {@link IndexedConsumer} 遍历的每条数据处理器 * @param consumer {@link SerBiConsumer} 遍历的每条数据处理器
* @since 5.4.7 * @since 5.4.7
*/ */
public static <T> void forEach(final Iterable<T> iterable, final IndexedConsumer<T> consumer) { public static <T> void forEach(final Iterable<T> iterable, final SerBiConsumer<T, Integer> consumer) {
if (iterable == null) { if (iterable == null) {
return; return;
} }
@ -1957,13 +1958,13 @@ public class CollUtil {
} }
/** /**
* 循环遍历 {@link Iterator}使用{@link IndexedConsumer} 接受遍历的每条数据并针对每条数据做处理 * 循环遍历 {@link Iterator}使用{@link SerBiConsumer} 接受遍历的每条数据并针对每条数据做处理
* *
* @param <T> 集合元素类型 * @param <T> 集合元素类型
* @param iterator {@link Iterator} * @param iterator {@link Iterator}
* @param consumer {@link IndexedConsumer} 遍历的每条数据处理器 * @param consumer {@link SerBiConsumer} 遍历的每条数据处理器
*/ */
public static <T> void forEach(final Iterator<T> iterator, final IndexedConsumer<T> consumer) { public static <T> void forEach(final Iterator<T> iterator, final SerBiConsumer<T, Integer> consumer) {
if (iterator == null) { if (iterator == null) {
return; return;
} }
@ -1975,13 +1976,13 @@ public class CollUtil {
} }
/** /**
* 循环遍历 {@link Enumeration}使用{@link IndexedConsumer} 接受遍历的每条数据并针对每条数据做处理 * 循环遍历 {@link Enumeration}使用{@link SerBiConsumer} 接受遍历的每条数据并针对每条数据做处理
* *
* @param <T> 集合元素类型 * @param <T> 集合元素类型
* @param enumeration {@link Enumeration} * @param enumeration {@link Enumeration}
* @param consumer {@link IndexedConsumer} 遍历的每条数据处理器 * @param consumer {@link SerBiConsumer} 遍历的每条数据处理器
*/ */
public static <T> void forEach(final Enumeration<T> enumeration, final IndexedConsumer<T> consumer) { public static <T> void forEach(final Enumeration<T> enumeration, final SerBiConsumer<T, Integer> consumer) {
if (enumeration == null) { if (enumeration == null) {
return; return;
} }
@ -1993,15 +1994,15 @@ public class CollUtil {
} }
/** /**
* 循环遍历Map使用{@link IndexedKVConsumer} 接受遍历的每条数据并针对每条数据做处理<br> * 循环遍历Map使用{@link SerConsumer3} 接受遍历的每条数据并针对每条数据做处理<br>
* 和JDK8中的map.forEach不同的是此方法支持index * 和JDK8中的map.forEach不同的是此方法支持index
* *
* @param <K> Key类型 * @param <K> Key类型
* @param <V> Value类型 * @param <V> Value类型
* @param map {@link Map} * @param map {@link Map}
* @param kvConsumer {@link IndexedKVConsumer} 遍历的每条数据处理器 * @param kvConsumer {@link SerConsumer3} 遍历的每条数据处理器
*/ */
public static <K, V> void forEach(final Map<K, V> map, final IndexedKVConsumer<K, V> kvConsumer) { public static <K, V> void forEach(final Map<K, V> map, final SerConsumer3<K, V, Integer> kvConsumer) {
if (map == null) { if (map == null) {
return; return;
} }
@ -2260,46 +2261,6 @@ public class CollUtil {
iterable.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y))); iterable.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y)));
} }
// ---------------------------------------------------------------------------------------------- Interface start
/**
* 针对一个参数做相应的操作<br>
* 此函数接口与JDK8中Consumer不同是多提供了index参数用于标记遍历对象是第几个
*
* @param <T> 处理参数类型
* @author Looly
*/
@FunctionalInterface
public interface IndexedConsumer<T> extends Serializable {
/**
* 接受并处理一个参数
*
* @param value 参数值
* @param index 参数在集合中的索引
*/
void accept(T value, int index);
}
/**
* 针对两个参数做相应的操作例如Map中的KEY和VALUE
*
* @param <K> KEY类型
* @param <V> VALUE类型
* @author Looly
*/
@FunctionalInterface
public interface IndexedKVConsumer<K, V> extends Serializable {
/**
* 接受并处理一对参数
*
* @param key
* @param value
* @param index 参数在集合中的索引
*/
void accept(K key, V value, int index);
}
// ---------------------------------------------------------------------------------------------- Interface end
/** /**
* 获取Collection或者iterator的大小此方法可以处理的对象类型如下 * 获取Collection或者iterator的大小此方法可以处理的对象类型如下
* <ul> * <ul>

View File

@ -13,6 +13,7 @@ import cn.hutool.core.io.file.Tailer;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.io.unit.DataSizeUtil; import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharUtil;
@ -2269,10 +2270,10 @@ public class FileUtil extends PathUtil {
* 按行处理文件内容编码为UTF-8 * 按行处理文件内容编码为UTF-8
* *
* @param file 文件 * @param file 文件
* @param lineHandler {@link LineHandler}行处理器 * @param lineHandler {@link SerConsumer}行处理器
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public static void readUtf8Lines(final File file, final LineHandler lineHandler) throws IORuntimeException { public static void readUtf8Lines(final File file, final SerConsumer<String> lineHandler) throws IORuntimeException {
readLines(file, CharsetUtil.UTF_8, lineHandler); readLines(file, CharsetUtil.UTF_8, lineHandler);
} }
@ -2281,10 +2282,10 @@ public class FileUtil extends PathUtil {
* *
* @param file 文件 * @param file 文件
* @param charset 编码 * @param charset 编码
* @param lineHandler {@link LineHandler}行处理器 * @param lineHandler {@link SerConsumer}行处理器
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public static void readLines(final File file, final Charset charset, final LineHandler lineHandler) throws IORuntimeException { public static void readLines(final File file, final Charset charset, final SerConsumer<String> lineHandler) throws IORuntimeException {
FileReader.of(file, charset).readLines(lineHandler); FileReader.of(file, charset).readLines(lineHandler);
} }
@ -2293,15 +2294,15 @@ public class FileUtil extends PathUtil {
* *
* @param file {@link RandomAccessFile}文件 * @param file {@link RandomAccessFile}文件
* @param charset 编码 * @param charset 编码
* @param lineHandler {@link LineHandler}行处理器 * @param lineHandler {@link SerConsumer}行处理器
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 4.5.2 * @since 4.5.2
*/ */
public static void readLines(final RandomAccessFile file, final Charset charset, final LineHandler lineHandler) { public static void readLines(final RandomAccessFile file, final Charset charset, final SerConsumer<String> lineHandler) {
String line; String line;
try { try {
while ((line = file.readLine()) != null) { while ((line = file.readLine()) != null) {
lineHandler.handle(CharsetUtil.convert(line, CharsetUtil.ISO_8859_1, charset)); lineHandler.accept(CharsetUtil.convert(line, CharsetUtil.ISO_8859_1, charset));
} }
} catch (final IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
@ -2313,14 +2314,14 @@ public class FileUtil extends PathUtil {
* *
* @param file {@link RandomAccessFile}文件 * @param file {@link RandomAccessFile}文件
* @param charset 编码 * @param charset 编码
* @param lineHandler {@link LineHandler}行处理器 * @param lineHandler {@link SerConsumer}行处理器
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 4.5.2 * @since 4.5.2
*/ */
public static void readLine(final RandomAccessFile file, final Charset charset, final LineHandler lineHandler) { public static void readLine(final RandomAccessFile file, final Charset charset, final SerConsumer<String> lineHandler) {
final String line = readLine(file, charset); final String line = readLine(file, charset);
if (null != line) { if (null != line) {
lineHandler.handle(line); lineHandler.accept(line);
} }
} }
@ -3352,7 +3353,7 @@ public class FileUtil extends PathUtil {
* @param file 文件 * @param file 文件
* @param handler 行处理器 * @param handler 行处理器
*/ */
public static void tail(final File file, final LineHandler handler) { public static void tail(final File file, final SerConsumer<String> handler) {
tail(file, CharsetUtil.UTF_8, handler); tail(file, CharsetUtil.UTF_8, handler);
} }
@ -3364,7 +3365,7 @@ public class FileUtil extends PathUtil {
* @param charset 编码 * @param charset 编码
* @param handler 行处理器 * @param handler 行处理器
*/ */
public static void tail(final File file, final Charset charset, final LineHandler handler) { public static void tail(final File file, final Charset charset, final SerConsumer<String> handler) {
new Tailer(file, charset, handler).start(); new Tailer(file, charset, handler).start();
} }

View File

@ -6,6 +6,7 @@ import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.copy.ReaderWriterCopier; import cn.hutool.core.io.copy.ReaderWriterCopier;
import cn.hutool.core.io.copy.StreamCopier; import cn.hutool.core.io.copy.StreamCopier;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.codec.HexUtil; import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
@ -621,7 +622,7 @@ public class IoUtil extends NioUtil {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public static <T extends Collection<String>> T readLines(final Reader reader, final T collection) throws IORuntimeException { public static <T extends Collection<String>> T readLines(final Reader reader, final T collection) throws IORuntimeException {
readLines(reader, (LineHandler) collection::add); readLines(reader, (SerConsumer<String>) collection::add);
return collection; return collection;
} }
@ -633,7 +634,7 @@ public class IoUtil extends NioUtil {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 3.1.1 * @since 3.1.1
*/ */
public static void readUtf8Lines(final InputStream in, final LineHandler lineHandler) throws IORuntimeException { public static void readUtf8Lines(final InputStream in, final SerConsumer<String> lineHandler) throws IORuntimeException {
readLines(in, CharsetUtil.UTF_8, lineHandler); readLines(in, CharsetUtil.UTF_8, lineHandler);
} }
@ -646,7 +647,7 @@ public class IoUtil extends NioUtil {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 3.0.9 * @since 3.0.9
*/ */
public static void readLines(final InputStream in, final Charset charset, final LineHandler lineHandler) throws IORuntimeException { public static void readLines(final InputStream in, final Charset charset, final SerConsumer<String> lineHandler) throws IORuntimeException {
readLines(getReader(in, charset), lineHandler); readLines(getReader(in, charset), lineHandler);
} }
@ -659,12 +660,12 @@ public class IoUtil extends NioUtil {
* @param lineHandler 行处理接口实现handle方法用于编辑一行的数据后入到指定地方 * @param lineHandler 行处理接口实现handle方法用于编辑一行的数据后入到指定地方
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public static void readLines(final Reader reader, final LineHandler lineHandler) throws IORuntimeException { public static void readLines(final Reader reader, final SerConsumer<String> lineHandler) throws IORuntimeException {
Assert.notNull(reader); Assert.notNull(reader);
Assert.notNull(lineHandler); Assert.notNull(lineHandler);
for (final String line : lineIter(reader)) { for (final String line : lineIter(reader)) {
lineHandler.handle(line); lineHandler.accept(line);
} }
} }

View File

@ -1,15 +0,0 @@
package cn.hutool.core.io;
/**
* 行处理器
* @author Looly
*
*/
@FunctionalInterface
public interface LineHandler {
/**
* 处理一行数据可以编辑后存入指定地方
* @param line
*/
void handle(String line);
}

View File

@ -3,9 +3,9 @@ package cn.hutool.core.io.file;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.LineHandler; import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.CharsetUtil;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -179,7 +179,7 @@ public class FileReader extends FileWrapper {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 3.0.9 * @since 3.0.9
*/ */
public void readLines(final LineHandler lineHandler) throws IORuntimeException{ public void readLines(final SerConsumer<String> lineHandler) throws IORuntimeException{
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = FileUtil.getReader(file, charset); reader = FileUtil.getReader(file, charset);

View File

@ -2,8 +2,8 @@ package cn.hutool.core.io.file;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.io.watch.SimpleWatcher; import cn.hutool.core.io.watch.SimpleWatcher;
import cn.hutool.core.lang.func.SerConsumer;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
@ -21,16 +21,16 @@ public class LineReadWatcher extends SimpleWatcher implements Runnable {
private final RandomAccessFile randomAccessFile; private final RandomAccessFile randomAccessFile;
private final Charset charset; private final Charset charset;
private final LineHandler lineHandler; private final SerConsumer<String> lineHandler;
/** /**
* 构造 * 构造
* *
* @param randomAccessFile {@link RandomAccessFile} * @param randomAccessFile {@link RandomAccessFile}
* @param charset 编码 * @param charset 编码
* @param lineHandler 行处理器{@link LineHandler}实现 * @param lineHandler 行处理器{@link SerConsumer}实现
*/ */
public LineReadWatcher(final RandomAccessFile randomAccessFile, final Charset charset, final LineHandler lineHandler) { public LineReadWatcher(final RandomAccessFile randomAccessFile, final Charset charset, final SerConsumer<String> lineHandler) {
this.randomAccessFile = randomAccessFile; this.randomAccessFile = randomAccessFile;
this.charset = charset; this.charset = charset;
this.lineHandler = lineHandler; this.lineHandler = lineHandler;
@ -45,7 +45,7 @@ public class LineReadWatcher extends SimpleWatcher implements Runnable {
public void onModify(final WatchEvent<?> event, final Path currentPath) { public void onModify(final WatchEvent<?> event, final Path currentPath) {
final RandomAccessFile randomAccessFile = this.randomAccessFile; final RandomAccessFile randomAccessFile = this.randomAccessFile;
final Charset charset = this.charset; final Charset charset = this.charset;
final LineHandler lineHandler = this.lineHandler; final SerConsumer<String> lineHandler = this.lineHandler;
try { try {
final long currentLength = randomAccessFile.length(); final long currentLength = randomAccessFile.length();

View File

@ -4,8 +4,8 @@ import cn.hutool.core.date.DateUnit;
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.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
@ -30,12 +30,15 @@ import java.util.concurrent.TimeUnit;
public class Tailer implements Serializable { public class Tailer implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final LineHandler CONSOLE_HANDLER = new ConsoleLineHandler(); /**
* 控制台打印的处理类
*/
public static final SerConsumer<String> CONSOLE_HANDLER = new ConsoleLineHandler();
/** 编码 */ /** 编码 */
private final Charset charset; private final Charset charset;
/** 行处理器 */ /** 行处理器 */
private final LineHandler lineHandler; private final SerConsumer<String> lineHandler;
/** 初始读取的行数 */ /** 初始读取的行数 */
private final int initReadLine; private final int initReadLine;
/** 定时任务检查间隔时长 */ /** 定时任务检查间隔时长 */
@ -50,7 +53,7 @@ public class Tailer implements Serializable {
* @param file 文件 * @param file 文件
* @param lineHandler 行处理器 * @param lineHandler 行处理器
*/ */
public Tailer(final File file, final LineHandler lineHandler) { public Tailer(final File file, final SerConsumer<String> lineHandler) {
this(file, lineHandler, 0); this(file, lineHandler, 0);
} }
@ -61,7 +64,7 @@ public class Tailer implements Serializable {
* @param lineHandler 行处理器 * @param lineHandler 行处理器
* @param initReadLine 启动时预读取的行数 * @param initReadLine 启动时预读取的行数
*/ */
public Tailer(final File file, final LineHandler lineHandler, final int initReadLine) { public Tailer(final File file, final SerConsumer<String> lineHandler, final int initReadLine) {
this(file, CharsetUtil.UTF_8, lineHandler, initReadLine, DateUnit.SECOND.getMillis()); this(file, CharsetUtil.UTF_8, lineHandler, initReadLine, DateUnit.SECOND.getMillis());
} }
@ -72,7 +75,7 @@ public class Tailer implements Serializable {
* @param charset 编码 * @param charset 编码
* @param lineHandler 行处理器 * @param lineHandler 行处理器
*/ */
public Tailer(final File file, final Charset charset, final LineHandler lineHandler) { public Tailer(final File file, final Charset charset, final SerConsumer<String> lineHandler) {
this(file, charset, lineHandler, 0, DateUnit.SECOND.getMillis()); this(file, charset, lineHandler, 0, DateUnit.SECOND.getMillis());
} }
@ -85,7 +88,7 @@ public class Tailer implements Serializable {
* @param initReadLine 启动时预读取的行数 * @param initReadLine 启动时预读取的行数
* @param period 检查间隔 * @param period 检查间隔
*/ */
public Tailer(final File file, final Charset charset, final LineHandler lineHandler, final int initReadLine, final long period) { public Tailer(final File file, final Charset charset, final SerConsumer<String> lineHandler, final int initReadLine, final long period) {
checkFile(file); checkFile(file);
this.charset = charset; this.charset = charset;
this.lineHandler = lineHandler; this.lineHandler = lineHandler;
@ -188,7 +191,7 @@ public class Tailer implements Serializable {
// 输出缓存栈中的内容 // 输出缓存栈中的内容
while (false == stack.isEmpty()) { while (false == stack.isEmpty()) {
this.lineHandler.handle(stack.pop()); this.lineHandler.accept(stack.pop());
} }
} }
@ -221,9 +224,10 @@ public class Tailer implements Serializable {
* @author looly * @author looly
* @since 4.5.2 * @since 4.5.2
*/ */
public static class ConsoleLineHandler implements LineHandler { public static class ConsoleLineHandler implements SerConsumer<String> {
private static final long serialVersionUID = 1L;
@Override @Override
public void handle(final String line) { public void accepting(final String line) {
Console.log(line); Console.log(line);
} }
} }

View File

@ -1,24 +0,0 @@
package cn.hutool.core.io.watch;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
/**
* 监听事件处理函数接口
*
* @author looly
* @since 5.4.0
*/
@FunctionalInterface
public interface WatchAction {
/**
* 事件处理通过实现此方法处理各种事件
*
* 事件可以调用 {@link WatchEvent#kind()}获取对应事件见{@link WatchKind}
*
* @param event 事件
* @param currentPath 事件发生的当前Path路径
*/
void doAction(WatchEvent<?> event, Path currentPath);
}

View File

@ -1,6 +1,7 @@
package cn.hutool.core.io.watch; package cn.hutool.core.io.watch;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.func.SerBiConsumer;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import java.io.Closeable; import java.io.Closeable;
@ -132,7 +133,7 @@ public class WatchServer extends Thread implements Closeable, Serializable {
* @param watchFilter 监听过滤接口通过实现此接口过滤掉不需要监听的情况{@link Predicate#test(Object)}{@code true}保留null表示不过滤 * @param watchFilter 监听过滤接口通过实现此接口过滤掉不需要监听的情况{@link Predicate#test(Object)}{@code true}保留null表示不过滤
* @since 5.4.0 * @since 5.4.0
*/ */
public void watch(final WatchAction action, final Predicate<WatchEvent<?>> watchFilter) { public void watch(final SerBiConsumer<WatchEvent<?>, Path> action, final Predicate<WatchEvent<?>> watchFilter) {
final WatchKey wk; final WatchKey wk;
try { try {
wk = watchService.take(); wk = watchService.take();
@ -150,7 +151,7 @@ public class WatchServer extends Thread implements Closeable, Serializable {
continue; continue;
} }
action.doAction(event, currentPath); action.accept(event, currentPath);
} }
wk.reset(); wk.reset();

View File

@ -6,6 +6,7 @@ import cn.hutool.core.tree.MapTree;
* 树节点解析器 可以参考{@link DefaultNodeParser} * 树节点解析器 可以参考{@link DefaultNodeParser}
* *
* @param <T> 转换的实体 为数据源里的对象类型 * @param <T> 转换的实体 为数据源里的对象类型
* @param <E> ID类型
* @author liangbaikai * @author liangbaikai
*/ */
@FunctionalInterface @FunctionalInterface

View File

@ -0,0 +1,4 @@
/**
* 节点解析器封装
*/
package cn.hutool.core.tree.parser;

View File

@ -56,7 +56,7 @@ public class CoordinateUtil {
* WGS84 转换为 火星坐标系 (GCJ-02) * WGS84 转换为 火星坐标系 (GCJ-02)
* *
* @param lng 经度值 * @param lng 经度值
* @param lat 度值 * @param lat 度值
* @return 火星坐标 (GCJ-02) * @return 火星坐标 (GCJ-02)
*/ */
public static Coordinate wgs84ToGcj02(final double lng, final double lat) { public static Coordinate wgs84ToGcj02(final double lng, final double lat) {
@ -67,7 +67,7 @@ public class CoordinateUtil {
* WGS84 坐标转为 百度坐标系 (BD-09) 坐标 * WGS84 坐标转为 百度坐标系 (BD-09) 坐标
* *
* @param lng 经度值 * @param lng 经度值
* @param lat 度值 * @param lat 度值
* @return bd09 坐标 * @return bd09 坐标
*/ */
public static Coordinate wgs84ToBd09(final double lng, final double lat) { public static Coordinate wgs84ToBd09(final double lng, final double lat) {

View File

@ -1,6 +1,7 @@
package cn.hutool.core.io; package cn.hutool.core.io;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -27,7 +28,7 @@ public class IoUtilTest {
@Test @Test
public void readLinesTest() { public void readLinesTest() {
try (final BufferedReader reader = ResourceUtil.getUtf8Reader("test_lines.csv")) { try (final BufferedReader reader = ResourceUtil.getUtf8Reader("test_lines.csv")) {
IoUtil.readLines(reader, (LineHandler) Assert::assertNotNull); IoUtil.readLines(reader, (SerConsumer<String>) Assert::assertNotNull);
} catch (final IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }

View File

@ -2,12 +2,17 @@ package cn.hutool.cron.task;
/** /**
* {@link Runnable} {@link Task}包装 * {@link Runnable} {@link Task}包装
* @author Looly
* *
* @author Looly
*/ */
public class RunnableTask implements Task{ public class RunnableTask implements Task {
private final Runnable runnable; private final Runnable runnable;
/**
* 构造
*
* @param runnable {@link Runnable}
*/
public RunnableTask(final Runnable runnable) { public RunnableTask(final Runnable runnable) {
this.runnable = runnable; this.runnable = runnable;
} }

View File

@ -2,6 +2,7 @@ package cn.hutool.http;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch; import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.core.net.ssl.SSLProtocols; import cn.hutool.core.net.ssl.SSLProtocols;
import cn.hutool.core.net.url.UrlBuilder; import cn.hutool.core.net.url.UrlBuilder;
@ -19,6 +20,7 @@ import java.util.Map;
* *
* @author Looly * @author Looly
*/ */
@SuppressWarnings("resource")
public class HttpRequestTest { public class HttpRequestTest {
final String url = "http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2"; final String url = "http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2";
@ -218,4 +220,34 @@ public class HttpRequestTest {
final HttpResponse execute = HttpRequest.get("http://localhost:8888/getCookier").execute(); final HttpResponse execute = HttpRequest.get("http://localhost:8888/getCookier").execute();
Console.log(execute.getCookies()); Console.log(execute.getCookies());
} }
@Test
public void optionsTest() {
final HttpRequest options = HttpRequest.options("https://hutool.cn");
Assert.notNull(options.toString());
}
@Test
public void deleteTest() {
final HttpRequest options = HttpRequest.delete("https://hutool.cn");
Assert.notNull(options.toString());
}
@Test
public void traceTest() {
final HttpRequest options = HttpRequest.trace("https://hutool.cn");
Assert.notNull(options.toString());
}
@Test
public void getToStringTest() {
final HttpRequest a = HttpRequest.get("https://hutool.cn/").form("a", 1);
Assert.notNull(a.toString());
}
@Test
public void postToStringTest() {
final HttpRequest a = HttpRequest.post("https://hutool.cn/").form("a", 1);
Console.log(a.toString());
}
} }

View File

@ -4,6 +4,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
@ -125,7 +126,7 @@ public class CsvBaseReader implements Serializable {
* @param csvStr CSV字符串 * @param csvStr CSV字符串
* @param rowHandler 行处理器用于一行一行的处理数据 * @param rowHandler 行处理器用于一行一行的处理数据
*/ */
public void readFromStr(final String csvStr, final CsvRowHandler rowHandler) { public void readFromStr(final String csvStr, final SerConsumer<CsvRow> rowHandler) {
read(parse(new StringReader(csvStr)), rowHandler); read(parse(new StringReader(csvStr)), rowHandler);
} }
@ -242,7 +243,7 @@ public class CsvBaseReader implements Serializable {
* @param rowHandler 行处理器用于一行一行的处理数据 * @param rowHandler 行处理器用于一行一行的处理数据
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public void read(final Reader reader, final CsvRowHandler rowHandler) throws IORuntimeException { public void read(final Reader reader, final SerConsumer<CsvRow> rowHandler) throws IORuntimeException {
read(parse(reader), rowHandler); read(parse(reader), rowHandler);
} }
@ -256,10 +257,10 @@ public class CsvBaseReader implements Serializable {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 5.0.4 * @since 5.0.4
*/ */
private void read(final CsvParser csvParser, final CsvRowHandler rowHandler) throws IORuntimeException { private void read(final CsvParser csvParser, final SerConsumer<CsvRow> rowHandler) throws IORuntimeException {
try { try {
while (csvParser.hasNext()){ while (csvParser.hasNext()){
rowHandler.handle(csvParser.next()); rowHandler.accept(csvParser.next());
} }
} finally { } finally {
IoUtil.close(csvParser); IoUtil.close(csvParser);

View File

@ -3,6 +3,7 @@ package cn.hutool.poi.csv;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.func.SerConsumer;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
@ -120,7 +121,7 @@ public class CsvReader extends CsvBaseReader implements Iterable<CsvRow>, Closea
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 5.0.4 * @since 5.0.4
*/ */
public void read(final CsvRowHandler rowHandler) throws IORuntimeException { public void read(final SerConsumer<CsvRow> rowHandler) throws IORuntimeException {
read(this.reader, rowHandler); read(this.reader, rowHandler);
} }

View File

@ -1,18 +0,0 @@
package cn.hutool.poi.csv;
/**
* CSV的行处理器实现此接口用于按照行处理数据
*
* @author Looly
* @since 5.0.4
*/
@FunctionalInterface
public interface CsvRowHandler {
/**
* 处理行数据
*
* @param row 行数据
*/
void handle(CsvRow row);
}

View File

@ -2,8 +2,8 @@ package cn.hutool.poi.excel;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.func.SerBiConsumer;
import cn.hutool.poi.excel.cell.CellEditor; import cn.hutool.poi.excel.cell.CellEditor;
import cn.hutool.poi.excel.cell.CellHandler;
import cn.hutool.poi.excel.cell.CellUtil; import cn.hutool.poi.excel.cell.CellUtil;
import cn.hutool.poi.excel.reader.BeanSheetReader; import cn.hutool.poi.excel.reader.BeanSheetReader;
import cn.hutool.poi.excel.reader.ColumnSheetReader; import cn.hutool.poi.excel.reader.ColumnSheetReader;
@ -252,7 +252,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
* @param cellHandler 单元格处理器用于处理读到的单元格及其数据 * @param cellHandler 单元格处理器用于处理读到的单元格及其数据
* @since 5.3.8 * @since 5.3.8
*/ */
public void read(final CellHandler cellHandler) { public void read(final SerBiConsumer<Cell, Object> cellHandler) {
read(0, Integer.MAX_VALUE, cellHandler); read(0, Integer.MAX_VALUE, cellHandler);
} }
@ -265,7 +265,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
* @param cellHandler 单元格处理器用于处理读到的单元格及其数据 * @param cellHandler 单元格处理器用于处理读到的单元格及其数据
* @since 5.3.8 * @since 5.3.8
*/ */
public void read(int startRowIndex, int endRowIndex, final CellHandler cellHandler) { public void read(int startRowIndex, int endRowIndex, final SerBiConsumer<Cell, Object> cellHandler) {
checkNotClosed(); checkNotClosed();
startRowIndex = Math.max(startRowIndex, this.sheet.getFirstRowNum());// 读取起始行包含 startRowIndex = Math.max(startRowIndex, this.sheet.getFirstRowNum());// 读取起始行包含
@ -280,7 +280,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
Cell cell; Cell cell;
for (short x = 0; x < columnSize; x++) { for (short x = 0; x < columnSize; x++) {
cell = row.getCell(x); cell = row.getCell(x);
cellHandler.handle(cell, CellUtil.getCellValue(cell)); cellHandler.accept(cell, CellUtil.getCellValue(cell));
} }
} }
} }

View File

@ -1,21 +0,0 @@
package cn.hutool.poi.excel.cell;
import org.apache.poi.ss.usermodel.Cell;
/**
* 单元格处理器接口<br>
* 用于在读取Excel单元格值时自定义结果值的获取如在获取值的同时获取单元格样式坐标等信息或根据单元格信息装饰转换结果值
*
* @author Looly
*/
@FunctionalInterface
public interface CellHandler {
/**
* 处理
*
* @param cell 单元格对象可以获取单元格行列样式等信息
* @param value 单元格值
*/
void handle(Cell cell, Object value);
}

View File

@ -15,9 +15,13 @@ import java.io.InputStream;
*/ */
public interface ExcelSaxReader<T> { public interface ExcelSaxReader<T> {
// sheet r:Id前缀 /**
* sheet r:Id前缀
*/
String RID_PREFIX = "rId"; String RID_PREFIX = "rId";
// sheet name前缀 /**
* sheet name前缀
*/
String SHEET_NAME_PREFIX = "sheetName:"; String SHEET_NAME_PREFIX = "sheetName:";
/** /**

View File

@ -20,6 +20,7 @@ import java.util.List;
* <pre> * <pre>
* &lt;sheetData&gt;&lt;/sheetData&gt; * &lt;sheetData&gt;&lt;/sheetData&gt;
* </pre> * </pre>
*
* @since 5.5.3 * @since 5.5.3
*/ */
public class SheetDataSaxHandler extends DefaultHandler { public class SheetDataSaxHandler extends DefaultHandler {
@ -61,7 +62,12 @@ public class SheetDataSaxHandler extends DefaultHandler {
// 存储每行的列元素 // 存储每行的列元素
private List<Object> rowCellList = new ArrayList<>(); private List<Object> rowCellList = new ArrayList<>();
public SheetDataSaxHandler(final RowHandler rowHandler){ /**
* 构造
*
* @param rowHandler 行处理器
*/
public SheetDataSaxHandler(final RowHandler rowHandler) {
this.rowHandler = rowHandler; this.rowHandler = rowHandler;
} }
@ -155,7 +161,7 @@ public class SheetDataSaxHandler extends DefaultHandler {
lastFormula.append(ch, start, length); lastFormula.append(ch, start, length);
break; break;
} }
} else{ } else {
// 按理说内容应该为"<v>内容</v>"但是某些特别的XML内容不在v或f标签中此处做一些兼容 // 按理说内容应该为"<v>内容</v>"但是某些特别的XML内容不在v或f标签中此处做一些兼容
// issue#1303@Github // issue#1303@Github
lastContent.append(ch, start, length); lastContent.append(ch, start, length);

View File

@ -2,10 +2,11 @@ package cn.hutool.poi.excel;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.func.SerBiConsumer;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
import cn.hutool.poi.excel.cell.CellHandler;
import lombok.Data; import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -231,7 +232,7 @@ public class ExcelReadTest {
@Ignore @Ignore
public void readNullRowTest(){ public void readNullRowTest(){
final ExcelReader reader = ExcelUtil.getReader("d:/test/1.-.xls"); final ExcelReader reader = ExcelUtil.getReader("d:/test/1.-.xls");
reader.read((CellHandler) Console::log); reader.read((SerBiConsumer<Cell, Object>) Console::log);
} }
@Test @Test