Skip to content

Commit

Permalink
Use computeIfAbsent with a protected method, make reset protected
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Nodet <gnodet@gmail.com>
  • Loading branch information
gnodet authored and ceki committed Jan 8, 2024
1 parent 31a3a8d commit 1915ef8
Showing 1 changed file with 12 additions and 9 deletions.
Expand Up @@ -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);
}

/**
Expand All @@ -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();
}
}

0 comments on commit 1915ef8

Please sign in to comment.