@@ -47,34 +47,57 @@ public abstract class DataAccessUtils {
4747 */
4848 @ Nullable
4949 public static <T > T singleResult (@ Nullable Collection <T > results ) throws IncorrectResultSizeDataAccessException {
50- int size = (results != null ? results .size () : 0 );
51- if (size == 0 ) {
50+ if (CollectionUtils .isEmpty (results )) {
5251 return null ;
5352 }
5453 if (results .size () > 1 ) {
55- throw new IncorrectResultSizeDataAccessException (1 , size );
54+ throw new IncorrectResultSizeDataAccessException (1 , results . size () );
5655 }
5756 return results .iterator ().next ();
5857 }
5958
6059 /**
6160 * Return a single result object from the given Collection.
6261 * <p>Throws an exception if 0 or more than 1 element found.
63- * @param results the result Collection (can be {@code null})
62+ * @param results the result Collection (can be {@code null}
63+ * but is not expected to contain {@code null} elements)
6464 * @return the single result object
6565 * @throws IncorrectResultSizeDataAccessException if more than one
6666 * element has been found in the given Collection
6767 * @throws EmptyResultDataAccessException if no element at all
6868 * has been found in the given Collection
6969 */
70- @ Nullable
7170 public static <T > T requiredSingleResult (@ Nullable Collection <T > results ) throws IncorrectResultSizeDataAccessException {
72- int size = (results != null ? results .size () : 0 );
73- if (size == 0 ) {
71+ if (CollectionUtils .isEmpty (results )) {
7472 throw new EmptyResultDataAccessException (1 );
7573 }
7674 if (results .size () > 1 ) {
77- throw new IncorrectResultSizeDataAccessException (1 , size );
75+ throw new IncorrectResultSizeDataAccessException (1 , results .size ());
76+ }
77+ return results .iterator ().next ();
78+ }
79+
80+ /**
81+ * Return a single result object from the given Collection.
82+ * <p>Throws an exception if 0 or more than 1 element found.
83+ * @param results the result Collection (can be {@code null}
84+ * and is also expected to contain {@code null} elements)
85+ * @return the single result object
86+ * @throws IncorrectResultSizeDataAccessException if more than one
87+ * element has been found in the given Collection
88+ * @throws EmptyResultDataAccessException if no element at all
89+ * has been found in the given Collection
90+ * @since 5.0.2
91+ */
92+ @ Nullable
93+ public static <T > T nullableSingleResult (@ Nullable Collection <T > results ) throws IncorrectResultSizeDataAccessException {
94+ // This is identical to the requiredSingleResult implementation but differs in the
95+ // semantics of the incoming Collection (which we currently can't formally express)
96+ if (CollectionUtils .isEmpty (results )) {
97+ throw new EmptyResultDataAccessException (1 );
98+ }
99+ if (results .size () > 1 ) {
100+ throw new IncorrectResultSizeDataAccessException (1 , results .size ());
78101 }
79102 return results .iterator ().next ();
80103 }
@@ -91,20 +114,20 @@ public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws
91114 */
92115 @ Nullable
93116 public static <T > T uniqueResult (@ Nullable Collection <T > results ) throws IncorrectResultSizeDataAccessException {
94- int size = (results != null ? results .size () : 0 );
95- if (size == 0 ) {
117+ if (CollectionUtils .isEmpty (results )) {
96118 return null ;
97119 }
98120 if (!CollectionUtils .hasUniqueObject (results )) {
99- throw new IncorrectResultSizeDataAccessException (1 , size );
121+ throw new IncorrectResultSizeDataAccessException (1 , results . size () );
100122 }
101123 return results .iterator ().next ();
102124 }
103125
104126 /**
105127 * Return a unique result object from the given Collection.
106128 * <p>Throws an exception if 0 or more than 1 instance found.
107- * @param results the result Collection (can be {@code null})
129+ * @param results the result Collection (can be {@code null}
130+ * but is not expected to contain {@code null} elements)
108131 * @return the unique result object
109132 * @throws IncorrectResultSizeDataAccessException if more than one
110133 * result object has been found in the given Collection
@@ -113,12 +136,11 @@ public static <T> T uniqueResult(@Nullable Collection<T> results) throws Incorre
113136 * @see org.springframework.util.CollectionUtils#hasUniqueObject
114137 */
115138 public static <T > T requiredUniqueResult (@ Nullable Collection <T > results ) throws IncorrectResultSizeDataAccessException {
116- int size = (results != null ? results .size () : 0 );
117- if (size == 0 ) {
139+ if (CollectionUtils .isEmpty (results )) {
118140 throw new EmptyResultDataAccessException (1 );
119141 }
120142 if (!CollectionUtils .hasUniqueObject (results )) {
121- throw new IncorrectResultSizeDataAccessException (1 , size );
143+ throw new IncorrectResultSizeDataAccessException (1 , results . size () );
122144 }
123145 return results .iterator ().next ();
124146 }
@@ -128,7 +150,8 @@ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws
128150 * Throws an exception if 0 or more than 1 result objects found,
129151 * of if the unique result object is not convertible to the
130152 * specified required type.
131- * @param results the result Collection (can be {@code null})
153+ * @param results the result Collection (can be {@code null}
154+ * but is not expected to contain {@code null} elements)
132155 * @return the unique result object
133156 * @throws IncorrectResultSizeDataAccessException if more than one
134157 * result object has been found in the given Collection
@@ -167,7 +190,8 @@ else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(
167190 * Return a unique int result from the given Collection.
168191 * Throws an exception if 0 or more than 1 result objects found,
169192 * of if the unique result object is not convertible to an int.
170- * @param results the result Collection (can be {@code null})
193+ * @param results the result Collection (can be {@code null}
194+ * but is not expected to contain {@code null} elements)
171195 * @return the unique int result
172196 * @throws IncorrectResultSizeDataAccessException if more than one
173197 * result object has been found in the given Collection
@@ -186,7 +210,8 @@ public static int intResult(@Nullable Collection<?> results)
186210 * Return a unique long result from the given Collection.
187211 * Throws an exception if 0 or more than 1 result objects found,
188212 * of if the unique result object is not convertible to a long.
189- * @param results the result Collection (can be {@code null})
213+ * @param results the result Collection (can be {@code null}
214+ * but is not expected to contain {@code null} elements)
190215 * @return the unique long result
191216 * @throws IncorrectResultSizeDataAccessException if more than one
192217 * result object has been found in the given Collection
@@ -204,11 +229,11 @@ public static long longResult(@Nullable Collection<?> results)
204229
205230 /**
206231 * Return a translated exception if this is appropriate,
207- * otherwise return the input exception.
208- * @param rawException exception we may wish to translate
232+ * otherwise return the given exception as-is .
233+ * @param rawException an exception that we may wish to translate
209234 * @param pet PersistenceExceptionTranslator to use to perform the translation
210- * @return a translated exception if translation is possible, or
211- * the raw exception if it is not
235+ * @return a translated persistence exception if translation is possible,
236+ * or the raw exception if it is not
212237 */
213238 public static RuntimeException translateIfNecessary (
214239 RuntimeException rawException , PersistenceExceptionTranslator pet ) {
0 commit comments