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 0e31401

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 c8f90c1 commit 0e31401

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
@@ -34,6 +34,7 @@
3434
* @param <T> the type of the parameter
3535
* @author Oliver Gierke
3636
* @author Christoph Strobl
37+
* @author Chris Bono
3738
*/
3839
public class Parameter<T, P extends PersistentProperty<P>> {
3940

@@ -98,6 +99,31 @@ public Parameter(@Nullable String name, TypeInformation<T> type, Annotation[] an
9899
return name;
99100
}
100101

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

‎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 によって変換されたページ (->オリジナル) /