Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
277 additions
and
33 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
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
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,32 @@ | ||
|
||
package info.freelibrary.util; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* A consumer that logs any exceptions. This is not an ideal solution (as the PMD suppressions indicate), but it | ||
* provides a generic way to handle exceptions thrown in lambdas. | ||
* | ||
* @param <T> The type that the function accepts | ||
*/ | ||
@FunctionalInterface | ||
public interface LoggingConsumer<T> extends Consumer<T> { | ||
|
||
@Override | ||
@SuppressWarnings("PMD.AvoidCatchingGenericException") | ||
default void accept(final T aType) { | ||
try { | ||
acceptLogs(aType); | ||
} catch (final Exception details) { | ||
LoggerFactory.getLogger(LoggingConsumer.class, MessageCodes.BUNDLE).error(details, details.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* A method that logs any exceptions thrown by the consumer. | ||
* | ||
* @param aType A type being accepted by the consumer | ||
*/ | ||
void acceptLogs(T aType); | ||
|
||
} |
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,34 @@ | ||
|
||
package info.freelibrary.util; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* A consumer that throws a runtime exception. This is not an ideal solution (as the PMD suppressions indicate), but it | ||
* provides a generic way to handle exceptions thrown in lambdas. | ||
* | ||
* @param <T> The type that the function accepts | ||
*/ | ||
@FunctionalInterface | ||
public interface ThrowingConsumer<T> extends Consumer<T> { | ||
|
||
@Override | ||
@SuppressWarnings("PMD.AvoidCatchingGenericException") | ||
default void accept(final T aType) { | ||
try { | ||
acceptThrows(aType); | ||
} catch (final Exception details) { | ||
throw new I18nRuntimeException(details); | ||
} | ||
} | ||
|
||
/** | ||
* A method that wraps any exceptions through by the consumer with a runtime exception. | ||
* | ||
* @param aType A type being accepted by the consumer | ||
* @throws Exception An exception thrown by the consumer | ||
*/ | ||
@SuppressWarnings("PMD.SignatureDeclareThrowsException") | ||
void acceptThrows(T aType) 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
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
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
Oops, something went wrong.