Skip to content

Commit 246e497

Browse files
quaffjhoeller
authored andcommitted
Polishing Optional usage
1 parent 4e5723c commit 246e497

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static class OptionalAutowiredMethodConfig {
277277

278278
@Bean
279279
public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours) {
280-
if (!colour.isPresent() && !colours.isPresent()) {
280+
if (colour.isEmpty() && colours.isEmpty()) {
281281
return new TestBean("");
282282
}
283283
else {

spring-core/src/testFixtures/java/org/springframework/core/testfixture/TestGroupsCondition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -44,7 +44,7 @@ class TestGroupsCondition implements ExecutionCondition {
4444
@Override
4545
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
4646
Optional<EnabledForTestGroups> optional = findAnnotation(context.getElement(), EnabledForTestGroups.class);
47-
if (!optional.isPresent()) {
47+
if (optional.isEmpty()) {
4848
return ENABLED_BY_DEFAULT;
4949
}
5050
TestGroup[] testGroups = optional.get().value();

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/AbstractExpressionEvaluatingCondition.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -89,8 +89,7 @@ protected <A extends Annotation> ConditionEvaluationResult evaluateAnnotation(Cl
8989
Function<A, String> expressionExtractor, Function<A, String> reasonExtractor,
9090
Function<A, Boolean> loadContextExtractor, boolean enabledOnTrue, ExtensionContext context) {
9191

92-
Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
93-
AnnotatedElement element = context.getElement().get();
92+
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
9493
Optional<A> annotation = findMergedAnnotation(element, annotationType);
9594

9695
if (annotation.isEmpty()) {
@@ -152,8 +151,7 @@ protected <A extends Annotation> ConditionEvaluationResult evaluateAnnotation(Cl
152151
private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext,
153152
Class<A> annotationType, ExtensionContext context) {
154153

155-
Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
156-
AnnotatedElement element = context.getElement().get();
154+
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
157155
GenericApplicationContext gac = null;
158156
ApplicationContext applicationContext;
159157

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -283,7 +283,7 @@ else if (resolvableType.resolve() == Resource.class) {
283283
.filter(partWriter -> partWriter.canWrite(finalBodyType, contentType))
284284
.findFirst();
285285

286-
if (!writer.isPresent()) {
286+
if (writer.isEmpty()) {
287287
return Flux.error(new CodecException("No suitable writer found for part: " + name));
288288
}
289289

0 commit comments

Comments
 (0)