Skip to content

Commit 382a931

Browse files
committed
Polishing
1 parent e59a599 commit 382a931

File tree

4 files changed

+39
-64
lines changed

4 files changed

+39
-64
lines changed

spring-core/src/main/java/org/springframework/core/ReactiveAdapter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.core;
1718

1819
import org.reactivestreams.Publisher;
@@ -22,8 +23,8 @@
2223
/**
2324
* Contract for adapting to and from {@link Flux} and {@link Mono}.
2425
*
25-
* <p>An adapter supports a specific adaptee type whose stream semantics can be
26-
* checked via {@link #getDescriptor()}.
26+
* <p>An adapter supports a specific adaptee type whose stream semantics
27+
* can be checked via {@link #getDescriptor()}.
2728
*
2829
* <p>Use the {@link ReactiveAdapterRegistry} to obtain an adapter for a
2930
* supported adaptee type or to register additional adapters.
@@ -78,14 +79,12 @@ class Descriptor {
7879

7980
private final boolean isNoValue;
8081

81-
8282
public Descriptor(boolean isMultiValue, boolean canBeEmpty, boolean isNoValue) {
8383
this.isMultiValue = isMultiValue;
8484
this.supportsEmpty = canBeEmpty;
8585
this.isNoValue = isNoValue;
8686
}
8787

88-
8988
/**
9089
* Return {@code true} if the adaptee implies 0..N values can be produced
9190
* and is therefore a good fit to adapt to {@link Flux}. A {@code false}
@@ -110,7 +109,6 @@ public boolean supportsEmpty() {
110109
public boolean isNoValue() {
111110
return this.isNoValue;
112111
}
113-
114112
}
115113

116114
}

spring-core/src/main/java/org/springframework/core/ReactiveAdapterRegistry.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.core;
1718

1819
import java.util.LinkedHashMap;
@@ -30,7 +31,6 @@
3031
import rx.Observable;
3132
import rx.Single;
3233

33-
import org.springframework.core.ReactiveAdapter.Descriptor;
3434
import org.springframework.util.ClassUtils;
3535

3636
/**
@@ -48,29 +48,25 @@ public class ReactiveAdapterRegistry {
4848
private static final boolean rxJava1Present =
4949
ClassUtils.isPresent("rx.Observable", ReactiveAdapterRegistry.class.getClassLoader());
5050

51-
52-
private final Map<Class<?>, ReactiveAdapter> adapterMap = new LinkedHashMap<>();
51+
private final Map<Class<?>, ReactiveAdapter> adapterMap = new LinkedHashMap<>(4);
5352

5453

5554
/**
5655
* Create a registry and auto-register default adapters.
5756
*/
5857
public ReactiveAdapterRegistry() {
59-
6058
// Flux and Mono ahead of Publisher...
6159
registerMonoAdapter(Mono.class,
62-
source -> (Mono<?>) source, source -> source, new Descriptor(false, true, false));
63-
60+
source -> (Mono<?>) source, source -> source,
61+
new ReactiveAdapter.Descriptor(false, true, false));
6462
registerFluxAdapter(
6563
Flux.class, source -> (Flux<?>) source, source -> source);
66-
6764
registerFluxAdapter(
6865
Publisher.class, source -> Flux.from((Publisher<?>) source), source -> source);
6966

7067
registerMonoAdapter(CompletableFuture.class,
71-
source -> Mono.fromFuture((CompletableFuture<?>) source),
72-
source -> Mono.from((Publisher<?>) source).toFuture(),
73-
new Descriptor(false, true, false)
68+
source -> Mono.fromFuture((CompletableFuture<?>) source), Mono::toFuture,
69+
new ReactiveAdapter.Descriptor(false, true, false)
7470
);
7571

7672
if (rxJava1Present) {
@@ -84,9 +80,8 @@ public ReactiveAdapterRegistry() {
8480
* functions can assume that input will never be {@code null} and also that
8581
* any {@link Optional} wrapper is unwrapped.
8682
*/
87-
public void registerMonoAdapter(Class<?> adapteeType,
88-
Function<Object, Mono<?>> toAdapter, Function<Mono<?>, Object> fromAdapter,
89-
Descriptor descriptor) {
83+
public void registerMonoAdapter(Class<?> adapteeType, Function<Object, Mono<?>> toAdapter,
84+
Function<Mono<?>, Object> fromAdapter, ReactiveAdapter.Descriptor descriptor) {
9085

9186
this.adapterMap.put(adapteeType, new MonoReactiveAdapter(toAdapter, fromAdapter, descriptor));
9287
}
@@ -96,8 +91,8 @@ public void registerMonoAdapter(Class<?> adapteeType,
9691
* functions can assume that input will never be {@code null} and also that
9792
* any {@link Optional} wrapper is unwrapped.
9893
*/
99-
public void registerFluxAdapter(Class<?> adapteeType,
100-
Function<Object, Flux<?>> toAdapter, Function<Flux<?>, Object> fromAdapter) {
94+
public void registerFluxAdapter(Class<?> adapteeType, Function<Object, Flux<?>> toAdapter,
95+
Function<Flux<?>, Object> fromAdapter) {
10196

10297
this.adapterMap.put(adapteeType, new FluxReactiveAdapter(toAdapter, fromAdapter));
10398
}
@@ -135,25 +130,22 @@ public ReactiveAdapter getAdapterTo(Class<?> adapteeType, Object adaptee) {
135130
return getAdapterInternal(supportedType -> supportedType.equals(actualType));
136131
}
137132

133+
private ReactiveAdapter getAdapterInternal(Predicate<Class<?>> adapteeTypePredicate) {
134+
return this.adapterMap.keySet().stream()
135+
.filter(adapteeTypePredicate)
136+
.map(this.adapterMap::get)
137+
.findFirst()
138+
.orElse(null);
139+
}
140+
138141

139142
private static Class<?> getActualType(Class<?> adapteeType, Object adaptee) {
140143
adaptee = unwrapOptional(adaptee);
141144
return (adaptee != null ? adaptee.getClass() : adapteeType);
142145
}
143146

144147
private static Object unwrapOptional(Object value) {
145-
if (value != null && value instanceof Optional) {
146-
value = ((Optional<?>) value).orElse(null);
147-
}
148-
return value;
149-
}
150-
151-
private ReactiveAdapter getAdapterInternal(Predicate<Class<?>> adapteeTypePredicate) {
152-
return this.adapterMap.keySet().stream()
153-
.filter(adapteeTypePredicate)
154-
.map(this.adapterMap::get)
155-
.findFirst()
156-
.orElse(null);
148+
return (value instanceof Optional ? ((Optional<?>) value).orElse(null) : value);
157149
}
158150

159151

@@ -256,25 +248,23 @@ public Object fromPublisher(Publisher<?> source) {
256248
}
257249
}
258250

251+
259252
private static class RxJava1AdapterRegistrar {
260253

261254
public void register(ReactiveAdapterRegistry registry) {
262-
263255
registry.registerFluxAdapter(Observable.class,
264256
source -> RxJava1Adapter.observableToFlux((Observable<?>) source),
265257
RxJava1Adapter::publisherToObservable
266258
);
267-
268259
registry.registerMonoAdapter(Single.class,
269260
source -> RxJava1Adapter.singleToMono((Single<?>) source),
270261
RxJava1Adapter::publisherToSingle,
271-
new Descriptor(false, false, false)
262+
new ReactiveAdapter.Descriptor(false, false, false)
272263
);
273-
274264
registry.registerMonoAdapter(Completable.class,
275265
source -> RxJava1Adapter.completableToMono((Completable) source),
276266
RxJava1Adapter::publisherToCompletable,
277-
new Descriptor(false, true, true)
267+
new ReactiveAdapter.Descriptor(false, true, true)
278268
);
279269
}
280270
}

spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
import org.springframework.web.context.request.NativeWebRequest;
3131
import org.springframework.web.context.request.ServletWebRequest;
3232

33-
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.*;
3434

3535
/**
3636
* Test fixture for {@link ContentNegotiationManagerFactoryBean} tests.
37+
*
3738
* @author Rossen Stoyanchev
3839
*/
3940
public class ContentNegotiationManagerFactoryBeanTests {
@@ -119,9 +120,7 @@ public void favorPathWithJafTurnedOff() throws Exception {
119120
assertEquals(Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
120121
}
121122

122-
// SPR-10170
123-
124-
@Test(expected = HttpMediaTypeNotAcceptableException.class)
123+
@Test(expected = HttpMediaTypeNotAcceptableException.class) // SPR-10170
125124
public void favorPathWithIgnoreUnknownPathExtensionTurnedOff() throws Exception {
126125
this.factoryBean.setFavorPathExtension(true);
127126
this.factoryBean.setIgnoreUnknownPathExtensions(false);
@@ -152,9 +151,7 @@ public void favorParameter() throws Exception {
152151
manager.resolveMediaTypes(this.webRequest));
153152
}
154153

155-
// SPR-10170
156-
157-
@Test(expected = HttpMediaTypeNotAcceptableException.class)
154+
@Test(expected = HttpMediaTypeNotAcceptableException.class) // SPR-10170
158155
public void favorParameterWithUnknownMediaType() throws HttpMediaTypeNotAcceptableException {
159156
this.factoryBean.setFavorParameter(true);
160157
this.factoryBean.afterPropertiesSet();
@@ -188,16 +185,12 @@ public void setDefaultContentType() throws Exception {
188185
manager.resolveMediaTypes(this.webRequest));
189186

190187
// SPR-10513
191-
192188
this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE);
193-
194189
assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON),
195190
manager.resolveMediaTypes(this.webRequest));
196191
}
197192

198-
// SPR-12286
199-
200-
@Test
193+
@Test // SPR-12286
201194
public void setDefaultContentTypeWithStrategy() throws Exception {
202195
this.factoryBean.setDefaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
203196
this.factoryBean.afterPropertiesSet();
@@ -216,7 +209,6 @@ private static class TestServletContext extends MockServletContext {
216209

217210
private final Map<String, String> mimeTypes = new HashMap<>();
218211

219-
220212
public Map<String, String> getMimeTypes() {
221213
return this.mimeTypes;
222214
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
6565
}
6666
catch (NoSuchMethodException ex) {
6767
// Should never happen
68-
throw new IllegalStateException("No handler for HTTP OPTIONS", ex);
68+
throw new IllegalStateException("Failed to retrieve internal handler method for HTTP OPTIONS", ex);
6969
}
7070
}
7171

@@ -183,7 +183,6 @@ private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
183183
/**
184184
* Iterate all RequestMappingInfo's once again, look if any match by URL at
185185
* least and raise exceptions according to what doesn't match.
186-
*
187186
* @throws HttpRequestMethodNotSupportedException if there are matches by URL
188187
* but not by HTTP method
189188
* @throws HttpMediaTypeNotAcceptableException if there are matches by URL
@@ -243,7 +242,6 @@ private static class PartialMatchHelper {
243242

244243
private final List<PartialMatch> partialMatches = new ArrayList<>();
245244

246-
247245
public PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
248246
for (RequestMappingInfo info : infos) {
249247
if (info.getPatternsCondition().getMatchingCondition(request) != null) {
@@ -252,7 +250,6 @@ public PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest requ
252250
}
253251
}
254252

255-
256253
/**
257254
* Whether there any partial matches.
258255
*/
@@ -387,20 +384,18 @@ private static class PartialMatch {
387384

388385
private final boolean paramsMatch;
389386

390-
391387
/**
392388
* @param info RequestMappingInfo that matches the URL path.
393389
* @param request the current request
394390
*/
395391
public PartialMatch(RequestMappingInfo info, HttpServletRequest request) {
396392
this.info = info;
397-
this.methodsMatch = info.getMethodsCondition().getMatchingCondition(request) != null;
398-
this.consumesMatch = info.getConsumesCondition().getMatchingCondition(request) != null;
399-
this.producesMatch = info.getProducesCondition().getMatchingCondition(request) != null;
400-
this.paramsMatch = info.getParamsCondition().getMatchingCondition(request) != null;
393+
this.methodsMatch = (info.getMethodsCondition().getMatchingCondition(request) != null);
394+
this.consumesMatch = (info.getConsumesCondition().getMatchingCondition(request) != null);
395+
this.producesMatch = (info.getProducesCondition().getMatchingCondition(request) != null);
396+
this.paramsMatch = (info.getParamsCondition().getMatchingCondition(request) != null);
401397
}
402398

403-
404399
public RequestMappingInfo getInfo() {
405400
return this.info;
406401
}
@@ -410,15 +405,15 @@ public boolean hasMethodsMatch() {
410405
}
411406

412407
public boolean hasConsumesMatch() {
413-
return hasMethodsMatch() && this.consumesMatch;
408+
return (hasMethodsMatch() && this.consumesMatch);
414409
}
415410

416411
public boolean hasProducesMatch() {
417-
return hasConsumesMatch() && this.producesMatch;
412+
return (hasConsumesMatch() && this.producesMatch);
418413
}
419414

420415
public boolean hasParamsMatch() {
421-
return hasProducesMatch() && this.paramsMatch;
416+
return (hasProducesMatch() && this.paramsMatch);
422417
}
423418

424419
@Override
@@ -428,14 +423,14 @@ public String toString() {
428423
}
429424
}
430425

426+
431427
/**
432428
* Default handler for HTTP OPTIONS.
433429
*/
434430
private static class HttpOptionsHandler {
435431

436432
private final HttpHeaders headers = new HttpHeaders();
437433

438-
439434
public HttpOptionsHandler(Set<String> declaredMethods) {
440435
this.headers.setAllow(initAllowedHttpMethods(declaredMethods));
441436
}

0 commit comments

Comments
 (0)