From 1915ef81caded6462bf2f52c05f9c73dc725d2ec Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 8 Jan 2024 16:41:13 +0100 Subject: [PATCH] Use computeIfAbsent with a protected method, make reset protected Signed-off-by: Guillaume Nodet --- .../org/slf4j/simple/SimpleLoggerFactory.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/slf4j-simple/src/main/java/org/slf4j/simple/SimpleLoggerFactory.java b/slf4j-simple/src/main/java/org/slf4j/simple/SimpleLoggerFactory.java index 8b45447cf..7df0eddf9 100644 --- a/slf4j-simple/src/main/java/org/slf4j/simple/SimpleLoggerFactory.java +++ b/slf4j-simple/src/main/java/org/slf4j/simple/SimpleLoggerFactory.java @@ -47,16 +47,19 @@ public SimpleLoggerFactory() { /** * Return an appropriate {@link SimpleLogger} instance by name. + * + * This method will call {@link #createLogger(String)} if the logger + * has not been created yet. */ public Logger getLogger(String name) { - Logger simpleLogger = loggerMap.get(name); - if (simpleLogger != null) { - return simpleLogger; - } else { - Logger newInstance = new SimpleLogger(name); - Logger oldInstance = loggerMap.putIfAbsent(name, newInstance); - return oldInstance == null ? newInstance : oldInstance; - } + return loggerMap.computeIfAbsent(name, this::createLogger); + } + + /** + * Actually creates the logger for the given name. + */ + protected Logger createLogger(String name) { + return new SimpleLogger(name); } /** @@ -68,7 +71,7 @@ public Logger getLogger(String name) { * * You are strongly discouraged from calling this method in production code. */ - void reset() { + protected void reset() { loggerMap.clear(); } }