Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polishing Optional usage #1341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Polishing Optional usage
quaff committed Mar 11, 2024
commit 58bbc0b0406a4530d9a6d70896fc2ea57d80ef6f
Original file line number Diff line number Diff line change
@@ -280,12 +280,7 @@ private <T> void determineConstrainedMethods(AnnotatedType<T> type, BeanDescript
}

boolean needsValidation;
if ( correspondingProperty.isPresent() ) {
needsValidation = isGetterConstrained( beanDescriptor, method, correspondingProperty.get() );
}
else {
needsValidation = isNonGetterConstrained( beanDescriptor, method );
}
needsValidation = correspondingProperty.map( s -> isGetterConstrained( beanDescriptor, method, s ) ).orElseGet( () -> isNonGetterConstrained( beanDescriptor, method ) );

if ( needsValidation ) {
callables.add( annotatedMethod );
Original file line number Diff line number Diff line change
@@ -135,7 +135,7 @@ public PropertyConstraintMappingContext field(String property) {

Optional<JavaBeanField> foundField = javaBeanHelper.findDeclaredField( beanClass, property );

if ( !foundField.isPresent() ) {
if ( foundField.isEmpty() ) {
throw LOG.getUnableToFindPropertyWithAccessException( beanClass, property, ElementType.FIELD );
}

@@ -157,7 +157,7 @@ public PropertyConstraintMappingContext getter(String property) {

Optional<JavaBeanGetter> foundGetter = javaBeanHelper.findDeclaredGetter( beanClass, property );

if ( !foundGetter.isPresent() ) {
if ( foundGetter.isEmpty() ) {
throw LOG.getUnableToFindPropertyWithAccessException( beanClass, property, ElementType.METHOD );
}

@@ -179,7 +179,7 @@ public MethodConstraintMappingContext method(String name, Class<?>... parameterT

Optional<JavaBeanMethod> foundMethod = javaBeanHelper.findDeclaredMethod( beanClass, name, parameterTypes );

if ( !foundMethod.isPresent() ) {
if ( foundMethod.isEmpty() ) {
throw LOG.getBeanDoesNotContainMethodException( beanClass, name, parameterTypes );
}

@@ -203,7 +203,7 @@ public MethodConstraintMappingContext method(String name, Class<?>... parameterT
public ConstructorConstraintMappingContext constructor(Class<?>... parameterTypes) {
Optional<JavaBeanConstructor> foundConstructor = javaBeanHelper.findDeclaredConstructor( beanClass, parameterTypes );

if ( !foundConstructor.isPresent() ) {
if ( foundConstructor.isEmpty() ) {
throw LOG.getBeanDoesNotContainConstructorException(
beanClass,
parameterTypes
Original file line number Diff line number Diff line change
@@ -831,7 +831,7 @@ private <T> void validateParametersInContext(ExecutableValidationContext<T> vali

Optional<ExecutableMetaData> executableMetaDataOptional = validationContext.getExecutableMetaData();

if ( !executableMetaDataOptional.isPresent() ) {
if ( executableMetaDataOptional.isEmpty() ) {
// the method is unconstrained
return;
}
@@ -1015,7 +1015,7 @@ private <V, T> void validateReturnValueInContext(ExecutableValidationContext<T>

Optional<ExecutableMetaData> executableMetaDataOptional = validationContext.getExecutableMetaData();

if ( !executableMetaDataOptional.isPresent() ) {
if ( executableMetaDataOptional.isEmpty() ) {
// the method is unconstrained
return;
}
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ protected void validateConstraints(ValidationContext<?> validationContext,

// We re-evaluate the boolean composition by taking into consideration also the violations
// from the local constraintValidator
if ( !violatedLocalConstraintValidatorContext.isPresent() ) {
if ( violatedLocalConstraintValidatorContext.isEmpty() ) {
compositionResult.setAtLeastOneTrue( true );
}
else {
@@ -168,7 +168,7 @@ private void prepareFinalConstraintViolations(ValidationContext<?> validationCon
// violations or not (or if there is no local ConstraintValidator at all).
// If not we create a violation
// using the error message in the annotation declaration at top level.
if ( !localConstraintValidatorContext.isPresent() ) {
if ( localConstraintValidatorContext.isEmpty() ) {
violatedConstraintValidatorContexts.add(
validationContext.createConstraintValidatorContextFor(
descriptor, valueContext.getPropertyPath()
@@ -185,9 +185,7 @@ private void prepareFinalConstraintViolations(ValidationContext<?> validationCon
// as checked in test CustomErrorMessage.java
// If no violations have been reported from the local ConstraintValidator, or no such validator exists,
// then we just add an empty list.
if ( localConstraintValidatorContext.isPresent() ) {
violatedConstraintValidatorContexts.add( localConstraintValidatorContext.get() );
}
localConstraintValidatorContext.ifPresent( violatedConstraintValidatorContexts::add );
}

/**
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ public Optional<ExecutableMetaData> getExecutableMetaData() {
}

private static boolean buildDisableAlreadyValidatedBeanTracking(Optional<ExecutableMetaData> executableMetaData) {
if ( !executableMetaData.isPresent() ) {
if ( executableMetaData.isEmpty() ) {
// the method is unconstrained so there's no need to worry about the tracking
return false;
}
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ public Optional<ExecutableMetaData> getExecutableMetaData() {
}

private static boolean buildDisableAlreadyValidatedBeanTracking(Optional<ExecutableMetaData> executableMetaData) {
if ( !executableMetaData.isPresent() ) {
if ( executableMetaData.isEmpty() ) {
// the method is unconstrained so there's no need to worry about the tracking
return false;
}
Original file line number Diff line number Diff line change
@@ -24,6 +24,6 @@ private OptionalValueExtractor() {

@Override
public void extractValues(Optional<?> originalValue, ValueExtractor.ValueReceiver receiver) {
receiver.value( null, originalValue.isPresent() ? originalValue.get() : null );
receiver.value( null, originalValue.orElse( null ) );
}
}
Original file line number Diff line number Diff line change
@@ -274,9 +274,7 @@ protected String getAcceptableQName() {
@Override
protected void add(XMLEventReader xmlEventReader, XMLEvent xmlEvent) throws XMLStreamException {
Optional<String> enabledAttribute = readAttribute( xmlEvent.asStartElement(), ENABLED_QNAME );
if ( enabledAttribute.isPresent() ) {
enabled = Boolean.parseBoolean( enabledAttribute.get() );
}
enabledAttribute.ifPresent( s -> enabled = Boolean.parseBoolean( s ) );

while ( !( xmlEvent.isEndElement() && xmlEvent.asEndElement().getName().getLocalPart().equals( EXECUTABLE_VALIDATION_QNAME_LOCAL_PART ) ) ) {
XMLEvent currentEvent = xmlEventReader.nextEvent();
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ abstract class AbstractConstrainedElementStaxBuilder extends AbstractStaxBuilder
protected void add(XMLEventReader xmlEventReader, XMLEvent xmlEvent) throws XMLStreamException {
Optional<QName> mainAttributeValueQname = getMainAttributeValueQname();
if ( mainAttributeValueQname.isPresent() ) {
mainAttributeValue = readAttribute( xmlEvent.asStartElement(), mainAttributeValueQname.get() ).get();
mainAttributeValue = readAttribute( xmlEvent.asStartElement(), mainAttributeValueQname.get() ).orElseThrow();
}
ignoreAnnotations = readAttribute( xmlEvent.asStartElement(), IGNORE_ANNOTATIONS_QNAME ).map( Boolean::parseBoolean );
ConstraintTypeStaxBuilder constraintTypeStaxBuilder = getNewConstraintTypeStaxBuilder();
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ abstract class AbstractConstrainedExecutableElementStaxBuilder extends AbstractS
protected void add(XMLEventReader xmlEventReader, XMLEvent xmlEvent) throws XMLStreamException {
Optional<QName> mainAttributeValueQname = getMainAttributeValueQname();
if ( mainAttributeValueQname.isPresent() ) {
mainAttributeValue = readAttribute( xmlEvent.asStartElement(), mainAttributeValueQname.get() ).get();
mainAttributeValue = readAttribute( xmlEvent.asStartElement(), mainAttributeValueQname.get() ).orElseThrow();
}
ignoreAnnotations = readAttribute( xmlEvent.asStartElement(), IGNORE_ANNOTATIONS_QNAME ).map( Boolean::parseBoolean );
ConstrainedParameterStaxBuilder constrainedParameterStaxBuilder = getNewConstrainedParameterStaxBuilder();
Original file line number Diff line number Diff line change
@@ -102,12 +102,7 @@ ConstrainedType build(Class<?> beanClass) {
.collect( Collectors.toSet() );

// ignore annotation
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreClassLevelConstraintAnnotations(
beanClass,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreClassLevelConstraintAnnotations( beanClass, b ) );

return new ConstrainedType(
ConfigurationSource.XML,
Original file line number Diff line number Diff line change
@@ -76,12 +76,7 @@ ConstrainedExecutable build(JavaBeanHelper javaBeanHelper, Class<?> beanClass, L
}

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsOnMember(
javaBeanConstructor,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsOnMember( javaBeanConstructor, b ) );

List<ConstrainedParameter> constrainedParameters = CollectionHelper.newArrayList( constrainedParameterStaxBuilders.size() );
for ( int index = 0; index < constrainedParameterStaxBuilders.size(); index++ ) {
Original file line number Diff line number Diff line change
@@ -82,12 +82,7 @@ ConstrainedField build(JavaBeanHelper javaBeanHelper, Class<?> beanClass, List<S
);

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsOnMember(
javaBeanField,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsOnMember( javaBeanField, b ) );

return constrainedField;
}
Original file line number Diff line number Diff line change
@@ -86,12 +86,7 @@ ConstrainedExecutable build(JavaBeanHelper javaBeanHelper, Class<?> beanClass, L
);

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsOnMember(
javaBeanGetter,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsOnMember( javaBeanGetter, b ) );

return constrainedGetter;
}
Original file line number Diff line number Diff line change
@@ -79,12 +79,7 @@ ConstrainedExecutable build(JavaBeanHelper javaBeanHelper, Class<?> beanClass, L
}

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsOnMember(
javaBeanMethod,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsOnMember( javaBeanMethod, b ) );

List<ConstrainedParameter> constrainedParameters = CollectionHelper.newArrayList( constrainedParameterStaxBuilders.size() );
for ( int index = 0; index < constrainedParameterStaxBuilders.size(); index++ ) {
Original file line number Diff line number Diff line change
@@ -77,13 +77,7 @@ ConstrainedParameter build(Callable callable, int index) {
ContainerElementTypeConfiguration containerElementTypeConfiguration = getContainerElementTypeConfiguration( type, constraintLocation );

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsOnParameter(
callable,
index,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsOnParameter( callable, index, b ) );

ConstrainedParameter constrainedParameter = new ConstrainedParameter(
ConfigurationSource.XML,
Original file line number Diff line number Diff line change
@@ -122,9 +122,7 @@ <A extends Annotation> MetaConstraint<A> build(ConstraintLocation constraintLoca

// set common things to all constraints:
Optional<String> message = messageStaxBuilder.build();
if ( message.isPresent() ) {
annotationDescriptorBuilder.setMessage( message.get() );
}
message.ifPresent( annotationDescriptorBuilder::setMessage );
annotationDescriptorBuilder.setGroups( groupsStaxBuilder.build() )
.setPayload( payloadStaxBuilder.build() );

Original file line number Diff line number Diff line change
@@ -79,9 +79,7 @@ protected String getAcceptableQName() {
@Override
protected void add(XMLEventReader xmlEventReader, XMLEvent xmlEvent) throws XMLStreamException {
Optional<String> typeArgumentIndex = readAttribute( xmlEvent.asStartElement(), TYPE_ARGUMENT_INDEX_QNAME );
if ( typeArgumentIndex.isPresent() ) {
this.typeArgumentIndex = Integer.parseInt( typeArgumentIndex.get() );
}
typeArgumentIndex.ifPresent( s -> this.typeArgumentIndex = Integer.parseInt( s ) );
ConstraintTypeStaxBuilder constraintTypeStaxBuilder = getNewConstraintTypeStaxBuilder();
ContainerElementTypeStaxBuilder containerElementTypeConfigurationStaxBuilder = getNewContainerElementTypeConfigurationStaxBuilder();
while ( !( xmlEvent.isEndElement() && xmlEvent.asEndElement().getName().getLocalPart().equals( getAcceptableQName() ) ) ) {
Original file line number Diff line number Diff line change
@@ -87,12 +87,7 @@ Set<MetaConstraint<?>> build(Callable callable) {
.collect( Collectors.toSet() );

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsForCrossParameterConstraint(
callable,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsForCrossParameterConstraint( callable, b ) );

return crossParameterConstraints;
}
Original file line number Diff line number Diff line change
@@ -62,12 +62,7 @@ CascadingMetaDataBuilder build(
returnValueTypeArgumentConstraints.addAll( containerElementTypeConfiguration.getMetaConstraints() );

// ignore annotations
if ( ignoreAnnotations.isPresent() ) {
annotationProcessingOptions.ignoreConstraintAnnotationsForReturnValue(
callable,
ignoreAnnotations.get()
);
}
ignoreAnnotations.ifPresent( b -> annotationProcessingOptions.ignoreConstraintAnnotationsForReturnValue( callable, b ) );

return getCascadingMetaData( containerElementTypeConfiguration.getTypeParametersCascadingMetaData(), callable.getType() );
}