Skip to content

Commit d224616

Browse files
philwebbrstoyanchev
authored andcommitted
Polish GroupsMetadata.Registration
Unify `Registration` and `DefualtRegistration` into a single class since they are both package-private and the interface isn't really needed.
1 parent abbee1a commit d224616

File tree

3 files changed

+31
-51
lines changed

3 files changed

+31
-51
lines changed

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

+20-39
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
*/
4242
final class GroupsMetadata {
4343

44-
private final Map<String, DefaultRegistration> groupMap;
44+
private final Map<String, Registration> groupMap;
4545

4646
public GroupsMetadata() {
4747
this(Collections.emptyList());
4848
}
4949

50-
GroupsMetadata(Iterable<DefaultRegistration> registrations) {
50+
GroupsMetadata(Iterable<Registration> registrations) {
5151
this.groupMap = new LinkedHashMap<>();
5252
registrations.forEach(registration -> this.groupMap.put(registration.name(), registration));
5353
}
@@ -58,7 +58,7 @@ public GroupsMetadata() {
5858
* types after checking they don't conflict.
5959
*/
6060
public Registration getOrCreateGroup(String groupName, HttpServiceGroup.ClientType clientType) {
61-
return this.groupMap.computeIfAbsent(groupName, name -> new DefaultRegistration(name, clientType))
61+
return this.groupMap.computeIfAbsent(groupName, name -> new Registration(name, clientType))
6262
.clientType(clientType);
6363
}
6464

@@ -94,71 +94,51 @@ public Collection<HttpServiceGroup> groups(@Nullable ClassLoader classLoader) {
9494
/**
9595
* Return the raw {@link DefaultRegistration registrations}.
9696
*/
97-
Stream<DefaultRegistration> registrations() {
97+
Stream<Registration> registrations() {
9898
return this.groupMap.values().stream();
9999
}
100100

101101

102102
/**
103103
* Registration metadata for an {@link HttpServiceGroup}.
104104
*/
105-
interface Registration {
106-
107-
String name();
108-
109-
HttpServiceGroup.ClientType clientType();
110-
111-
Set<String> httpServiceTypeNames();
112-
}
113-
114-
115-
/**
116-
* Default implementation of {@link Registration}.
117-
*/
118-
static class DefaultRegistration implements Registration {
105+
static class Registration {
119106

120107
private final String name;
121108

122109
private HttpServiceGroup.ClientType clientType;
123110

124-
private final Set<String> typeNames;
111+
private final Set<String> httpServiceTypeNames;
125112

126-
DefaultRegistration(String name, HttpServiceGroup.ClientType clientType) {
113+
Registration(String name, HttpServiceGroup.ClientType clientType) {
127114
this(name, clientType, new LinkedHashSet<>());
128115
}
129116

130-
DefaultRegistration(String name, HttpServiceGroup.ClientType clientType, Set<String> typeNames) {
117+
Registration(String name, HttpServiceGroup.ClientType clientType, Set<String> httpServiceTypeNames) {
131118
this.name = name;
132119
this.clientType = clientType;
133-
this.typeNames = typeNames;
120+
this.httpServiceTypeNames = httpServiceTypeNames;
134121
}
135122

136-
@Override
137-
public String name() {
123+
String name() {
138124
return this.name;
139125
}
140126

141-
@Override
142-
public HttpServiceGroup.ClientType clientType() {
127+
HttpServiceGroup.ClientType clientType() {
143128
return this.clientType;
144129
}
145130

146-
@Override
147-
public Set<String> httpServiceTypeNames() {
148-
return this.typeNames;
131+
Set<String> httpServiceTypeNames() {
132+
return this.httpServiceTypeNames;
149133
}
150134

151135
/**
152136
* Update the client type if it does not conflict with the existing value.
153137
*/
154-
public DefaultRegistration clientType(HttpServiceGroup.ClientType other) {
155-
if (this.clientType.isUnspecified()) {
156-
this.clientType = other;
157-
}
158-
else {
159-
Assert.isTrue(this.clientType == other || other.isUnspecified(),
160-
"ClientType conflict for HttpServiceGroup '" + this.name + "'");
161-
}
138+
public Registration clientType(HttpServiceGroup.ClientType other) {
139+
this.clientType = (this.clientType.isUnspecified() ? other : this.clientType);
140+
Assert.isTrue(this.clientType == other || other.isUnspecified(),
141+
"ClientType conflict for HttpServiceGroup '" + this.name + "'");
162142
return this;
163143
}
164144

@@ -168,15 +148,16 @@ public DefaultRegistration clientType(HttpServiceGroup.ClientType other) {
168148
public HttpServiceGroup toHttpServiceGroup(@Nullable ClassLoader classLoader) {
169149
return new RegisteredGroup(this.name,
170150
(this.clientType.isUnspecified() ? HttpServiceGroup.ClientType.REST_CLIENT : this.clientType),
171-
this.typeNames.stream()
151+
this.httpServiceTypeNames.stream()
172152
.map(typeName -> ClassUtils.resolveClassName(typeName, classLoader))
173153
.collect(Collectors.toSet()));
174154
}
175155

176156
@Override
177157
public String toString() {
178-
return "Group '" + this.name + "', ClientType." + this.clientType + ", " + this.typeNames;
158+
return "Group '" + this.name + "', ClientType." + this.clientType + ", " + this.httpServiceTypeNames;
179159
}
160+
180161
}
181162

182163

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
3030
import org.springframework.aot.generate.ValueCodeGenerator;
3131
import org.springframework.javapoet.CodeBlock;
32-
import org.springframework.web.service.registry.GroupsMetadata.DefaultRegistration;
32+
import org.springframework.web.service.registry.GroupsMetadata.Registration;
3333

3434
/**
3535
* {@link ValueCodeGenerator.Delegate} for {@link GroupsMetadata}.
@@ -41,7 +41,7 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate {
4141

4242
@Override
4343
public @Nullable CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
44-
if (value instanceof DefaultRegistration registration) {
44+
if (value instanceof Registration registration) {
4545
return generateRegistrationCode(valueCodeGenerator, registration);
4646
}
4747
if (value instanceof GroupsMetadata groupsMetadata) {
@@ -51,9 +51,9 @@ final class GroupsMetadataValueDelegate implements ValueCodeGenerator.Delegate {
5151
}
5252

5353
public CodeBlock generateRegistrationCode(ValueCodeGenerator
54-
valueCodeGenerator, DefaultRegistration value) {
54+
valueCodeGenerator, Registration value) {
5555
CodeBlock.Builder code = CodeBlock.builder();
56-
code.add("new $T($S, $L, $L)", DefaultRegistration.class, value.name(),
56+
code.add("new $T($S, $L, $L)", Registration.class, value.name(),
5757
valueCodeGenerator.generateCode(value.clientType()),
5858
!value.httpServiceTypeNames().isEmpty() ?
5959
valueCodeGenerator.generateCode(value.httpServiceTypeNames()) :
@@ -62,7 +62,7 @@ public CodeBlock generateRegistrationCode(ValueCodeGenerator
6262
}
6363

6464
private CodeBlock generateGroupsMetadataCode(ValueCodeGenerator valueCodeGenerator, GroupsMetadata groupsMetadata) {
65-
Collection<DefaultRegistration> registrations = groupsMetadata.registrations()
65+
Collection<Registration> registrations = groupsMetadata.registrations()
6666
.collect(Collectors.toCollection(ArrayList::new));
6767
if (valueCodeGenerator.getGeneratedMethods() != null) {
6868
return valueCodeGenerator.getGeneratedMethods().add("getGroupsMetadata", method -> method
@@ -77,11 +77,11 @@ private CodeBlock generateGroupsMetadataCode(ValueCodeGenerator valueCodeGenerat
7777
}
7878

7979
private CodeBlock generateGroupsMetadataMethod(
80-
ValueCodeGenerator valueCodeGenerator, Collection<DefaultRegistration> registrations) {
80+
ValueCodeGenerator valueCodeGenerator, Collection<Registration> registrations) {
8181

8282
CodeBlock.Builder code = CodeBlock.builder();
8383
String registrationsVariable = "registrations";
84-
code.addStatement("$T<$T> $L = new $T<>()", List.class, DefaultRegistration.class,
84+
code.addStatement("$T<$T> $L = new $T<>()", List.class, Registration.class,
8585
registrationsVariable, ArrayList.class);
8686
registrations.forEach(registration ->
8787
code.addStatement("$L.add($L)", registrationsVariable,

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.javapoet.CodeBlock;
4040
import org.springframework.javapoet.MethodSpec;
4141
import org.springframework.util.ReflectionUtils;
42-
import org.springframework.web.service.registry.GroupsMetadata.DefaultRegistration;
4342
import org.springframework.web.service.registry.GroupsMetadata.Registration;
4443
import org.springframework.web.service.registry.HttpServiceGroup.ClientType;
4544
import org.springframework.web.service.registry.echo.EchoA;
@@ -59,21 +58,21 @@ class GroupsMetadataValueDelegateTests {
5958

6059
@Test
6160
void generateRegistrationWithOnlyName() {
62-
DefaultRegistration registration = new DefaultRegistration("test", ClientType.UNSPECIFIED);
61+
Registration registration = new Registration("test", ClientType.UNSPECIFIED);
6362
compile(registration, (instance, compiled) -> assertThat(instance)
6463
.isInstanceOfSatisfying(Registration.class, hasRegistration("test", ClientType.UNSPECIFIED)));
6564
}
6665

6766
@Test
6867
void generateRegistrationWitNoHttpServiceTypeName() {
69-
DefaultRegistration registration = new DefaultRegistration("test", ClientType.REST_CLIENT);
68+
Registration registration = new Registration("test", ClientType.REST_CLIENT);
7069
compile(registration, (instance, compiled) -> assertThat(instance)
7170
.isInstanceOfSatisfying(Registration.class, hasRegistration("test", ClientType.REST_CLIENT)));
7271
}
7372

7473
@Test
7574
void generateRegistrationWitOneHttpServiceTypeName() {
76-
DefaultRegistration registration = new DefaultRegistration("test", ClientType.WEB_CLIENT,
75+
Registration registration = new Registration("test", ClientType.WEB_CLIENT,
7776
httpServiceTypeNames("com.example.MyClient"));
7877
compile(registration, (instance, compiled) -> assertThat(instance)
7978
.isInstanceOfSatisfying(Registration.class, hasRegistration(
@@ -82,7 +81,7 @@ void generateRegistrationWitOneHttpServiceTypeName() {
8281

8382
@Test
8483
void generateRegistrationWitHttpServiceTypeNames() {
85-
DefaultRegistration registration = new DefaultRegistration("test", ClientType.WEB_CLIENT,
84+
Registration registration = new Registration("test", ClientType.WEB_CLIENT,
8685
httpServiceTypeNames("com.example.MyClient", "com.example.another.TestClient"));
8786
compile(registration, (instance, compiled) -> assertThat(instance)
8887
.isInstanceOfSatisfying(Registration.class, hasRegistration(

0 commit comments

Comments
 (0)