Polish "Add factory to create a NamedThreadLocal with an initial value"

See gh-24705
This commit is contained in:
Stephane Nicoll
2023-08-23 18:47:06 +02:00
parent e1d0176faa
commit 5878a0741e
3 changed files with 10 additions and 14 deletions

View File

@@ -136,13 +136,8 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
private final XmlValidationModeDetector validationModeDetector = new XmlValidationModeDetector();
private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded =
new NamedThreadLocal<>("XML bean definition resources currently being loaded"){
@Override
protected Set<EncodedResource> initialValue() {
return new HashSet<>(4);
}
};
private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded = NamedThreadLocal.withInitial(
"XML bean definition resources currently being loaded", () -> new HashSet<>(4));
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,7 +55,8 @@ public class SimpleThreadScope implements Scope {
private static final Log logger = LogFactory.getLog(SimpleThreadScope.class);
private final ThreadLocal<Map<String, Object>> threadScope = NamedThreadLocal.withInitial("SimpleThreadScope", HashMap::new);
private final ThreadLocal<Map<String, Object>> threadScope = NamedThreadLocal.withInitial(
"SimpleThreadScope", HashMap::new);
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,12 +48,11 @@ public class NamedThreadLocal<T> extends ThreadLocal<T> {
/**
* Creates a named thread local variable. The initial value of the variable is
* determined by invoking the {@code get} method on the {@code Supplier}.
*
* @param <S> the type of the named thread local's value
* @param name a descriptive name for the thread local
* @param supplier the supplier to be used to determine the initial value
* @return a new named thread local variable
* @throws NullPointerException if the specified supplier is null
* @since 5.2.5
* @return a new named thread local
* @since 6.1
*/
public static <S> ThreadLocal<S> withInitial(String name, Supplier<? extends S> supplier) {
return new SuppliedNamedThreadLocal<>(name, supplier);
@@ -83,6 +82,7 @@ public class NamedThreadLocal<T> extends ThreadLocal<T> {
protected T initialValue() {
return this.supplier.get();
}
}
}