Skip to content

Commit

Permalink
Polishing Optional usage
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Mar 11, 2024
1 parent 729cd01 commit 58bbc0b
Show file tree
Hide file tree
Showing 20 changed files with 26 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand All @@ -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 );
}

Expand All @@ -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 );
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++ ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++ ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand Down

0 comments on commit 58bbc0b

Please sign in to comment.