Avoid NullPointerException´ and IllegalArgumentException for control flow in Sort`.

Checking for null explicitly is cheap and prevents two unnecessary Exception
objects creations.

Closes #3081
This commit is contained in:
Jan Kurella
2024-04-16 13:48:06 +02:00
committed by Mark Paluch
parent 289e4d1f52
commit 23de76de35

View File

@@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
* @author Thomas Darimont
* @author Mark Paluch
* @author Johannes Englmeier
* @author Jan Kurella
*/
public class Sort implements Streamable<org.springframework.data.domain.Sort.Order>, Serializable {
@@ -341,7 +342,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
}
/**
* Returns the {@link Direction} enum for the given {@link String} or null if it cannot be parsed into an enum
* Returns the {@link Direction} enum for the given {@link String} or empty if it cannot be parsed into an enum
* value.
*
* @param value
@@ -349,6 +350,10 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
*/
public static Optional<Direction> fromOptionalString(String value) {
if (value == null) {
return Optional.empty();
}
try {
return Optional.of(fromString(value));
} catch (IllegalArgumentException e) {