Consistent nullability in DataAccessUtils through nullableSingleResult
Issue: SPR-16225
This commit is contained in:
@@ -47,12 +47,11 @@ public abstract class DataAccessUtils {
|
||||
*/
|
||||
@Nullable
|
||||
public static <T> T singleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
|
||||
int size = (results != null ? results.size() : 0);
|
||||
if (size == 0) {
|
||||
if (CollectionUtils.isEmpty(results)) {
|
||||
return null;
|
||||
}
|
||||
if (results.size() > 1) {
|
||||
throw new IncorrectResultSizeDataAccessException(1, size);
|
||||
throw new IncorrectResultSizeDataAccessException(1, results.size());
|
||||
}
|
||||
return results.iterator().next();
|
||||
}
|
||||
@@ -60,21 +59,45 @@ public abstract class DataAccessUtils {
|
||||
/**
|
||||
* Return a single result object from the given Collection.
|
||||
* <p>Throws an exception if 0 or more than 1 element found.
|
||||
* @param results the result Collection (can be {@code null})
|
||||
* @param results the result Collection (can be {@code null}
|
||||
* but is not expected to contain {@code null} elements)
|
||||
* @return the single result object
|
||||
* @throws IncorrectResultSizeDataAccessException if more than one
|
||||
* element has been found in the given Collection
|
||||
* @throws EmptyResultDataAccessException if no element at all
|
||||
* has been found in the given Collection
|
||||
*/
|
||||
@Nullable
|
||||
public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
|
||||
int size = (results != null ? results.size() : 0);
|
||||
if (size == 0) {
|
||||
if (CollectionUtils.isEmpty(results)) {
|
||||
throw new EmptyResultDataAccessException(1);
|
||||
}
|
||||
if (results.size() > 1) {
|
||||
throw new IncorrectResultSizeDataAccessException(1, size);
|
||||
throw new IncorrectResultSizeDataAccessException(1, results.size());
|
||||
}
|
||||
return results.iterator().next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a single result object from the given Collection.
|
||||
* <p>Throws an exception if 0 or more than 1 element found.
|
||||
* @param results the result Collection (can be {@code null}
|
||||
* and is also expected to contain {@code null} elements)
|
||||
* @return the single result object
|
||||
* @throws IncorrectResultSizeDataAccessException if more than one
|
||||
* element has been found in the given Collection
|
||||
* @throws EmptyResultDataAccessException if no element at all
|
||||
* has been found in the given Collection
|
||||
* @since 5.0.2
|
||||
*/
|
||||
@Nullable
|
||||
public static <T> T nullableSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
|
||||
// This is identical to the requiredSingleResult implementation but differs in the
|
||||
// semantics of the incoming Collection (which we currently can't formally express)
|
||||
if (CollectionUtils.isEmpty(results)) {
|
||||
throw new EmptyResultDataAccessException(1);
|
||||
}
|
||||
if (results.size() > 1) {
|
||||
throw new IncorrectResultSizeDataAccessException(1, results.size());
|
||||
}
|
||||
return results.iterator().next();
|
||||
}
|
||||
@@ -91,12 +114,11 @@ public abstract class DataAccessUtils {
|
||||
*/
|
||||
@Nullable
|
||||
public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
|
||||
int size = (results != null ? results.size() : 0);
|
||||
if (size == 0) {
|
||||
if (CollectionUtils.isEmpty(results)) {
|
||||
return null;
|
||||
}
|
||||
if (!CollectionUtils.hasUniqueObject(results)) {
|
||||
throw new IncorrectResultSizeDataAccessException(1, size);
|
||||
throw new IncorrectResultSizeDataAccessException(1, results.size());
|
||||
}
|
||||
return results.iterator().next();
|
||||
}
|
||||
@@ -104,7 +126,8 @@ public abstract class DataAccessUtils {
|
||||
/**
|
||||
* Return a unique result object from the given Collection.
|
||||
* <p>Throws an exception if 0 or more than 1 instance found.
|
||||
* @param results the result Collection (can be {@code null})
|
||||
* @param results the result Collection (can be {@code null}
|
||||
* but is not expected to contain {@code null} elements)
|
||||
* @return the unique result object
|
||||
* @throws IncorrectResultSizeDataAccessException if more than one
|
||||
* result object has been found in the given Collection
|
||||
@@ -113,12 +136,11 @@ public abstract class DataAccessUtils {
|
||||
* @see org.springframework.util.CollectionUtils#hasUniqueObject
|
||||
*/
|
||||
public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
|
||||
int size = (results != null ? results.size() : 0);
|
||||
if (size == 0) {
|
||||
if (CollectionUtils.isEmpty(results)) {
|
||||
throw new EmptyResultDataAccessException(1);
|
||||
}
|
||||
if (!CollectionUtils.hasUniqueObject(results)) {
|
||||
throw new IncorrectResultSizeDataAccessException(1, size);
|
||||
throw new IncorrectResultSizeDataAccessException(1, results.size());
|
||||
}
|
||||
return results.iterator().next();
|
||||
}
|
||||
@@ -128,7 +150,8 @@ public abstract class DataAccessUtils {
|
||||
* Throws an exception if 0 or more than 1 result objects found,
|
||||
* of if the unique result object is not convertible to the
|
||||
* specified required type.
|
||||
* @param results the result Collection (can be {@code null})
|
||||
* @param results the result Collection (can be {@code null}
|
||||
* but is not expected to contain {@code null} elements)
|
||||
* @return the unique result object
|
||||
* @throws IncorrectResultSizeDataAccessException if more than one
|
||||
* result object has been found in the given Collection
|
||||
@@ -167,7 +190,8 @@ public abstract class DataAccessUtils {
|
||||
* Return a unique int result from the given Collection.
|
||||
* Throws an exception if 0 or more than 1 result objects found,
|
||||
* of if the unique result object is not convertible to an int.
|
||||
* @param results the result Collection (can be {@code null})
|
||||
* @param results the result Collection (can be {@code null}
|
||||
* but is not expected to contain {@code null} elements)
|
||||
* @return the unique int result
|
||||
* @throws IncorrectResultSizeDataAccessException if more than one
|
||||
* result object has been found in the given Collection
|
||||
@@ -186,7 +210,8 @@ public abstract class DataAccessUtils {
|
||||
* Return a unique long result from the given Collection.
|
||||
* Throws an exception if 0 or more than 1 result objects found,
|
||||
* of if the unique result object is not convertible to a long.
|
||||
* @param results the result Collection (can be {@code null})
|
||||
* @param results the result Collection (can be {@code null}
|
||||
* but is not expected to contain {@code null} elements)
|
||||
* @return the unique long result
|
||||
* @throws IncorrectResultSizeDataAccessException if more than one
|
||||
* result object has been found in the given Collection
|
||||
@@ -204,11 +229,11 @@ public abstract class DataAccessUtils {
|
||||
|
||||
/**
|
||||
* Return a translated exception if this is appropriate,
|
||||
* otherwise return the input exception.
|
||||
* @param rawException exception we may wish to translate
|
||||
* otherwise return the given exception as-is.
|
||||
* @param rawException an exception that we may wish to translate
|
||||
* @param pet PersistenceExceptionTranslator to use to perform the translation
|
||||
* @return a translated exception if translation is possible, or
|
||||
* the raw exception if it is not
|
||||
* @return a translated persistence exception if translation is possible,
|
||||
* or the raw exception if it is not
|
||||
*/
|
||||
public static RuntimeException translateIfNecessary(
|
||||
RuntimeException rawException, PersistenceExceptionTranslator pet) {
|
||||
|
||||
Reference in New Issue
Block a user