44import static java .util .Objects .requireNonNull ;
55
66import ch .qos .logback .classic .Level ;
7+ import com .github .benmanes .caffeine .cache .CacheLoader ;
8+ import com .github .benmanes .caffeine .cache .Caffeine ;
9+ import com .github .benmanes .caffeine .cache .LoadingCache ;
710import com .google .common .base .Throwables ;
8- import com .google .common .cache .CacheBuilder ;
9- import com .google .common .cache .CacheLoader ;
10- import com .google .common .cache .LoadingCache ;
1111import com .google .common .collect .HashBasedTable ;
12- import com .google .common .collect .Lists ;
13- import com .google .common .collect .Maps ;
14- import com .google .common .collect .Sets ;
1512import com .google .common .collect .Table ;
13+ import java .io .Serializable ;
14+ import java .util .ArrayList ;
1615import java .util .Collection ;
1716import java .util .Collections ;
1817import java .util .Comparator ;
18+ import java .util .HashMap ;
1919import java .util .HashSet ;
2020import java .util .Map ;
2121import java .util .Optional ;
2222import java .util .Set ;
23- import java .util .concurrent .ExecutionException ;
23+ import java .util .concurrent .CompletionException ;
24+ import java .util .function .Function ;
2425import org .slf4j .Logger ;
2526
2627/**
@@ -41,7 +42,7 @@ public class FilterThresholdSet {
4142
4243 private final FilterThresholdSet _parent ;
4344
44- private final Set <String > _appenders = Sets . newHashSet ();
45+ private final Set <String > _appenders = new HashSet <> ();
4546
4647 private final Set <LoggerName > _roots = new HashSet <>();
4748
@@ -50,26 +51,25 @@ public class FilterThresholdSet {
5051
5152 /* Logger -> (Appender -> Level) */
5253 private final LoadingCache <String , Map <String , Level >> _effectiveMaps =
53- CacheBuilder .newBuilder ().build (CacheLoader . from (
54+ Caffeine .newBuilder ().build (new FunctionToCacheLoader <> (
5455 logger -> computeEffectiveMap (LoggerName .getInstance (logger ))));
5556
5657 /* Logger -> Level */
5758 private final LoadingCache <Logger , Optional <Level >> _effectiveLevels =
58- CacheBuilder .newBuilder ().build (CacheLoader . from (
59+ Caffeine .newBuilder ().build (new FunctionToCacheLoader <> (
5960 logger -> {
6061 try {
6162 Map <String , Level > map = _effectiveMaps .get (logger .getName ());
6263 return map .isEmpty ()
6364 ? Optional .empty ()
6465 : Optional .of (Collections .min (map .values (), LEVEL_ORDER ));
65- } catch (ExecutionException e ) {
66+ } catch (CompletionException e ) {
6667 Throwables .throwIfUnchecked (e .getCause ());
6768 throw new RuntimeException (e .getCause ());
6869 }
6970 }));
7071
71- private static final Comparator <Level > LEVEL_ORDER =
72- (o1 , o2 ) -> Integer .compare (o1 .toInt (), o2 .toInt ());
72+ private static final Comparator <Level > LEVEL_ORDER = Comparator .comparingInt (Level ::toInt );
7373
7474 public FilterThresholdSet () {
7575 this (null );
@@ -93,7 +93,7 @@ public synchronized void addAppender(String name) {
9393 */
9494 public synchronized Collection <String > getAppenders () {
9595 if (_parent == null ) {
96- return Lists . newArrayList (_appenders );
96+ return new ArrayList <> (_appenders );
9797 } else {
9898 Collection <String > appenders = _parent .getAppenders ();
9999 appenders .addAll (_appenders );
@@ -182,7 +182,7 @@ private void clearCache() {
182182 */
183183 public synchronized Map <String , Level > getInheritedMap (LoggerName logger ) {
184184 if (_parent == null ) {
185- return Maps . newHashMap (_rules .row (logger ));
185+ return new HashMap <> (_rules .row (logger ));
186186 } else {
187187 Map <String , Level > map = _parent .getInheritedMap (logger );
188188 map .putAll (_rules .row (logger ));
@@ -222,7 +222,7 @@ public Level getThreshold(LoggerName logger, String appender) {
222222 public Level getThreshold (String logger , String appender ) {
223223 try {
224224 return _effectiveMaps .get (logger ).get (appender );
225- } catch (ExecutionException e ) {
225+ } catch (CompletionException e ) {
226226 Throwables .throwIfUnchecked (e .getCause ());
227227 throw new RuntimeException (e .getCause ());
228228 }
@@ -234,9 +234,24 @@ public Level getThreshold(String logger, String appender) {
234234 public Level getThreshold (Logger logger ) {
235235 try {
236236 return _effectiveLevels .get (logger ).orElse (null );
237- } catch (ExecutionException e ) {
237+ } catch (CompletionException e ) {
238238 Throwables .throwIfUnchecked (e .getCause ());
239239 throw new RuntimeException (e .getCause ());
240240 }
241241 }
242+
243+ private static final class FunctionToCacheLoader <K , V > implements
244+ CacheLoader <K , V >, Serializable {
245+
246+ private final Function <K , V > computingFunction ;
247+ private static final long serialVersionUID = 0L ;
248+
249+ public FunctionToCacheLoader (Function <K , V > computingFunction ) {
250+ this .computingFunction = requireNonNull (computingFunction );
251+ }
252+
253+ public V load (K key ) {
254+ return this .computingFunction .apply (requireNonNull (key ));
255+ }
256+ }
242257}
0 commit comments