-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Split Validator::of into two factory methods: - forInstanceOf using Class::isAssignableFrom - forType using Class::equals - Moved anonymous implementation into TypedValidator with default access Closes gh-30341
- Loading branch information
Showing
5 changed files
with
176 additions
and
58 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
spring-context/src/main/java/org/springframework/validation/TypedValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.validation; | ||
|
||
import java.util.function.BiConsumer; | ||
import java.util.function.Predicate; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Validator instance returned by {@link Validator#forInstanceOf(Class, BiConsumer)} | ||
* and {@link Validator#forType(Class, BiConsumer)}. | ||
* | ||
* @author Toshiaki Maki | ||
* @author Arjen Poutsma | ||
* @since 6.1 | ||
* @param <T> the target object type | ||
*/ | ||
final class TypedValidator<T> implements Validator { | ||
|
||
private final Class<T> targetClass; | ||
|
||
private final Predicate<Class<?>> supports; | ||
|
||
private final BiConsumer<T, Errors> validate; | ||
|
||
|
||
public TypedValidator(Class<T> targetClass, Predicate<Class<?>> supports, BiConsumer<T, Errors> validate) { | ||
Assert.notNull(targetClass, "TargetClass must not be null"); | ||
Assert.notNull(supports, "Supports function must not be null"); | ||
Assert.notNull(validate, "Validate function must not be null"); | ||
|
||
this.targetClass = targetClass; | ||
this.supports = supports; | ||
this.validate = validate; | ||
} | ||
|
||
|
||
@Override | ||
public boolean supports(Class<?> clazz) { | ||
return this.supports.test(clazz); | ||
} | ||
|
||
@Override | ||
public void validate(Object target, Errors errors) { | ||
this.validate.accept(this.targetClass.cast(target), errors); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
spring-context/src/test/java/org/springframework/validation/ValidatorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.validation; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.testfixture.beans.TestBean; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Arjen Poutsma | ||
*/ | ||
class ValidatorTests { | ||
|
||
@Test | ||
public void testSupportsForInstanceOf() { | ||
Validator validator = Validator.forInstanceOf(TestBean.class, (testBean, errors) -> {}); | ||
assertThat(validator.supports(TestBean.class)).isTrue(); | ||
assertThat(validator.supports(TestBeanSubclass.class)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testSupportsForType() { | ||
Validator validator = Validator.forType(TestBean.class, (testBean, errors) -> {}); | ||
assertThat(validator.supports(TestBean.class)).isTrue(); | ||
assertThat(validator.supports(TestBeanSubclass.class)).isFalse(); | ||
} | ||
|
||
|
||
private static class TestBeanSubclass extends TestBean { | ||
} | ||
|
||
} |
3c57d55
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit had an incorrect commit message, it should have closed #29890; not #30341.