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 65257d9

Browse files
committed
Introduce CustomConversions.getRequiredValueConverter(...).
Provide convenience method to simplify flow in calling code. See #3170
1 parent 5b96fb4 commit 65257d9

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

‎src/main/java/org/springframework/data/convert/CustomConversions.java‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,36 @@ public SimpleTypeHolder getSimpleTypeHolder() {
185185
*/
186186
public boolean hasValueConverter(PersistentProperty<?> property) {
187187

188-
PropertyValueConversions propertyValueConversions = getPropertyValueConversions();
188+
PropertyValueConversions pvc = getPropertyValueConversions();
189189

190-
return propertyValueConversions != null && propertyValueConversions.hasValueConverter(property);
190+
return pvc != null && pvc.hasValueConverter(property);
191+
}
192+
193+
/**
194+
* Returns the required {@link PropertyValueConverter} for the given {@link PersistentProperty} or throws
195+
* {@link IllegalStateException} if no converter is available. This is a convenience method for
196+
* {@code getPropertyValueConversions().getValueConverter(...)} enforcing non-null constraints.
197+
* <p>
198+
* Prior to calling this method you should verify a converter is available using
199+
* {@link #hasValueConverter(PersistentProperty)}.
200+
*
201+
* @param property {@link PersistentProperty} to evaluate; must not be {@literal null}.
202+
* @return the required {@link PropertyValueConverter}
203+
* @throws IllegalStateException if no converter is available.
204+
* @since 4.0
205+
* @see #hasValueConverter(PersistentProperty)
206+
*/
207+
public <DV, SV, P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, VCC> getRequiredValueConverter(
208+
P property) {
209+
210+
PropertyValueConversions pvc = getPropertyValueConversions();
211+
PropertyValueConverter<DV, SV, VCC> converter = pvc != null ? pvc.getValueConverter(property) : null;
212+
213+
if (converter == null) {
214+
throw new IllegalStateException("No value converter registered for property %s".formatted(property.getName()));
215+
}
216+
217+
return converter;
191218
}
192219

193220
/**

0 commit comments

Comments
(0)

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