Add getRequiredName and hasName API to org.springframework.data.mapping.Parameter.
Introduces a more convenient API to simplify the caller side especially for conditionals that want to determine whether a parameter name is present. Closes #3088 Original pull request: #3272
This commit is contained in:
@@ -33,6 +33,7 @@ import org.springframework.util.StringUtils;
|
||||
* @param <T> the type of the parameter
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @author Chris Bono
|
||||
*/
|
||||
public class Parameter<T, P extends PersistentProperty<P>> {
|
||||
|
||||
@@ -99,6 +100,31 @@ public class Parameter<T, P extends PersistentProperty<P>> {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the required parameter name.
|
||||
*
|
||||
* @return the parameter name or throws {@link IllegalStateException} if the parameter does not have a name
|
||||
* @since 3.5
|
||||
*/
|
||||
public String getRequiredName() {
|
||||
|
||||
if (!hasName()) {
|
||||
throw new IllegalStateException("No name associated with this parameter");
|
||||
}
|
||||
|
||||
return getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the parameter has a name.
|
||||
*
|
||||
* @return whether the parameter has a name
|
||||
* @since 3.5
|
||||
*/
|
||||
public boolean hasName() {
|
||||
return this.name != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link TypeInformation} of the parameter.
|
||||
*
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @author Chris Bono
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ParameterUnitTests<P extends PersistentProperty<P>> {
|
||||
@@ -149,6 +150,35 @@ class ParameterUnitTests<P extends PersistentProperty<P>> {
|
||||
assertThat(iFace.isEnclosingClassParameter()).isFalse();
|
||||
}
|
||||
|
||||
@Test // GH-3088
|
||||
void getRequiredNameDoesNotThrowExceptionWhenHasName() {
|
||||
|
||||
var parameter = new Parameter<>("someName", type, annotations, entity);
|
||||
assertThat(parameter.getRequiredName()).isEqualTo("someName");
|
||||
}
|
||||
|
||||
@Test // GH-3088
|
||||
void getRequiredNameThrowsExceptionWhenHasNoName() {
|
||||
|
||||
var parameter = new Parameter<>(null, type, annotations, entity);
|
||||
assertThatIllegalStateException().isThrownBy(() -> parameter.getRequiredName())
|
||||
.withMessage("No name associated with this parameter");
|
||||
}
|
||||
|
||||
@Test // GH-3088
|
||||
void hasNameReturnsTrueWhenHasName() {
|
||||
|
||||
var parameter = new Parameter<>("someName", type, annotations, entity);
|
||||
assertThat(parameter.hasName()).isTrue();
|
||||
}
|
||||
|
||||
@Test // GH-3088
|
||||
void hasNameReturnsFalseWhenHasNoName() {
|
||||
|
||||
var parameter = new Parameter<>(null, type, annotations, entity);
|
||||
assertThat(parameter.hasName()).isFalse();
|
||||
}
|
||||
|
||||
interface IFace {
|
||||
|
||||
record RecordMember(IFace iFace) {
|
||||
|
||||
Reference in New Issue
Block a user