Skip to content

Commit db69a08

Browse files
committed
Avoid java.util.Optional signatures for simple field access
Issue: SPR-15576
1 parent ce5e2b9 commit db69a08

File tree

62 files changed

+469
-551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+469
-551
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.core;
1818

19-
import java.util.Optional;
2019
import java.util.function.Function;
2120

2221
import org.reactivestreams.Publisher;
@@ -106,7 +105,6 @@ public boolean isNoValue() {
106105
*/
107106
@SuppressWarnings("unchecked")
108107
public <T> Publisher<T> toPublisher(Object source) {
109-
source = (source instanceof Optional ? ((Optional<?>) source).orElse(null) : source);
110108
if (source == null) {
111109
source = getDescriptor().getEmptyValue();
112110
}

spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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.mock.http.server.reactive;
1718

1819
import java.net.InetSocketAddress;
@@ -22,7 +23,6 @@
2223
import java.nio.charset.StandardCharsets;
2324
import java.util.Arrays;
2425
import java.util.List;
25-
import java.util.Optional;
2626

2727
import org.reactivestreams.Publisher;
2828
import reactor.core.publisher.Flux;
@@ -89,8 +89,8 @@ public String getContextPath() {
8989
}
9090

9191
@Override
92-
public Optional<InetSocketAddress> getRemoteAddress() {
93-
return Optional.ofNullable(this.remoteAddress);
92+
public InetSocketAddress getRemoteAddress() {
93+
return this.remoteAddress;
9494
}
9595

9696
@Override

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22-
import java.util.Optional;
2322
import java.util.function.Consumer;
2423

2524
import org.springframework.context.ApplicationContext;
@@ -199,8 +198,8 @@ public void addFormatters(FormatterRegistry registry) {
199198
}
200199

201200
@Override
202-
public Optional<Validator> getValidator() {
203-
return Optional.ofNullable(this.validator);
201+
public Validator getValidator() {
202+
return this.validator;
204203
}
205204

206205
@Override

spring-web/src/main/java/org/springframework/http/ResponseCookie.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
package org.springframework.http;
1818

1919
import java.time.Duration;
20-
import java.util.Optional;
2120

2221
import org.springframework.util.Assert;
2322
import org.springframework.util.ObjectUtils;
2423

2524
/**
26-
* An {@code HttpCookie} sub-class with the additional attributes allowed in
25+
* An {@code HttpCookie} subclass with the additional attributes allowed in
2726
* the "Set-Cookie" response header. To build an instance use the {@link #from}
2827
* static method.
2928
*
@@ -35,9 +34,9 @@ public final class ResponseCookie extends HttpCookie {
3534

3635
private final Duration maxAge;
3736

38-
private final Optional<String> domain;
37+
private final String domain;
3938

40-
private final Optional<String> path;
39+
private final String path;
4140

4241
private final boolean secure;
4342

@@ -53,8 +52,8 @@ private ResponseCookie(String name, String value, Duration maxAge, String domain
5352
super(name, value);
5453
Assert.notNull(maxAge, "Max age must not be null");
5554
this.maxAge = maxAge;
56-
this.domain = Optional.ofNullable(domain);
57-
this.path = Optional.ofNullable(path);
55+
this.domain = domain;
56+
this.path = path;
5857
this.secure = secure;
5958
this.httpOnly = httpOnly;
6059
}
@@ -72,16 +71,16 @@ public Duration getMaxAge() {
7271
}
7372

7473
/**
75-
* Return the cookie "Domain" attribute.
74+
* Return the cookie "Domain" attribute, or {@code null} if not set.
7675
*/
77-
public Optional<String> getDomain() {
76+
public String getDomain() {
7877
return this.domain;
7978
}
8079

8180
/**
82-
* Return the cookie "Path" attribute.
81+
* Return the cookie "Path" attribute, or {@code null} if not set.
8382
*/
84-
public Optional<String> getPath() {
83+
public String getPath() {
8584
return this.path;
8685
}
8786

spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,21 +17,21 @@
1717
package org.springframework.http.codec;
1818

1919
import java.time.Duration;
20-
import java.util.Optional;
2120

2221
import org.springframework.http.codec.json.Jackson2JsonEncoder;
2322

2423
/**
25-
* Representation for a Server-Sent Event for use with Spring's reactive Web
26-
* support. {@code Flux<ServerSentEvent>} or {@code Observable<ServerSentEvent>} is the
24+
* Representation for a Server-Sent Event for use with Spring's reactive Web support.
25+
* {@code Flux<ServerSentEvent>} or {@code Observable<ServerSentEvent>} is the
2726
* reactive equivalent to Spring MVC's {@code SseEmitter}.
2827
*
2928
* @param <T> the type of data that this event contains
29+
*
3030
* @author Sebastien Deleuze
3131
* @author Arjen Poutsma
32+
* @since 5.0
3233
* @see ServerSentEventHttpMessageWriter
3334
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
34-
* @since 5.0
3535
*/
3636
public class ServerSentEvent<T> {
3737

@@ -54,68 +54,69 @@ private ServerSentEvent(String id, String event, T data, Duration retry, String
5454
this.comment = comment;
5555
}
5656

57-
/**
58-
* Return a builder for a {@code SseEvent}.
59-
*
60-
* @param <T> the type of data that this event contains
61-
* @return the builder
62-
*/
63-
public static <T> Builder<T> builder() {
64-
return new BuilderImpl<>();
65-
}
66-
67-
/**
68-
* Return a builder for a {@code SseEvent}, populated with the give {@linkplain #data() data}.
69-
*
70-
* @param <T> the type of data that this event contains
71-
* @return the builder
72-
*/
73-
public static <T> Builder<T> builder(T data) {
74-
return new BuilderImpl<>(data);
75-
}
7657

7758
/**
7859
* Return the {@code id} field of this event, if available.
7960
*/
80-
public Optional<String> id() {
81-
return Optional.ofNullable(this.id);
61+
public String id() {
62+
return this.id;
8263
}
8364

8465
/**
8566
* Return the {@code event} field of this event, if available.
8667
*/
87-
public Optional<String> event() {
88-
return Optional.ofNullable(this.event);
68+
public String event() {
69+
return this.event;
8970
}
9071

9172
/**
9273
* Return the {@code data} field of this event, if available.
9374
*/
94-
public Optional<T> data() {
95-
return Optional.ofNullable(this.data);
75+
public T data() {
76+
return this.data;
9677
}
9778

9879
/**
9980
* Return the {@code retry} field of this event, if available.
10081
*/
101-
public Optional<Duration> retry() {
102-
return Optional.ofNullable(this.retry);
82+
public Duration retry() {
83+
return this.retry;
10384
}
10485

10586
/**
10687
* Return the comment of this event, if available.
10788
*/
108-
public Optional<String> comment() {
109-
return Optional.ofNullable(this.comment);
89+
public String comment() {
90+
return this.comment;
11091
}
11192

93+
11294
@Override
11395
public String toString() {
114-
return "ServerSentEvent [id = '" + id + '\'' + ", event='" + event + '\'' +
115-
", data=" + data + ", retry=" + retry + ", comment='" + comment + '\'' +
116-
']';
96+
return ("ServerSentEvent [id = '" + this.id + '\'' + ", event='" + this.event + '\'' +
97+
", data=" + this.data + ", retry=" + this.retry + ", comment='" + this.comment + '\'' + ']');
11798
}
11899

100+
101+
/**
102+
* Return a builder for a {@code SseEvent}.
103+
* @param <T> the type of data that this event contains
104+
* @return the builder
105+
*/
106+
public static <T> Builder<T> builder() {
107+
return new BuilderImpl<>();
108+
}
109+
110+
/**
111+
* Return a builder for a {@code SseEvent}, populated with the give {@linkplain #data() data}.
112+
* @param <T> the type of data that this event contains
113+
* @return the builder
114+
*/
115+
public static <T> Builder<T> builder(T data) {
116+
return new BuilderImpl<>(data);
117+
}
118+
119+
119120
/**
120121
* A mutable builder for a {@code SseEvent}.
121122
*

spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,16 +62,17 @@ public ServerSentEventHttpMessageWriter() {
6262
}
6363

6464
/**
65-
* Constructor with JSON {@code Encoder} for encoding objects. Support for
66-
* {@code String} event data is built-in.
65+
* Constructor with JSON {@code Encoder} for encoding objects.
66+
* Support for {@code String} event data is built-in.
67+
* @param encoder the Encoder to use (may be {@code null})
6768
*/
6869
public ServerSentEventHttpMessageWriter(Encoder<?> encoder) {
6970
this.encoder = encoder;
7071
}
7172

7273

7374
/**
74-
* Return the configured {@code Encoder}, possibly {@code null}.
75+
* Return the configured {@code Encoder}, if any.
7576
*/
7677
public Encoder<?> getEncoder() {
7778
return this.encoder;
@@ -85,8 +86,8 @@ public List<MediaType> getWritableMediaTypes() {
8586

8687
@Override
8788
public boolean canWrite(ResolvableType elementType, MediaType mediaType) {
88-
return mediaType == null || MediaType.TEXT_EVENT_STREAM.includes(mediaType) ||
89-
ServerSentEvent.class.isAssignableFrom(elementType.resolve(Object.class));
89+
return (mediaType == null || MediaType.TEXT_EVENT_STREAM.includes(mediaType) ||
90+
ServerSentEvent.class.isAssignableFrom(elementType.resolve(Object.class)));
9091
}
9192

9293
@Override
@@ -100,23 +101,33 @@ public Mono<Void> write(Publisher<?> input, ResolvableType elementType, MediaTyp
100101
private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, DataBufferFactory factory,
101102
ResolvableType elementType, Map<String, Object> hints) {
102103

103-
ResolvableType valueType = ServerSentEvent.class.isAssignableFrom(elementType.getRawClass()) ?
104-
elementType.getGeneric(0) : elementType;
104+
ResolvableType valueType = (ServerSentEvent.class.isAssignableFrom(elementType.getRawClass()) ?
105+
elementType.getGeneric() : elementType);
105106

106107
return Flux.from(input).map(element -> {
107108

108-
ServerSentEvent<?> sse = element instanceof ServerSentEvent ?
109-
(ServerSentEvent<?>) element : ServerSentEvent.builder().data(element).build();
109+
ServerSentEvent<?> sse = (element instanceof ServerSentEvent ?
110+
(ServerSentEvent<?>) element : ServerSentEvent.builder().data(element).build());
110111

111112
StringBuilder sb = new StringBuilder();
112-
sse.id().ifPresent(v -> writeField("id", v, sb));
113-
sse.event().ifPresent(v -> writeField("event", v, sb));
114-
sse.retry().ifPresent(v -> writeField("retry", v.toMillis(), sb));
115-
sse.comment().ifPresent(v -> sb.append(':').append(v.replaceAll("\\n", "\n:")).append("\n"));
116-
sse.data().ifPresent(v -> sb.append("data:"));
113+
if (sse.id() != null) {
114+
writeField("id", sse.id(), sb);
115+
}
116+
if (sse.event() != null) {
117+
writeField("event", sse.event(), sb);
118+
}
119+
if (sse.retry() != null) {
120+
writeField("retry", sse.retry().toMillis(), sb);
121+
}
122+
if (sse.comment() != null) {
123+
sb.append(':').append(sse.comment().replaceAll("\\n", "\n:")).append("\n");
124+
}
125+
if (sse.data() != null) {
126+
sb.append("data:");
127+
}
117128

118129
return Flux.concat(encodeText(sb, factory),
119-
encodeData(sse, valueType, factory, hints),
130+
encodeData(sse.data(), valueType, factory, hints),
120131
encodeText("\n", factory));
121132
});
122133
}
@@ -129,10 +140,9 @@ private void writeField(String fieldName, Object fieldValue, StringBuilder strin
129140
}
130141

131142
@SuppressWarnings("unchecked")
132-
private <T> Flux<DataBuffer> encodeData(ServerSentEvent<?> event, ResolvableType valueType,
143+
private <T> Flux<DataBuffer> encodeData(T data, ResolvableType valueType,
133144
DataBufferFactory factory, Map<String, Object> hints) {
134145

135-
Object data = event.data().orElse(null);
136146
if (data == null) {
137147
return Flux.empty();
138148
}
@@ -147,7 +157,7 @@ private <T> Flux<DataBuffer> encodeData(ServerSentEvent<?> event, ResolvableType
147157
}
148158

149159
return ((Encoder<T>) this.encoder)
150-
.encode(Mono.just((T) data), factory, valueType, MediaType.TEXT_EVENT_STREAM, hints)
160+
.encode(Mono.just(data), factory, valueType, MediaType.TEXT_EVENT_STREAM, hints)
151161
.concatWith(encodeText("\n", factory));
152162
}
153163

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.net.InetSocketAddress;
2020
import java.net.URI;
2121
import java.net.URISyntaxException;
22-
import java.util.Optional;
2322

2423
import io.netty.handler.codec.http.cookie.Cookie;
2524
import reactor.core.publisher.Flux;
@@ -100,8 +99,8 @@ protected MultiValueMap<String, HttpCookie> initCookies() {
10099
}
101100

102101
@Override
103-
public Optional<InetSocketAddress> getRemoteAddress() {
104-
return Optional.ofNullable(this.request.remoteAddress());
102+
public InetSocketAddress getRemoteAddress() {
103+
return this.request.remoteAddress();
105104
}
106105

107106
@Override

0 commit comments

Comments
 (0)