This commit is contained in:
Looly 2023-03-08 23:36:21 +08:00
parent 1960e1ef2a
commit e215f22292
7 changed files with 85 additions and 43 deletions

View File

@ -1,6 +1,7 @@
package cn.hutool.core.date; package cn.hutool.core.date;
import cn.hutool.core.date.format.GlobalCustomFormat; import cn.hutool.core.date.format.GlobalCustomFormat;
import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
@ -346,19 +347,11 @@ public class TimeUtil extends TemporalAccessorUtil {
/** /**
* 格式化时间函数 * 格式化时间函数
* *
* @param dateTimeFormatter {@link DateTimeFormatter}
* @return 格式化时间的函数 * @return 格式化时间的函数
*/ */
public static Function<LocalDateTime, String> formatTimeFunction(DateTimeFormatter dateTimeFormatter) { public static Function<TemporalAccessor, String> formatFunc(final DateTimeFormatter dateTimeFormatter) {
return time -> format(time, dateTimeFormatter); return LambdaUtil.toFunction(TimeUtil::format, dateTimeFormatter);
}
/**
* 格式化日期函数
*
* @return 格式化时间的函数
*/
public static Function<LocalDate, String> formatDateFunction(DateTimeFormatter dateTimeFormatter) {
return date -> format(date, dateTimeFormatter);
} }
/** /**

View File

@ -13,8 +13,7 @@ import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.function.BiConsumer; import java.util.function.*;
import java.util.function.Function;
/** /**
* Lambda相关工具类 * Lambda相关工具类
@ -200,6 +199,49 @@ public class LambdaUtil {
return LambdaFactory.build(lambdaType, clazz, methodName, paramsTypes); return LambdaFactory.build(lambdaType, clazz, methodName, paramsTypes);
} }
/**
* 通过自定义固定参数{@link BiFunction}转换为{@link Function}
*
* @param biFunction {@link BiFunction}
* @param param 参数
* @param <T> 参数类型
* @param <U> 参数2类型
* @param <R> 返回值类型
* @return {@link Function}
* @since 6.0.0
*/
public static <T, U, R> Function<T, R> toFunction(final BiFunction<T, U, R> biFunction, final U param) {
return (t) -> biFunction.apply(t, param);
}
/**
* 通过自定义固定参数{@link BiPredicate}转换为{@link Predicate}
*
* @param biPredicate {@link BiFunction}
* @param param 参数
* @param <T> 参数类型
* @param <U> 参数2类型
* @return {@link Predicate}
* @since 6.0.0
*/
public static <T, U> Predicate<T> toPredicate(final BiPredicate<T, U> biPredicate, final U param) {
return (t) -> biPredicate.test(t, param);
}
/**
* 通过自定义固定参数{@link BiConsumer}转换为{@link Consumer}
*
* @param biConsumer {@link BiConsumer}
* @param param 参数
* @param <T> 参数类型
* @param <U> 参数2类型
* @return {@link Consumer}
* @since 6.0.0
*/
public static <T, U> Consumer<T> toPredicate(final BiConsumer<T, U> biConsumer, final U param) {
return (t) -> biConsumer.accept(t, param);
}
//region Private methods //region Private methods
/** /**

View File

@ -12,6 +12,13 @@ import java.util.function.Predicate;
*/ */
public class PredicateUtil { public class PredicateUtil {
/**
* 反向条件
*
* @param predicate 条件
* @param <T> 参数类型
* @return 反向条件 {@link Predicate}
*/
public static <T> Predicate<T> negate(final Predicate<T> predicate) { public static <T> Predicate<T> negate(final Predicate<T> predicate) {
return predicate.negate(); return predicate.negate();
} }

View File

@ -10,9 +10,9 @@ import java.util.function.BinaryOperator;
/** /**
* SerBinaryOperator * SerBinaryOperator
* *
* @author VampireAchao
* @since 2022/6/8
* @param <T> 参数和返回值类型 * @param <T> 参数和返回值类型
* @author VampireAchao
* @see BinaryOperator
*/ */
@FunctionalInterface @FunctionalInterface
public interface SerBinaryOperator<T> extends BinaryOperator<T>, Serializable { public interface SerBinaryOperator<T> extends BinaryOperator<T>, Serializable {
@ -44,7 +44,7 @@ public interface SerBinaryOperator<T> extends BinaryOperator<T>, Serializable {
} }
/** /**
* Returns a {@link SerBinaryOperator} which returns the lesser of two elements * Returns a {@code SerBinaryOperator} which returns the lesser of two elements
* according to the specified {@code Comparator}. * according to the specified {@code Comparator}.
* *
* @param <T> the type of the input arguments of the comparator * @param <T> the type of the input arguments of the comparator
@ -59,7 +59,7 @@ public interface SerBinaryOperator<T> extends BinaryOperator<T>, Serializable {
} }
/** /**
* Returns a {@link SerBinaryOperator} which returns the greater of two elements * Returns a {@code SerBinaryOperator} which returns the greater of two elements
* according to the specified {@code Comparator}. * according to the specified {@code Comparator}.
* *
* @param <T> the type of the input arguments of the comparator * @param <T> the type of the input arguments of the comparator

View File

@ -66,7 +66,7 @@ public interface SerConsumer<T> extends Consumer<T>, Serializable {
*/ */
default SerConsumer<T> andThen(final SerConsumer<? super T> after) { default SerConsumer<T> andThen(final SerConsumer<? super T> after) {
Objects.requireNonNull(after); Objects.requireNonNull(after);
return (T t) -> { return (final T t) -> {
accept(t); accept(t);
after.accept(t); after.accept(t);
}; };

View File

@ -34,10 +34,10 @@ public interface SerConsumer3<P1, P2, P3> extends Serializable {
* @param p2 参数二 * @param p2 参数二
* @param p3 参数三 * @param p3 参数三
*/ */
default void accept(P1 p1, P2 p2, P3 p3) { default void accept(final P1 p1, final P2 p2, final P3 p3) {
try { try {
accepting(p1, p2, p3); accepting(p1, p2, p3);
} catch (Exception e) { } catch (final Exception e) {
throw new UtilException(e); throw new UtilException(e);
} }
} }
@ -54,9 +54,9 @@ public interface SerConsumer3<P1, P2, P3> extends Serializable {
* operation followed by the {@code after} operation * operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null * @throws NullPointerException if {@code after} is null
*/ */
default SerConsumer3<P1, P2, P3> andThen(SerConsumer3<P1, P2, P3> after) { default SerConsumer3<P1, P2, P3> andThen(final SerConsumer3<P1, P2, P3> after) {
Objects.requireNonNull(after); Objects.requireNonNull(after);
return (P1 p1, P2 p2, P3 p3) -> { return (final P1 p1, final P2 p2, final P3 p3) -> {
accept(p1, p2, p3); accept(p1, p2, p3);
after.accept(p1, p2, p3); after.accept(p1, p2, p3);
}; };

View File

@ -326,9 +326,9 @@ public class TimeUtilTest {
@Test @Test
public void formatDateFunctionTest() { public void formatDateFunctionTest() {
List<String> dateStrList = Stream.of("2023-03-01", "2023-03-02") final List<String> dateStrList = Stream.of("2023-03-01", "2023-03-02")
.map(LocalDate::parse) .map(LocalDate::parse)
.map(TimeUtil.formatDateFunction(DatePattern.CHINESE_DATE_FORMATTER)) .map(TimeUtil.formatFunc(DatePattern.CHINESE_DATE_FORMATTER))
.collect(Collectors.toList()); .collect(Collectors.toList());
Assert.assertEquals("2023年03月01日", dateStrList.get(0)); Assert.assertEquals("2023年03月01日", dateStrList.get(0));
Assert.assertEquals("2023年03月02日", dateStrList.get(1)); Assert.assertEquals("2023年03月02日", dateStrList.get(1));
@ -336,9 +336,9 @@ public class TimeUtilTest {
@Test @Test
public void formatTimeFunctionTest() { public void formatTimeFunctionTest() {
List<String> dateStrList = Stream.of("2023-03-01T12:23:56", "2023-03-02T12:23:56") final List<String> dateStrList = Stream.of("2023-03-01T12:23:56", "2023-03-02T12:23:56")
.map(LocalDateTime::parse) .map(LocalDateTime::parse)
.map(TimeUtil.formatTimeFunction(DatePattern.CHINESE_DATE_FORMATTER)) .map(TimeUtil.formatFunc(DatePattern.CHINESE_DATE_FORMATTER))
.collect(Collectors.toList()); .collect(Collectors.toList());
Assert.assertEquals("2023年03月01日", dateStrList.get(0)); Assert.assertEquals("2023年03月01日", dateStrList.get(0));
Assert.assertEquals("2023年03月02日", dateStrList.get(1)); Assert.assertEquals("2023年03月02日", dateStrList.get(1));