|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.validator.internal.constraintvalidators.bv; |
| 6 | + |
| 7 | + |
| 8 | +import static java.util.Objects.nonNull; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import jakarta.validation.ConstraintValidator; |
| 16 | +import jakarta.validation.ConstraintValidatorContext; |
| 17 | + |
| 18 | +import org.hibernate.validator.constraints.OneOf; |
| 19 | + |
| 20 | +/** |
| 21 | + * Validator that checks if a given {@link CharSequence} matches one of the allowed values specified |
| 22 | + * in the {@link OneOf} annotation. |
| 23 | + * |
| 24 | + * <p>This class implements the {@link ConstraintValidator} interface to perform the validation logic |
| 25 | + * based on the configuration in the {@link OneOf} annotation.</p> |
| 26 | + * |
| 27 | + * @author Yusuf Àlàmù Musa |
| 28 | + * @version 1.0 |
| 29 | + */ |
| 30 | +public class OneOfValidator implements ConstraintValidator<OneOf, CharSequence> { |
| 31 | + |
| 32 | + private final List<String> acceptedValues = new ArrayList<>(); |
| 33 | + private boolean ignoreCase; |
| 34 | + |
| 35 | + /** |
| 36 | + * Initializes the validator with the values specified in the {@link OneOf} annotation. |
| 37 | + * |
| 38 | + * <p>This method sets the case sensitivity flag and adds the allowed values (either enum constants or specified values) |
| 39 | + * to the list of accepted values for validation.</p> |
| 40 | + * |
| 41 | + * @param constraintAnnotation the {@link OneOf} annotation containing the configuration for validation. |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public void initialize(final OneOf constraintAnnotation) { |
| 45 | + ignoreCase = constraintAnnotation.ignoreCase(); |
| 46 | + |
| 47 | + // If an enum class is specified, initialize accepted values from the enum constants |
| 48 | + if ( constraintAnnotation.enumClass() != null ) { |
| 49 | + final Enum<?>[] enumConstants = constraintAnnotation.enumClass().getEnumConstants(); |
| 50 | + initializeAcceptedValues( enumConstants ); |
| 51 | + } |
| 52 | + |
| 53 | + // If specific allowed values are provided, initialize accepted values from them |
| 54 | + if ( constraintAnnotation.allowedValues() != null ) { |
| 55 | + initializeAcceptedValues( constraintAnnotation.allowedValues() ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Validates the given value based on the accepted values. |
| 61 | + * |
| 62 | + * <p>If the value is not null, it checks whether the value matches any of the accepted values. |
| 63 | + * If the value is null, it is considered valid.</p> |
| 64 | + * |
| 65 | + * @param value the value to validate. |
| 66 | + * @param context the validation context. |
| 67 | + * @return {@code true} if the value is valid, {@code false} otherwise. |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public boolean isValid(final CharSequence value, final ConstraintValidatorContext context) { |
| 71 | + if ( nonNull( value ) ) { |
| 72 | + return checkIfValueTheSame( value.toString() ); |
| 73 | + } |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks if the provided value matches any of the accepted values. |
| 79 | + * |
| 80 | + * <p>If {@code ignoreCase} is false, the comparison is case-sensitive. |
| 81 | + * If {@code ignoreCase} is true, the value is compared in lowercase.</p> |
| 82 | + * |
| 83 | + * @param value the value to check. |
| 84 | + * @return {@code true} if the value matches an accepted value, {@code false} otherwise. |
| 85 | + */ |
| 86 | + protected boolean checkIfValueTheSame(final String value) { |
| 87 | + if ( !ignoreCase ) { |
| 88 | + return acceptedValues.contains( value ); |
| 89 | + } |
| 90 | + |
| 91 | + for ( final String acceptedValue : acceptedValues ) { |
| 92 | + if ( acceptedValue.toLowerCase( Locale.ROOT ).equals( value.toLowerCase( Locale.ROOT ) ) ) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Initializes and adds the names of the provided enum constants to the accepted values list. |
| 102 | + * |
| 103 | + * @param enumConstants the enum constants to be added, ignored if null. |
| 104 | + */ |
| 105 | + protected void initializeAcceptedValues(final Enum<?>... enumConstants) { |
| 106 | + if ( nonNull( enumConstants ) ) { |
| 107 | + acceptedValues.addAll( Stream.of( enumConstants ).map( Enum::name ).toList() ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Initializes and adds the provided values to the accepted values list after trimming them. |
| 113 | + * |
| 114 | + * @param values the values to be added, ignored if null. |
| 115 | + */ |
| 116 | + protected void initializeAcceptedValues(final String... values) { |
| 117 | + if ( nonNull( values ) ) { |
| 118 | + acceptedValues.addAll( Stream.of( values ).map( String::trim ).toList() ); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments