Skip to content

Commit c87ee59

Browse files
committed
[java] Using Optional to check for nullity is over-engineering
1 parent eb37b63 commit c87ee59

File tree

1 file changed

+36
-52
lines changed
  • java/client/src/org/openqa/selenium/remote/tracing

1 file changed

+36
-52
lines changed

java/client/src/org/openqa/selenium/remote/tracing/Tags.java

+36-52
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.StringWriter;
2626
import java.util.AbstractMap.SimpleEntry;
2727
import java.util.Map;
28-
import java.util.Optional;
2928
import java.util.function.BiConsumer;
3029
import java.util.stream.Collectors;
3130
import java.util.stream.Stream;
@@ -73,76 +72,61 @@ private Tags() {
7372
};
7473

7574
public static final BiConsumer<Map<String, EventAttributeValue>, HttpRequest>
76-
HTTP_REQUEST_EVENT =
77-
(map, req) -> {
78-
map.put(AttributeKey.HTTP_METHOD.getKey(),
79-
EventAttribute.setValue(req.getMethod().toString()));
75+
HTTP_REQUEST_EVENT = (map, req) -> {
76+
map.put(AttributeKey.HTTP_METHOD.getKey(), EventAttribute.setValue(req.getMethod().toString()));
8077
map.put(AttributeKey.HTTP_TARGET.getKey(), EventAttribute.setValue(req.getUri()));
8178

82-
Optional<String> userAgent = Optional.ofNullable(req.getHeader(HttpHeaders.USER_AGENT));
83-
if (userAgent.isPresent()) {
84-
map.put(AttributeKey.HTTP_USER_AGENT.getKey(),
85-
EventAttribute.setValue(userAgent.get()));
79+
String userAgent = req.getHeader(HttpHeaders.USER_AGENT);
80+
if (userAgent != null) {
81+
map.put(AttributeKey.HTTP_USER_AGENT.getKey(), EventAttribute.setValue(userAgent));
8682
}
8783

88-
Optional<String> host = Optional.ofNullable(req.getHeader(HttpHeaders.HOST));
89-
if (host.isPresent()) {
90-
map.put(AttributeKey.HTTP_HOST.getKey(),
91-
EventAttribute.setValue(host.get()));
84+
String host = req.getHeader(HttpHeaders.HOST);
85+
if (host != null) {
86+
map.put(AttributeKey.HTTP_HOST.getKey(), EventAttribute.setValue(host));
9287
}
9388

94-
Optional<String> contentLength =
95-
Optional.ofNullable(req.getHeader(HttpHeaders.CONTENT_LENGTH));
96-
if (contentLength.isPresent()) {
89+
String contentLength = req.getHeader(HttpHeaders.CONTENT_LENGTH);
90+
if (contentLength != null) {
9791
map.put(AttributeKey.HTTP_REQUEST_CONTENT_LENGTH.getKey(),
98-
EventAttribute.setValue(contentLength.get()));
92+
EventAttribute.setValue(contentLength));
9993
}
10094

101-
Optional<String> clientIpAddress =
102-
Optional.ofNullable(req.getHeader(HttpHeaders.X_FORWARDED_FOR));
103-
if (clientIpAddress.isPresent()) {
104-
map.put(AttributeKey.HTTP_CLIENT_IP.getKey(),
105-
EventAttribute.setValue(clientIpAddress.get()));
95+
String clientIpAddress = req.getHeader(HttpHeaders.X_FORWARDED_FOR);
96+
if (clientIpAddress != null) {
97+
map.put(AttributeKey.HTTP_CLIENT_IP.getKey(), EventAttribute.setValue(clientIpAddress));
10698
}
10799

108-
Optional<String> httpScheme =
109-
Optional.ofNullable((String) req.getAttribute(AttributeKey.HTTP_SCHEME.getKey()));
110-
if (httpScheme.isPresent()) {
111-
map.put(AttributeKey.HTTP_SCHEME.getKey(),
112-
EventAttribute.setValue(httpScheme.get()));
100+
Object httpScheme = req.getAttribute(AttributeKey.HTTP_SCHEME.getKey());
101+
if (httpScheme != null) {
102+
map.put(AttributeKey.HTTP_SCHEME.getKey(), EventAttribute.setValue((String) httpScheme));
113103
}
114104

115-
Optional<Integer> httpVersion =
116-
Optional.ofNullable((Integer) req.getAttribute(AttributeKey.HTTP_FLAVOR.getKey()));
117-
if (httpVersion.isPresent()) {
118-
map.put(AttributeKey.HTTP_FLAVOR.getKey(),
119-
EventAttribute.setValue(httpVersion.get()));
105+
Object httpVersion = req.getAttribute(AttributeKey.HTTP_FLAVOR.getKey());
106+
if (httpVersion != null) {
107+
map.put(AttributeKey.HTTP_FLAVOR.getKey(), EventAttribute.setValue((Integer) httpVersion));
120108
}
121-
122109
};
123110

124111
public static final BiConsumer<Map<String, EventAttributeValue>, HttpResponse>
125-
HTTP_RESPONSE_EVENT =
126-
(map, res) -> {
127-
int statusCode = res.getStatus();
128-
if (res.getTargetHost() != null) {
129-
map.put(AttributeKey.HTTP_TARGET_HOST.getKey(),
130-
EventAttribute.setValue(res.getTargetHost()));
131-
}
132-
map.put(AttributeKey.HTTP_STATUS_CODE.getKey(), EventAttribute.setValue(statusCode));
133-
};
112+
HTTP_RESPONSE_EVENT = (map, res) -> {
113+
int statusCode = res.getStatus();
114+
if (res.getTargetHost() != null) {
115+
map.put(AttributeKey.HTTP_TARGET_HOST.getKey(),
116+
EventAttribute.setValue(res.getTargetHost()));
117+
}
118+
map.put(AttributeKey.HTTP_STATUS_CODE.getKey(), EventAttribute.setValue(statusCode));
119+
};
134120

135121
public static final BiConsumer<Map<String, EventAttributeValue>, Throwable>
136-
EXCEPTION =
137-
(map, t) -> {
138-
StringWriter sw = new StringWriter();
139-
t.printStackTrace(new PrintWriter(sw));
122+
EXCEPTION = (map, t) -> {
123+
StringWriter sw = new StringWriter();
124+
t.printStackTrace(new PrintWriter(sw));
140125

141-
map.put(AttributeKey.EXCEPTION_TYPE.getKey(),
142-
EventAttribute.setValue(t.getClass().getName()));
143-
map.put(AttributeKey.EXCEPTION_STACKTRACE.getKey(),
144-
EventAttribute.setValue(sw.toString()));
145-
146-
};
126+
map.put(AttributeKey.EXCEPTION_TYPE.getKey(),
127+
EventAttribute.setValue(t.getClass().getName()));
128+
map.put(AttributeKey.EXCEPTION_STACKTRACE.getKey(),
129+
EventAttribute.setValue(sw.toString()));
147130

131+
};
148132
}

0 commit comments

Comments
 (0)