Skip to content

Commit 7a11623

Browse files
committed
applyWithMessage() and getWithMessage() special function to SafeFunction
1 parent 418a7bb commit 7a11623

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- applyWithMessage() and getWithMessage() special function to SafeFunction
11+
1012
## [8.4.3] - 2023-10-22
1113

1214
### Added

fj-core/src/main/java/org/fugerit/java/core/function/SafeFunction.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ private SafeFunction() {}
7777
*/
7878
private static final Consumer<Exception> DEFAULT_EX_CONSUMER = EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE;
7979

80+
private static Consumer<Exception> createRethrowWithMessageExHandler( String message ) {
81+
return e -> { throw new ConfigRuntimeException( message+" caused by ("+e+")" , e); };
82+
}
83+
8084
/**
8185
* <p>Get a value returned by an UnsafeSupplier, and convert any raised Exception</p>
8286
*
@@ -109,19 +113,60 @@ public static <T> T get( UnsafeSupplier<T, Exception> supplier ) {
109113
return get( supplier, DEFAULT_EX_CONSUMER );
110114
}
111115

116+
/**
117+
* <p>Return the value of an UnsafeSupplier, only logging (level WARN) any error.</p>
118+
*
119+
* @param <T> the returned type
120+
* @param supplier the {@link UnsafeSupplier} function
121+
* @return the value evaluated by the {@link UnsafeSupplier}
122+
*/
123+
public static <T> T getSilent( UnsafeSupplier<T, Exception> supplier ) {
124+
return get( supplier, EX_CONSUMER_LOG_WARN );
125+
}
126+
127+
/**
128+
* <p>Return the value of an UnsafeSupplier, converting any raised Exception to ConfigRuntimeException, a message will be prefixed to the thrown ConfigRuntimeException.</p>
129+
*
130+
* @param <T> the returned type
131+
* @param supplier the {@link UnsafeSupplier} function
132+
* @param message the message to be prefixed to the thrown ConfigRuntimeException
133+
* @return the value evaluated by the {@link UnsafeSupplier}
134+
* @throws ConfigRuntimeException may throw ConfigRuntimeException in case of exceptions
135+
*/
136+
public static <T> T getWithMessage( UnsafeSupplier<T, Exception> supplier, String message ) {
137+
return get( supplier, createRethrowWithMessageExHandler( message ) );
138+
}
139+
112140
/**
113141
* <p>Apply an UnsafeVoid function, converting any raised Exception to ConfigRuntimeException.</p>
114142
*
115143
* @param fun the {@link UnsafeVoid} function
144+
* @throws ConfigRuntimeException may throw ConfigRuntimeException in case of exceptions
116145
*/
117146
public static void apply( UnsafeVoid<Exception> fun ) {
118147
apply( fun , DEFAULT_EX_CONSUMER );
119148
}
120149

150+
/**
151+
* <p>Apply an UnsafeVoid function, only logging (level WARN) any error.</p>
152+
*
153+
* @param fun the {@link UnsafeVoid} function
154+
*/
121155
public static void applySilent( UnsafeVoid<Exception> fun ) {
122156
apply( fun , EX_CONSUMER_LOG_WARN );
123157
}
124158

159+
/**
160+
* <p>Apply an UnsafeVoid function, converting any raised Exception to ConfigRuntimeExceptiona, message will be prefixed to the thrown ConfigRuntimeException.</p>
161+
*
162+
* @param fun the {@link UnsafeVoid} function
163+
* @param message the message to be prefixed to the thrown ConfigRuntimeException
164+
* @throws ConfigRuntimeException may throw ConfigRuntimeException in case of exceptions
165+
*/
166+
public static void applyWithMessage( UnsafeVoid<Exception> fun, String message ) {
167+
apply( fun , createRethrowWithMessageExHandler( message ) );
168+
}
169+
125170
/**
126171
* <p>Return the value provided by the supplier, handling any exception with a {@link Consumer} function.</p>
127172
*

fj-core/src/test/java/test/org/fugerit/java/core/function/TestSafeFunction.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.fugerit.java.core.db.dao.DAOException;
1212
import org.fugerit.java.core.db.dao.DAORuntimeException;
1313
import org.fugerit.java.core.function.SafeFunction;
14+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
1415
import org.fugerit.java.core.xml.XMLException;
1516
import org.junit.Assert;
1617
import org.junit.Test;
@@ -72,6 +73,34 @@ public void testGetLogGen() {
7273
Assert.assertNull( res );
7374
}
7475

76+
@Test
77+
public void testApplyWithMessage() {
78+
Assert.assertThrows( ConfigRuntimeException.class , () -> SafeFunction.applyWithMessage( () -> { throw new IOException( "exApplyWithMessage" ); }, "error test" ) );
79+
}
80+
81+
@Test
82+
public void testGetWithMessage() {
83+
Assert.assertThrows( ConfigRuntimeException.class , () -> SafeFunction.getWithMessage( () -> { throw new IOException( "exGetWithMessage" ); }, "error test" ) );
84+
}
85+
86+
@Test
87+
public void testApplyWithMessageOk() {
88+
boolean ok = true;
89+
SafeFunction.applyWithMessage( () -> log.info( "ok" ), "no error apply" );
90+
Assert.assertTrue( ok );
91+
}
92+
93+
@Test
94+
public void testGetWithMessageOk() {
95+
Assert.assertEquals( BooleanUtils.BOOLEAN_1 , SafeFunction.getWithMessage( () -> BooleanUtils.BOOLEAN_1, "no error get" ) );
96+
}
97+
98+
@Test
99+
public void testGetSilent() {
100+
String res = SafeFunction.getSilent( () -> { throw new IOException( "exGetSilent" ); } );
101+
Assert.assertNull( res );
102+
}
103+
75104
@Test
76105
public void testGetWithDefaultLogGen() {
77106
String res = SafeFunction.getWithDefault( () -> { throw new IOException( "ex3" ); }, e -> {

0 commit comments

Comments
 (0)