Skip to content

Commit 42e854e

Browse files
philwebbrstoyanchev
authored andcommitted
Polish AbstractHttpServiceRegistrar.DefaultGroupSpec
Remove `DefaultGroupRegistry` since it's not really needed and polish `DefaultGroupSpec` a little.
1 parent d224616 commit 42e854e

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

spring-web/src/main/java/org/springframework/web/service/registry/AbstractHttpServiceRegistrar.java

+35-45
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.web.service.registry;
1818

19+
import java.util.Arrays;
20+
import java.util.Objects;
21+
1922
import org.jspecify.annotations.Nullable;
2023

2124
import org.springframework.beans.BeansException;
@@ -141,7 +144,7 @@ public final void registerBeanDefinitions(
141144
@Override
142145
public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry beanRegistry) {
143146

144-
registerHttpServices(new DefaultGroupRegistry(), metadata);
147+
registerHttpServices(DefaultGroupSpec::new, metadata);
145148

146149
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
147150

@@ -261,58 +264,45 @@ interface GroupSpec {
261264

262265

263266
/**
264-
* Default implementation of {@link GroupRegistry}.
267+
* Default implementation of {@link GroupSpec}.
265268
*/
266-
private class DefaultGroupRegistry implements GroupRegistry {
269+
private class DefaultGroupSpec implements GroupRegistry.GroupSpec {
267270

268-
@Override
269-
public GroupSpec forGroup(String name, HttpServiceGroup.ClientType clientType) {
270-
return new DefaultGroupSpec(name, clientType);
271-
}
271+
private final GroupsMetadata.Registration registration;
272272

273-
/**
274-
* Default implementation of {@link GroupSpec}.
275-
*/
276-
private class DefaultGroupSpec implements GroupSpec {
277-
278-
private final GroupsMetadata.Registration registration;
273+
DefaultGroupSpec(String groupName, HttpServiceGroup.ClientType clientType) {
274+
clientType = (clientType != HttpServiceGroup.ClientType.UNSPECIFIED ? clientType : defaultClientType);
275+
this.registration = groupsMetadata.getOrCreateGroup(groupName, clientType);
276+
}
279277

280-
public DefaultGroupSpec(String groupName, HttpServiceGroup.ClientType clientType) {
281-
clientType = (clientType != HttpServiceGroup.ClientType.UNSPECIFIED ? clientType : defaultClientType);
282-
this.registration = groupsMetadata.getOrCreateGroup(groupName, clientType);
283-
}
278+
@Override
279+
public GroupRegistry.GroupSpec register(Class<?>... serviceTypes) {
280+
Arrays.stream(serviceTypes).map(Class::getName).forEach(this::register);
281+
return this;
282+
}
284283

285-
@Override
286-
public GroupSpec register(Class<?>... serviceTypes) {
287-
for (Class<?> serviceType : serviceTypes) {
288-
this.registration.httpServiceTypeNames().add(serviceType.getName());
289-
}
290-
return this;
291-
}
284+
@Override
285+
public GroupRegistry.GroupSpec detectInBasePackages(Class<?>... packageClasses) {
286+
Arrays.stream(packageClasses).map(Class::getPackageName).forEach(this::detectInBasePackage);
287+
return this;
288+
}
292289

293-
@Override
294-
public GroupSpec detectInBasePackages(Class<?>... packageClasses) {
295-
for (Class<?> packageClass : packageClasses) {
296-
detect(packageClass.getPackageName());
297-
}
298-
return this;
299-
}
290+
@Override
291+
public GroupRegistry.GroupSpec detectInBasePackages(String... packageNames) {
292+
Arrays.stream(packageNames).forEach(this::detectInBasePackage);
293+
return this;
294+
}
300295

301-
@Override
302-
public GroupSpec detectInBasePackages(String... packageNames) {
303-
for (String packageName : packageNames) {
304-
detect(packageName);
305-
}
306-
return this;
307-
}
296+
private void detectInBasePackage(String packageName) {
297+
getScanner().findCandidateComponents(packageName)
298+
.stream()
299+
.map(BeanDefinition::getBeanClassName)
300+
.filter(Objects::nonNull)
301+
.forEach(this::register);
302+
}
308303

309-
private void detect(String packageName) {
310-
for (BeanDefinition definition : getScanner().findCandidateComponents(packageName)) {
311-
if (definition.getBeanClassName() != null) {
312-
this.registration.httpServiceTypeNames().add(definition.getBeanClassName());
313-
}
314-
}
315-
}
304+
private void register(String httpServiceTypeName) {
305+
this.registration.httpServiceTypeNames().add(httpServiceTypeName);
316306
}
317307
}
318308

spring-web/src/test/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrarTests.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,12 @@ public Map<String, StubGroup> groupMap() {
180180

181181
@Override
182182
public GroupSpec forGroup(String name, ClientType clientType) {
183-
return new TestGroupSpec(name, clientType);
183+
return new TestGroupSpec(this.groupMap, name, clientType);
184184
}
185185

186-
private class TestGroupSpec implements GroupSpec {
187186

188-
private final String groupName;
189-
190-
private final ClientType clientType;
191-
192-
public TestGroupSpec(String groupName, ClientType clientType) {
193-
this.groupName = groupName;
194-
this.clientType = clientType;
195-
}
187+
private record TestGroupSpec(Map<String, StubGroup> groupMap, String groupName,
188+
ClientType clientType) implements GroupSpec {
196189

197190
@Override
198191
public GroupSpec register(Class<?>... serviceTypes) {
@@ -213,7 +206,7 @@ public GroupSpec detectInBasePackages(String... packageNames) {
213206
}
214207

215208
private StubGroup getOrCreateGroup() {
216-
return groupMap.computeIfAbsent(this.groupName, name -> new StubGroup(name, this.clientType));
209+
return this.groupMap.computeIfAbsent(this.groupName, name -> new StubGroup(name, this.clientType));
217210
}
218211
}
219212
}

0 commit comments

Comments
 (0)