Skip to content

Commit

Permalink
Add factory to create a NamedThreadLocal with an initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqimiao authored and snicoll committed Aug 23, 2023
1 parent b2a86cc commit e1d0176
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,7 @@ public class SimpleThreadScope implements Scope {

private static final Log logger = LogFactory.getLog(SimpleThreadScope.class);

private final ThreadLocal<Map<String, Object>> threadScope =
new NamedThreadLocal<>("SimpleThreadScope") {
@Override
protected Map<String, Object> initialValue() {
return new HashMap<>();
}
};

private final ThreadLocal<Map<String, Object>> threadScope = NamedThreadLocal.withInitial("SimpleThreadScope", HashMap::new);

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
Expand All @@ -16,13 +16,17 @@

package org.springframework.core;

import java.util.Objects;
import java.util.function.Supplier;

import org.springframework.util.Assert;

/**
* {@link ThreadLocal} subclass that exposes a specified name
* as {@link #toString()} result (allowing for introspection).
*
* @author Juergen Hoeller
* @author Qimiao Chen
* @since 2.5.2
* @param <T> the value type
* @see NamedInheritableThreadLocal
Expand All @@ -41,9 +45,44 @@ public NamedThreadLocal(String name) {
this.name = name;
}

/**
* 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 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
*/
public static <S> ThreadLocal<S> withInitial(String name, Supplier<? extends S> supplier) {
return new SuppliedNamedThreadLocal<>(name, supplier);
}

@Override
public String toString() {
return this.name;
}


/**
* An extension of NamedThreadLocal that obtains its initial value from
* the specified {@code Supplier}.
* @param <T> the type of the named thread local's value
*/
static final class SuppliedNamedThreadLocal<T> extends NamedThreadLocal<T> {

private final Supplier<? extends T> supplier;

SuppliedNamedThreadLocal(String name, Supplier<? extends T> supplier) {
super(name);
this.supplier = Objects.requireNonNull(supplier);
}

@Override
protected T initialValue() {
return this.supplier.get();
}
}

}

0 comments on commit e1d0176

Please sign in to comment.