Skip to content

Provide computeIfAbsent-like method on ObjectProvider [SPR-14980] #19546

@spring-projects-issues

Description

@spring-projects-issues

Andy Wilkinson opened SPR-14980 and commented

When using ObjectProvider with constructor injection, I quite often find myself doing something like this:

public class FooConfiguration {

	private final List<FooCustomizer> fooCustomizers;

	public FooConfiguration(ObjectProvider<List<FooCustomizer>> fooCustomizersProvider) {
		List<FooCustomizer> providedCustomizers = fooCustomizersProvider.getIfAvailable();
		this.fooCustomizers = providedCustomizers == null ? Collections.emptyList()
				: providedCustomizers;
	}

}

I'd quite like to be able to avoid the temporary storage in providedCustomizers and do something like this instead:

public FooConfiguration(ObjectProvider<List<FooCustomizer>> fooCustomizersProvider) {
	this.fooCustomizers = fooCustomizersProvider.computeIfAbsent(() -> {
		return Collections.emptyList();
	});
}

Affects: 5.0 M3

Issue Links:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions