Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a978209

Browse files
onobcmp911de
authored andcommitted
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
1 parent c0b60b2 commit a978209

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎src/main/java/org/springframework/data/mapping/Parameter.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @param <T> the type of the parameter
3434
* @author Oliver Gierke
3535
* @author Christoph Strobl
36+
* @author Chris Bono
3637
*/
3738
public class Parameter<T, P extends PersistentProperty<P>> {
3839

@@ -99,6 +100,31 @@ public String getName() {
99100
return name;
100101
}
101102

103+
/**
104+
* Returns the required parameter name.
105+
*
106+
* @return the parameter name or throws {@link IllegalStateException} if the parameter does not have a name
107+
* @since 3.5
108+
*/
109+
public String getRequiredName() {
110+
111+
if (!hasName()) {
112+
throw new IllegalStateException("No name associated with this parameter");
113+
}
114+
115+
return getName();
116+
}
117+
118+
/**
119+
* Returns whether the parameter has a name.
120+
*
121+
* @return whether the parameter has a name
122+
* @since 3.5
123+
*/
124+
public boolean hasName() {
125+
return this.name != null;
126+
}
127+
102128
/**
103129
* Returns the {@link TypeInformation} of the parameter.
104130
*

‎src/test/java/org/springframework/data/mapping/ParameterUnitTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Oliver Gierke
3939
* @author Christoph Strobl
40+
* @author Chris Bono
4041
*/
4142
@ExtendWith(MockitoExtension.class)
4243
class ParameterUnitTests<P extends PersistentProperty<P>> {
@@ -149,6 +150,35 @@ void shouldNotConsiderRecordMemberTypeOfClassEnclosingClassParameter() {
149150
assertThat(iFace.isEnclosingClassParameter()).isFalse();
150151
}
151152

153+
@Test // GH-3088
154+
void getRequiredNameDoesNotThrowExceptionWhenHasName() {
155+
156+
var parameter = new Parameter<>("someName", type, annotations, entity);
157+
assertThat(parameter.getRequiredName()).isEqualTo("someName");
158+
}
159+
160+
@Test // GH-3088
161+
void getRequiredNameThrowsExceptionWhenHasNoName() {
162+
163+
var parameter = new Parameter<>(null, type, annotations, entity);
164+
assertThatIllegalStateException().isThrownBy(() -> parameter.getRequiredName())
165+
.withMessage("No name associated with this parameter");
166+
}
167+
168+
@Test // GH-3088
169+
void hasNameReturnsTrueWhenHasName() {
170+
171+
var parameter = new Parameter<>("someName", type, annotations, entity);
172+
assertThat(parameter.hasName()).isTrue();
173+
}
174+
175+
@Test // GH-3088
176+
void hasNameReturnsFalseWhenHasNoName() {
177+
178+
var parameter = new Parameter<>(null, type, annotations, entity);
179+
assertThat(parameter.hasName()).isFalse();
180+
}
181+
152182
interface IFace {
153183

154184
record RecordMember(IFace iFace) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /