-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Java): embrace Java functional interface and Lambda
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
1 parent
aafe4b2
commit d3c55c3
Showing
11 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/Cache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...arter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/ExceptionHandling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../src/main/java/com/jmsoftware/maf/springcloudstarter/function/ExceptionReturnDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...oud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/FunctionLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/Retry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...oftware/maf/springcloudstarter/function/functionalinterface/ThrowExceptionBiFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
20 changes: 20 additions & 0 deletions
20
...msoftware/maf/springcloudstarter/function/functionalinterface/ThrowExceptionFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
18 changes: 18 additions & 0 deletions
18
...msoftware/maf/springcloudstarter/function/functionalinterface/ThrowExceptionRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
19 changes: 19 additions & 0 deletions
19
...msoftware/maf/springcloudstarter/function/functionalinterface/ThrowExceptionSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters