Skip to content

Commit

Permalink
Avoid NullPointerException´ and IllegalArgumentException` for contr…
Browse files Browse the repository at this point in the history
…ol flow in `Sort`.

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

Closes #3081
  • Loading branch information
kurellajunior authored and mp911de committed Apr 24, 2024
1 parent 027b8c2 commit 7bac883
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/org/springframework/data/domain/Sort.java
Expand Up @@ -41,6 +41,7 @@
* @author Thomas Darimont
* @author Mark Paluch
* @author Johannes Englmeier
* @author Jan Kurella
*/
public class Sort implements Streamable<org.springframework.data.domain.Sort.Order>, Serializable {

Expand Down Expand Up @@ -341,14 +342,18 @@ public static Direction fromString(String value) {
}

/**
* 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
* @return
*/
public static Optional<Direction> fromOptionalString(String value) {

if (value == null) {
return Optional.empty();
}

try {
return Optional.of(fromString(value));
} catch (IllegalArgumentException e) {
Expand Down

0 comments on commit 7bac883

Please sign in to comment.