@@ -47,34 +47,57 @@ public abstract class DataAccessUtils {
47
47
*/
48
48
@ Nullable
49
49
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 )) {
52
51
return null ;
53
52
}
54
53
if (results .size () > 1 ) {
55
- throw new IncorrectResultSizeDataAccessException (1 , size );
54
+ throw new IncorrectResultSizeDataAccessException (1 , results . size () );
56
55
}
57
56
return results .iterator ().next ();
58
57
}
59
58
60
59
/**
61
60
* Return a single result object from the given Collection.
62
61
* <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)
64
64
* @return the single result object
65
65
* @throws IncorrectResultSizeDataAccessException if more than one
66
66
* element has been found in the given Collection
67
67
* @throws EmptyResultDataAccessException if no element at all
68
68
* has been found in the given Collection
69
69
*/
70
- @ Nullable
71
70
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 )) {
74
72
throw new EmptyResultDataAccessException (1 );
75
73
}
76
74
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 ());
78
101
}
79
102
return results .iterator ().next ();
80
103
}
@@ -91,20 +114,20 @@ public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws
91
114
*/
92
115
@ Nullable
93
116
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 )) {
96
118
return null ;
97
119
}
98
120
if (!CollectionUtils .hasUniqueObject (results )) {
99
- throw new IncorrectResultSizeDataAccessException (1 , size );
121
+ throw new IncorrectResultSizeDataAccessException (1 , results . size () );
100
122
}
101
123
return results .iterator ().next ();
102
124
}
103
125
104
126
/**
105
127
* Return a unique result object from the given Collection.
106
128
* <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)
108
131
* @return the unique result object
109
132
* @throws IncorrectResultSizeDataAccessException if more than one
110
133
* result object has been found in the given Collection
@@ -113,12 +136,11 @@ public static <T> T uniqueResult(@Nullable Collection<T> results) throws Incorre
113
136
* @see org.springframework.util.CollectionUtils#hasUniqueObject
114
137
*/
115
138
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 )) {
118
140
throw new EmptyResultDataAccessException (1 );
119
141
}
120
142
if (!CollectionUtils .hasUniqueObject (results )) {
121
- throw new IncorrectResultSizeDataAccessException (1 , size );
143
+ throw new IncorrectResultSizeDataAccessException (1 , results . size () );
122
144
}
123
145
return results .iterator ().next ();
124
146
}
@@ -128,7 +150,8 @@ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws
128
150
* Throws an exception if 0 or more than 1 result objects found,
129
151
* of if the unique result object is not convertible to the
130
152
* 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)
132
155
* @return the unique result object
133
156
* @throws IncorrectResultSizeDataAccessException if more than one
134
157
* result object has been found in the given Collection
@@ -167,7 +190,8 @@ else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(
167
190
* Return a unique int result from the given Collection.
168
191
* Throws an exception if 0 or more than 1 result objects found,
169
192
* 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)
171
195
* @return the unique int result
172
196
* @throws IncorrectResultSizeDataAccessException if more than one
173
197
* result object has been found in the given Collection
@@ -186,7 +210,8 @@ public static int intResult(@Nullable Collection<?> results)
186
210
* Return a unique long result from the given Collection.
187
211
* Throws an exception if 0 or more than 1 result objects found,
188
212
* 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)
190
215
* @return the unique long result
191
216
* @throws IncorrectResultSizeDataAccessException if more than one
192
217
* result object has been found in the given Collection
@@ -204,11 +229,11 @@ public static long longResult(@Nullable Collection<?> results)
204
229
205
230
/**
206
231
* 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
209
234
* @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
212
237
*/
213
238
public static RuntimeException translateIfNecessary (
214
239
RuntimeException rawException , PersistenceExceptionTranslator pet ) {
0 commit comments