blob: 9ae0f784e57e1659d718ff76e26de16b312d4972 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/*
* Copyright (C) 2023 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.common.util.concurrent;
import com.google.common.annotations.GwtCompatible;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
/** A holder for a {@link Logger} that is initialized only when requested. */
@GwtCompatible
@ElementTypesAreNonnullByDefault
final class LazyLogger {
private final String loggerName;
private volatile @Nullable Logger logger;
LazyLogger(Class<?> ownerOfLogger) {
this.loggerName = ownerOfLogger.getName();
}
Logger get() {
/*
* We use double-checked locking. We could the try racy single-check idiom, but that would
* depend on Logger not contain mutable state.
*
* We could use Suppliers.memoizingSupplier here, but I micro-optimized to this implementation
* to avoid the extra class for the lambda (and maybe more for memoizingSupplier itself) and the
* indirection.
*
* One thing to *avoid* is a change to make each Logger user use memoizingSupplier directly:
* That may introduce an extra class for each lambda (currently a dozen).
*/
Logger local = logger;
if (local != null) {
return local;
}
synchronized (this) {
local = logger;
if (local != null) {
return local;
}
return logger = Logger.getLogger(loggerName);
}
}
}
|