DataAccessUtils result accessors with Optional return value (#27735)

This commit is contained in:
René Wittner
2023-07-14 11:37:09 +02:00
committed by GitHub
parent 793581ebde
commit 4b80b0f490
2 changed files with 150 additions and 0 deletions

View File

@@ -17,6 +17,10 @@
package org.springframework.dao.support;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
@@ -56,6 +60,89 @@ public abstract class DataAccessUtils {
return results.iterator().next();
}
/**
* Return a single result object from the given Stream.
* <p>Returns {@code null} if 0 result objects found;
* throws an exception if more than 1 element found.
* @param results the result Stream (can be {@code null})
* @return the single result object, or {@code null} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Stream
*/
@Nullable
public static <T> T singleResult(@Nullable Stream<T> results) throws IncorrectResultSizeDataAccessException {
if (results == null) {
return null;
}
try (results) {
List<T> resultList = results.limit(2).toList();
if (resultList.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1);
}
return CollectionUtils.isEmpty(resultList) ? null : resultList.get(0);
}
}
/**
* Return a single result object from the given Iterator.
* <p>Returns {@code null} if 0 result objects found;
* throws an exception if more than 1 element found.
* @param results the result Iterator (can be {@code null})
* @return the single result object, or {@code null} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Iterator
*/
@Nullable
public static <T> T singleResult(@Nullable Iterator<T> results) throws IncorrectResultSizeDataAccessException {
if (results == null) {
return null;
}
T result = results.hasNext() ? results.next() : null;
if (results.hasNext()) {
throw new IncorrectResultSizeDataAccessException(1);
}
return result;
}
/**
* Return a single result object from the given Collection.
* <p>Returns {@code Optional.empty()} if 0 result objects found;
* throws an exception if more than 1 element found.
* @param results the result Collection (can be {@code null})
* @return the single optional result object, or {@code Optional.empty()} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Collection
*/
public static <T> Optional<T> optionalResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
return Optional.ofNullable(singleResult(results));
}
/**
* Return a single result object from the given Stream.
* <p>Returns {@code Optional.empty()} if 0 result objects found;
* throws an exception if more than 1 element found.
* @param results the result Stream (can be {@code null})
* @return the single optional result object, or {@code Optional.empty()} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Stream
*/
public static <T> Optional<T> optionalResult(@Nullable Stream<T> results) throws IncorrectResultSizeDataAccessException {
return Optional.ofNullable(singleResult(results));
}
/**
* Return a single result object from the given Iterator.
* <p>Returns {@code Optional.empty()} if 0 result objects found;
* throws an exception if more than 1 element found.
* @param results the result Iterator (can be {@code null})
* @return the single optional result object, or {@code Optional.empty()} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Iterator
*/
public static <T> Optional<T> optionalResult(@Nullable Iterator<T> results) throws IncorrectResultSizeDataAccessException {
return Optional.ofNullable(singleResult(results));
}
/**
* Return a single result object from the given Collection.
* <p>Throws an exception if 0 or more than 1 element found.