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相关工具类
@ -34,12 +33,12 @@ public class LambdaUtil {
* MyTeacher myTeacher = new MyTeacher(); * MyTeacher myTeacher = new MyTeacher();
* Class<MyTeacher> supplierClass = LambdaUtil.getRealClass(myTeacher::getAge); * Class<MyTeacher> supplierClass = LambdaUtil.getRealClass(myTeacher::getAge);
* Assert.assertEquals(MyTeacher.class, supplierClass); * Assert.assertEquals(MyTeacher.class, supplierClass);
* }</pre> * }</pre>
* </li> * </li>
* <li>引用静态无参方法<pre>{@code * <li>引用静态无参方法<pre>{@code
* Class<MyTeacher> staticSupplierClass = LambdaUtil.getRealClass(MyTeacher::takeAge); * Class<MyTeacher> staticSupplierClass = LambdaUtil.getRealClass(MyTeacher::takeAge);
* Assert.assertEquals(MyTeacher.class, staticSupplierClass); * Assert.assertEquals(MyTeacher.class, staticSupplierClass);
* }</pre> * }</pre>
* </li> * </li>
* </ul> * </ul>
* 在以下场景无法获取到正确类型 * 在以下场景无法获取到正确类型
@ -123,7 +122,7 @@ public class LambdaUtil {
* </ul> * </ul>
* *
* @param func 函数 * @param func 函数
* @param <T> lambda的类型 * @param <T> lambda的类型
* @return 方法名称 * @return 方法名称
* @throws IllegalArgumentException 非Getter或Setter方法 * @throws IllegalArgumentException 非Getter或Setter方法
* @since 5.7.23 * @since 5.7.23
@ -136,8 +135,8 @@ public class LambdaUtil {
* 等效于 Obj::getXxx * 等效于 Obj::getXxx
* *
* @param getMethod getter方法 * @param getMethod getter方法
* @param <T> 调用getter方法对象类型 * @param <T> 调用getter方法对象类型
* @param <R> getter方法返回值类型 * @param <R> getter方法返回值类型
* @return Obj::getXxx * @return Obj::getXxx
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -148,10 +147,10 @@ public class LambdaUtil {
/** /**
* 等效于 Obj::getXxx * 等效于 Obj::getXxx
* *
* @param clazz 调用getter方法对象类 * @param clazz 调用getter方法对象类
* @param fieldName 字段名称 * @param fieldName 字段名称
* @param <T> 调用getter方法对象类型 * @param <T> 调用getter方法对象类型
* @param <R> getter方法返回值类型 * @param <R> getter方法返回值类型
* @return Obj::getXxx * @return Obj::getXxx
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -163,8 +162,8 @@ public class LambdaUtil {
* 等效于 Obj::setXxx * 等效于 Obj::setXxx
* *
* @param setMethod setter方法 * @param setMethod setter方法
* @param <T> 调用setter方法对象类型 * @param <T> 调用setter方法对象类型
* @param <P> setter方法返回的值类型 * @param <P> setter方法返回的值类型
* @return Obj::setXxx * @return Obj::setXxx
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -175,10 +174,10 @@ public class LambdaUtil {
/** /**
* Obj::setXxx * Obj::setXxx
* *
* @param clazz 调用setter方法对象类 * @param clazz 调用setter方法对象类
* @param fieldName 字段名称 * @param fieldName 字段名称
* @param <T> 调用setter方法对象类型 * @param <T> 调用setter方法对象类型
* @param <P> setter方法返回的值类型 * @param <P> setter方法返回的值类型
* @return Obj::setXxx * @return Obj::setXxx
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -189,17 +188,60 @@ public class LambdaUtil {
/** /**
* 等效于 Obj::method * 等效于 Obj::method
* *
* @param lambdaType 接受lambda的函数式接口类型 * @param lambdaType 接受lambda的函数式接口类型
* @param clazz 调用类 * @param clazz 调用类
* @param methodName 方法名 * @param methodName 方法名
* @param paramsTypes 方法参数类型数组 * @param paramsTypes 方法参数类型数组
* @param <F> 函数式接口类型 * @param <F> 函数式接口类型
* @return Obj::method * @return Obj::method
*/ */
public static <F> F build(final Class<F> lambdaType, final Class<?> clazz, final String methodName, final Class<?>... paramsTypes) { public static <F> F build(final Class<F> lambdaType, final Class<?> clazz, final String methodName, final Class<?>... paramsTypes) {
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));