Polishing Optional usage

This commit is contained in:
Yanming Zhou
2024-03-11 15:58:31 +08:00
committed by Juergen Hoeller
parent 4e5723ca9e
commit 246e4977a2
4 changed files with 8 additions and 10 deletions

View File

@@ -277,7 +277,7 @@ class AutowiredConfigurationTests {
@Bean
public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours) {
if (!colour.isPresent() && !colours.isPresent()) {
if (colour.isEmpty() && colours.isEmpty()) {
return new TestBean("");
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ class TestGroupsCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<EnabledForTestGroups> optional = findAnnotation(context.getElement(), EnabledForTestGroups.class);
if (!optional.isPresent()) {
if (optional.isEmpty()) {
return ENABLED_BY_DEFAULT;
}
TestGroup[] testGroups = optional.get().value();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,8 +89,7 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
Function<A, String> expressionExtractor, Function<A, String> reasonExtractor,
Function<A, Boolean> loadContextExtractor, boolean enabledOnTrue, ExtensionContext context) {
Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
AnnotatedElement element = context.getElement().get();
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
Optional<A> annotation = findMergedAnnotation(element, annotationType);
if (annotation.isEmpty()) {
@@ -152,8 +151,7 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext,
Class<A> annotationType, ExtensionContext context) {
Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
AnnotatedElement element = context.getElement().get();
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
GenericApplicationContext gac = null;
ApplicationContext applicationContext;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -283,7 +283,7 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport
.filter(partWriter -> partWriter.canWrite(finalBodyType, contentType))
.findFirst();
if (!writer.isPresent()) {
if (writer.isEmpty()) {
return Flux.error(new CodecException("No suitable writer found for part: " + name));
}