Skip to content

Commit

Permalink
实现TtlWrappers中的非重载版本,解决部分泛型场景下类型推测的问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
huangfei1101 committed Dec 31, 2021
1 parent 669766b commit c3e04b7
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/main/java/com/alibaba/ttl/TtlWrappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,87 @@
* @since 2.11.4
*/
public class TtlWrappers {
/**
* wrap {@link Supplier} to TTL warapper.
*
* @param supplier input {@link Supplier}
* @param <T>
* @return Wrapped {@link Supplier}
* @see TtlUnwrap#unwrap(Object)
* @since 2.12.4
*/
@Nullable
public static <T> Supplier<T> wrapSupplier(@Nullable Supplier<T> supplier) {
return wrap(supplier);
}

/**
* wrap {@link Consumer} to TTL warapper.
*
* @param consumer input {@link Consumer}
* @param <T>
* @return Wrapped {@link Consumer}
* @see TtlUnwrap#unwrap(Object)
* @since 2.12.4
*/
@Nullable
public static <T> Consumer<T> wrapConsumer(@Nullable Consumer<T> consumer) {
return wrap(consumer);
}

/**
* wrap {@link BiConsumer} to TTL warapper.
*
* @param biConsumer input {@link BiConsumer}
* @param <T>
* @return Wrapped {@link BiConsumer}
* @see TtlUnwrap#unwrap(Object)
* @since 2.12.4
*/
@Nullable
public static <T, U> BiConsumer<T, U> wrapBiConsumer(@Nullable BiConsumer<T, U> biConsumer) {
return wrap(biConsumer);
}

/**
* wrap {@link Function} to TTL warapper.
*
* @param function input {@link Function}
* @param <T>
* @return Wrapped {@link Function}
* @see TtlUnwrap#unwrap(Object)
* @since 2.12.4
*/
@Nullable
public static <T, R> Function<T, R> wrapFunction(@Nullable Function<T, R> function) {
return wrap(function);
}

/**
* wrap {@link BiFunction} to TTL warapper.
*
* @param biFunction input {@link BiFunction}
* @param <T>
* @return Wrapped {@link BiFunction}
* @see TtlUnwrap#unwrap(Object)
* @since 2.12.4
*/
@Nullable
public static <T, U, R> BiFunction<T, U, R> wrapBiFunction(@Nullable BiFunction<T, U, R> biFunction) {
return wrap(biFunction);
}

/**
* wrap input {@link Supplier} to TTL wrapper.
*
* @param supplier input {@link Supplier}
* @return Wrapped {@link Supplier}
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
* @deprecated please use {@link TtlWrappers#wrapSupplier(java.util.function.Supplier)}
*/
@Nullable
@Deprecated
public static <T> Supplier<T> wrap(@Nullable Supplier<T> supplier) {
if (supplier == null) return null;
else if (supplier instanceof TtlEnhanced) return supplier;
Expand Down Expand Up @@ -95,8 +167,10 @@ public String toString() {
* @return Wrapped {@link Consumer}
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
* @deprecated please use {@link TtlWrappers#wrapConsumer(java.util.function.Consumer)}
*/
@Nullable
@Deprecated
public static <T> Consumer<T> wrap(@Nullable Consumer<T> consumer) {
if (consumer == null) return null;
else if (consumer instanceof TtlEnhanced) return consumer;
Expand Down Expand Up @@ -157,8 +231,10 @@ public String toString() {
* @return Wrapped {@link BiConsumer}
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
* @deprecated please use {@link TtlWrappers#wrapBiConsumer(java.util.function.BiConsumer)}
*/
@Nullable
@Deprecated
public static <T, U> BiConsumer<T, U> wrap(@Nullable BiConsumer<T, U> consumer) {
if (consumer == null) return null;
else if (consumer instanceof TtlEnhanced) return consumer;
Expand Down Expand Up @@ -219,8 +295,10 @@ public String toString() {
* @return Wrapped {@link Function}
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
* @deprecated please use {@link TtlWrappers#wrapFunction(java.util.function.Function)}
*/
@Nullable
@Deprecated
public static <T, R> Function<T, R> wrap(@Nullable Function<T, R> fn) {
if (fn == null) return null;
else if (fn instanceof TtlEnhanced) return fn;
Expand Down Expand Up @@ -281,8 +359,10 @@ public String toString() {
* @return Wrapped {@link BiFunction}
* @see TtlUnwrap#unwrap(Object)
* @since 2.11.4
* @deprecated please use {@link TtlWrappers#wrapBiFunction(java.util.function.BiFunction)}
*/
@Nullable
@Deprecated
public static <T, U, R> BiFunction<T, U, R> wrap(@Nullable BiFunction<T, U, R> fn) {
if (fn == null) return null;
else if (fn instanceof TtlEnhanced) return fn;
Expand Down

0 comments on commit c3e04b7

Please sign in to comment.