Skip to content

Commit

Permalink
feat($Java): embrace Java functional interface and Lambda
Browse files Browse the repository at this point in the history
Add more functions to simplify boilerplate code
 - Cache
 - ExceptionHandling
 - ExceptionReturnDefault
 - FunctionLog
 - Retry

BREAKING CHANGE: Add more functions to simplify boilerplate code; give Java function superpower 🔥

[skip ci]
  • Loading branch information
johnnymillergh committed Sep 28, 2021
1 parent aafe4b2 commit d3c55c3
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import static java.util.Objects.nonNull;

/**
* Description: BooleanCheckUtil, change description here.
* Description: BooleanCheck, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/27/2021 12:03 PM
**/
public class BooleanCheckUtil {
public class BooleanCheck {
/**
* Require true or else throw exception.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.jmsoftware.maf.springcloudstarter.function;

import java.util.Map;
import java.util.function.Function;

/**
* <h1>Cache</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:51 AM
* @see <a href='https://juejin.cn/post/6892298625058078727#heading-2'>Java 函数式编程最佳实践 - 赋予函数缓存能力</a>
**/
public class Cache {
public static <T, R> R cacheFunction(Function<T, R> function, T t, Map<T, R> cache) {
R r = cache.get(t);
if (r != null) {
return r;
}
R result = function.apply(t);
cache.put(t, result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.jmsoftware.maf.springcloudstarter.function;

import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionBiFunction;
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionFunction;
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionSupplier;

import java.util.function.Function;

/**
* <h1>ExceptionHandling</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:54 AM
* @see <a href='https://juejin.cn/post/6892298625058078727#heading-4'>Java 函数式编程最佳实践 - 赋予函数处理异常的能力</a>
**/
public class ExceptionHandling {
public static <T, R> R computeAndDealException(ThrowExceptionFunction<T, R> function, T t,
Function<Exception, R> handlingFunc) {
try {
return function.apply(t);
} catch (Exception e) {
return handlingFunc.apply(e);
}
}

public static <T, U, R> R computeAndDealException(ThrowExceptionBiFunction<T, U, R> function, T t, U u,
Function<Exception, R> handlingFunc) {
try {
return function.apply(t, u);
} catch (Exception e) {
return handlingFunc.apply(e);
}
}

public static <R> R computeAndDealException(ThrowExceptionSupplier<R> supplier,
Function<Exception, R> handlingFunc) {
try {
return supplier.get();
} catch (Exception e) {
return handlingFunc.apply(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jmsoftware.maf.springcloudstarter.function;

import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionFunction;
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionSupplier;

/**
* <h1>ExceptionReturnDefault</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:52 AM
* @see <a href='https://juejin.cn/post/6892298625058078727#heading-3'>Java 函数式编程最佳实践 - 赋予函数报错返回默认值能力</a>
**/
public class ExceptionReturnDefault {
public static <T, R> R computeOrGetDefault(ThrowExceptionFunction<T, R> function, T t, R r) {
try {
return function.apply(t);
} catch (Exception e) {
return r;
}
}

public static <R> R computeOrGetDefault(ThrowExceptionSupplier<R> supplier, R r) {
try {
return supplier.get();
} catch (Exception e) {
return r;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jmsoftware.maf.springcloudstarter.function;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Function;

import static com.jmsoftware.maf.common.constant.UniversalDateTime.DATE_TIME_FORMAT;

/**
* <h1>FunctionLog</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:57 AM
* @see <a href='https://juejin.cn/post/6892298625058078727#heading-5'>Java 函数式编程最佳实践 - 赋予函数记录日志能力</a>
**/
@Slf4j
public class FunctionLog {
public static <T, R> R logFunction(Function<T, R> function, T t, String tag) {
long startTime = System.currentTimeMillis();
log.info("[{}], parameter = {}, requestTime = {}", tag, t.toString(),
LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
R apply = function.apply(t);
long endTime = System.currentTimeMillis();
log.info("[{}], return = {}, elapsed time = {} ms", tag, apply.toString(), endTime - startTime);
return apply;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jmsoftware.maf.springcloudstarter.function;

import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionBiFunction;
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionFunction;
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionRunnable;

/**
* <h1>Retry</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:27 AM
* @see <a href='https://juejin.cn/post/6892298625058078727#heading-1'>Java 函数式编程最佳实践 - 赋予方法重试能力</a>
*/
public class Retry {
public static void retryFunction(ThrowExceptionRunnable runnable, int time) {
while (true) {
try {
runnable.run();
return;
} catch (Exception e) {
time--;
if (time <= 0) {
throw new RuntimeException(e);
}
}
}
}

public static <T, R> R retryFunction(ThrowExceptionFunction<T, R> function, T t, int time) {
while (true) {
try {
return function.apply(t);
} catch (Exception e) {
time--;
if (time <= 0) {
throw new RuntimeException(e);
}
}
}
}

public static <T, U, R> R retryFunction(ThrowExceptionBiFunction<T, U, R> function, T t, U u, int time) {
while (true) {
try {
return function.apply(t, u);
} catch (Exception e) {
time--;
if (time <= 0) {
throw new RuntimeException(e);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.jmsoftware.maf.springcloudstarter.function.functionalinterface;

/**
* <h1>ThrowExceptionBiFunction</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:48 AM
**/
@FunctionalInterface
public interface ThrowExceptionBiFunction<T, U, R> {
/**
* Apply r.
*
* @param t the t
* @param u the u
* @return the r
* @throws Exception the exception
*/
R apply(T t, U u) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jmsoftware.maf.springcloudstarter.function.functionalinterface;

/**
* <h1>ThrowExceptionFunction</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:47 AM
**/
@FunctionalInterface
public interface ThrowExceptionFunction<T, R> {
/**
* Apply r.
*
* @param t the t
* @return the r
* @throws Exception the exception
*/
R apply(T t) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jmsoftware.maf.springcloudstarter.function.functionalinterface;

/**
* <h1>ThrowExceptionRunnable</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:49 AM
**/
@FunctionalInterface
public interface ThrowExceptionRunnable {
/**
* Run.
*
* @throws Exception the exception
*/
void run() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jmsoftware.maf.springcloudstarter.function.functionalinterface;

/**
* <h1>ThrowExceptionSupplier</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/28/21 8:49 AM
**/
@FunctionalInterface
public interface ThrowExceptionSupplier<T> {
/**
* Get t.
*
* @return the t
* @throws Exception the exception
*/
T get() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static com.jmsoftware.maf.springcloudstarter.function.BooleanCheckUtil.requireTrue;
import static com.jmsoftware.maf.springcloudstarter.function.BooleanCheck.requireTrue;

/**
* Description: QuartzJobConfigurationServiceImpl
Expand Down

0 comments on commit d3c55c3

Please sign in to comment.