Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add factory method to create a NamedThreadLocal with an initial value #24705

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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 Down 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<Map<String, Object>>("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();
}
}

}