Polish GroupsMetadata.Registration

Unify `Registration` and `DefualtRegistration` into a single
class since they are both package-private and the interface
isn't really needed.
This commit is contained in:
Phillip Webb
2025-04-29 19:52:06 -07:00
committed by rstoyanchev
parent abbee1a050
commit d2246162c3
3 changed files with 31 additions and 51 deletions

View File

@@ -41,13 +41,13 @@ import org.springframework.util.ClassUtils;
*/
final class GroupsMetadata {
private final Map<String, DefaultRegistration> groupMap;
private final Map<String, Registration> groupMap;
public GroupsMetadata() {
this(Collections.emptyList());
}
GroupsMetadata(Iterable<DefaultRegistration> registrations) {
GroupsMetadata(Iterable<Registration> registrations) {
this.groupMap = new LinkedHashMap<>();
registrations.forEach(registration -> this.groupMap.put(registration.name(), registration));
}
@@ -58,7 +58,7 @@ final class GroupsMetadata {
* types after checking they don't conflict.
*/
public Registration getOrCreateGroup(String groupName, HttpServiceGroup.ClientType clientType) {
return this.groupMap.computeIfAbsent(groupName, name -> new DefaultRegistration(name, clientType))
return this.groupMap.computeIfAbsent(groupName, name -> new Registration(name, clientType))
.clientType(clientType);
}
@@ -94,7 +94,7 @@ final class GroupsMetadata {
/**
* Return the raw {@link DefaultRegistration registrations}.
*/
Stream<DefaultRegistration> registrations() {
Stream<Registration> registrations() {
return this.groupMap.values().stream();
}
@@ -102,63 +102,43 @@ final class GroupsMetadata {
/**
* Registration metadata for an {@link HttpServiceGroup}.
*/
interface Registration {
String name();
HttpServiceGroup.ClientType clientType();
Set<String> httpServiceTypeNames();
}
/**
* Default implementation of {@link Registration}.
*/
static class DefaultRegistration implements Registration {
static class Registration {
private final String name;
private HttpServiceGroup.ClientType clientType;
private final Set<String> typeNames;
private final Set<String> httpServiceTypeNames;
DefaultRegistration(String name, HttpServiceGroup.ClientType clientType) {
Registration(String name, HttpServiceGroup.ClientType clientType) {
this(name, clientType, new LinkedHashSet<>());
}
DefaultRegistration(String name, HttpServiceGroup.ClientType clientType, Set<String> typeNames) {
Registration(String name, HttpServiceGroup.ClientType clientType, Set<String> httpServiceTypeNames) {
this.name = name;
this.clientType = clientType;
this.typeNames = typeNames;
this.httpServiceTypeNames = httpServiceTypeNames;
}
@Override
public String name() {
String name() {
return this.name;
}
@Override
public HttpServiceGroup.ClientType clientType() {
HttpServiceGroup.ClientType clientType() {
return this.clientType;
}
@Override
public Set<String> httpServiceTypeNames() {
return this.typeNames;
Set<String> httpServiceTypeNames() {
return this.httpServiceTypeNames;
}
/**
* Update the client type if it does not conflict with the existing value.
*/
public DefaultRegistration clientType(HttpServiceGroup.ClientType other) {
if (this.clientType.isUnspecified()) {
this.clientType = other;
}
else {
Assert.isTrue(this.clientType == other || other.isUnspecified(),
"ClientType conflict for HttpServiceGroup '" + this.name + "'");
}
public Registration clientType(HttpServiceGroup.ClientType other) {
this.clientType = (this.clientType.isUnspecified() ? other : this.clientType);
Assert.isTrue(this.clientType == other || other.isUnspecified(),
"ClientType conflict for HttpServiceGroup '" + this.name + "'");
return this;
}
@@ -168,15 +148,16 @@ final class GroupsMetadata {
public HttpServiceGroup toHttpServiceGroup(@Nullable ClassLoader classLoader) {
return new RegisteredGroup(this.name,
(this.clientType.isUnspecified() ? HttpServiceGroup.ClientType.REST_CLIENT : this.clientType),
this.typeNames.stream()
this.httpServiceTypeNames.stream()
.map(typeName -> ClassUtils.resolveClassName(typeName, classLoader))
.collect(Collectors.toSet()));
}
@Override
public String toString() {
return "Group '" + this.name + "', ClientType." + this.clientType + ", " + this.typeNames;
return "Group '" + this.name + "', ClientType." + this.clientType + ", " + this.httpServiceTypeNames;
}
}

View File

@@ -29,7 +29,7 @@ import org.jspecify.annotations.Nullable;
import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
import org.springframework.aot.generate.ValueCodeGenerator;
import org.springframework.javapoet.CodeBlock;
import org.springframework.web.service.registry.GroupsMetadata.DefaultRegistration;
import org.springframework.web.service.registry.GroupsMetadata.Registration;
/**
* {@link ValueCodeGenerator.Delegate} for {@link GroupsMetadata}.
@@ -41,7 +41,7 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate {
@Override
public @Nullable CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof DefaultRegistration registration) {
if (value instanceof Registration registration) {
return generateRegistrationCode(valueCodeGenerator, registration);
}
if (value instanceof GroupsMetadata groupsMetadata) {
@@ -51,9 +51,9 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate {
}
public CodeBlock generateRegistrationCode(ValueCodeGenerator
valueCodeGenerator, DefaultRegistration value) {
valueCodeGenerator, Registration value) {
CodeBlock.Builder code = CodeBlock.builder();
code.add("new $T($S, $L, $L)", DefaultRegistration.class, value.name(),
code.add("new $T($S, $L, $L)", Registration.class, value.name(),
valueCodeGenerator.generateCode(value.clientType()),
!value.httpServiceTypeNames().isEmpty() ?
valueCodeGenerator.generateCode(value.httpServiceTypeNames()) :
@@ -62,7 +62,7 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate {
}
private CodeBlock generateGroupsMetadataCode(ValueCodeGenerator valueCodeGenerator, GroupsMetadata groupsMetadata) {
Collection<DefaultRegistration> registrations = groupsMetadata.registrations()
Collection<Registration> registrations = groupsMetadata.registrations()
.collect(Collectors.toCollection(ArrayList::new));
if (valueCodeGenerator.getGeneratedMethods() != null) {
return valueCodeGenerator.getGeneratedMethods().add("getGroupsMetadata", method -> method
@@ -77,11 +77,11 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate {
}
private CodeBlock generateGroupsMetadataMethod(
ValueCodeGenerator valueCodeGenerator, Collection<DefaultRegistration> registrations) {
ValueCodeGenerator valueCodeGenerator, Collection<Registration> registrations) {
CodeBlock.Builder code = CodeBlock.builder();
String registrationsVariable = "registrations";
code.addStatement("$T<$T> $L = new $T<>()", List.class, DefaultRegistration.class,
code.addStatement("$T<$T> $L = new $T<>()", List.class, Registration.class,
registrationsVariable, ArrayList.class);
registrations.forEach(registration ->
code.addStatement("$L.add($L)", registrationsVariable,

View File

@@ -39,7 +39,6 @@ import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.MethodSpec;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.service.registry.GroupsMetadata.DefaultRegistration;
import org.springframework.web.service.registry.GroupsMetadata.Registration;
import org.springframework.web.service.registry.HttpServiceGroup.ClientType;
import org.springframework.web.service.registry.echo.EchoA;
@@ -59,21 +58,21 @@ class GroupsMetadataValueDelegateTests {
@Test
void generateRegistrationWithOnlyName() {
DefaultRegistration registration = new DefaultRegistration("test", ClientType.UNSPECIFIED);
Registration registration = new Registration("test", ClientType.UNSPECIFIED);
compile(registration, (instance, compiled) -> assertThat(instance)
.isInstanceOfSatisfying(Registration.class, hasRegistration("test", ClientType.UNSPECIFIED)));
}
@Test
void generateRegistrationWitNoHttpServiceTypeName() {
DefaultRegistration registration = new DefaultRegistration("test", ClientType.REST_CLIENT);
Registration registration = new Registration("test", ClientType.REST_CLIENT);
compile(registration, (instance, compiled) -> assertThat(instance)
.isInstanceOfSatisfying(Registration.class, hasRegistration("test", ClientType.REST_CLIENT)));
}
@Test
void generateRegistrationWitOneHttpServiceTypeName() {
DefaultRegistration registration = new DefaultRegistration("test", ClientType.WEB_CLIENT,
Registration registration = new Registration("test", ClientType.WEB_CLIENT,
httpServiceTypeNames("com.example.MyClient"));
compile(registration, (instance, compiled) -> assertThat(instance)
.isInstanceOfSatisfying(Registration.class, hasRegistration(
@@ -82,7 +81,7 @@ class GroupsMetadataValueDelegateTests {
@Test
void generateRegistrationWitHttpServiceTypeNames() {
DefaultRegistration registration = new DefaultRegistration("test", ClientType.WEB_CLIENT,
Registration registration = new Registration("test", ClientType.WEB_CLIENT,
httpServiceTypeNames("com.example.MyClient", "com.example.another.TestClient"));
compile(registration, (instance, compiled) -> assertThat(instance)
.isInstanceOfSatisfying(Registration.class, hasRegistration(