Browse Source

Add since tags and code style refinements

See gh-27735
pull/30891/head
Juergen Hoeller 2 years ago
parent
commit
0ab94478fe
  1. 7
      spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java
  2. 12
      spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java

7
spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java

@ -68,6 +68,7 @@ public abstract class DataAccessUtils { @@ -68,6 +68,7 @@ public abstract class DataAccessUtils {
* @return the single result object, or {@code null} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Stream
* @since 6.1
*/
@Nullable
public static <T> T singleResult(@Nullable Stream<T> results) throws IncorrectResultSizeDataAccessException {
@ -91,13 +92,14 @@ public abstract class DataAccessUtils { @@ -91,13 +92,14 @@ public abstract class DataAccessUtils {
* @return the single result object, or {@code null} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* element has been found in the given Iterator
* @since 6.1
*/
@Nullable
public static <T> T singleResult(@Nullable Iterator<T> results) throws IncorrectResultSizeDataAccessException {
if (results == null) {
return null;
}
T result = results.hasNext() ? results.next() : null;
T result = (results.hasNext() ? results.next() : null);
if (results.hasNext()) {
throw new IncorrectResultSizeDataAccessException(1);
}
@ -112,6 +114,7 @@ public abstract class DataAccessUtils { @@ -112,6 +114,7 @@ public abstract class DataAccessUtils {
* @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
* @since 6.1
*/
public static <T> Optional<T> optionalResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
return Optional.ofNullable(singleResult(results));
@ -125,6 +128,7 @@ public abstract class DataAccessUtils { @@ -125,6 +128,7 @@ public abstract class DataAccessUtils {
* @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
* @since 6.1
*/
public static <T> Optional<T> optionalResult(@Nullable Stream<T> results) throws IncorrectResultSizeDataAccessException {
return Optional.ofNullable(singleResult(results));
@ -138,6 +142,7 @@ public abstract class DataAccessUtils { @@ -138,6 +142,7 @@ public abstract class DataAccessUtils {
* @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
* @since 6.1
*/
public static <T> Optional<T> optionalResult(@Nullable Iterator<T> results) throws IncorrectResultSizeDataAccessException {
return Optional.ofNullable(singleResult(results));

12
spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -247,21 +247,21 @@ public class DataAccessUtilsTests { @@ -247,21 +247,21 @@ public class DataAccessUtilsTests {
assertThat(DataAccessUtils.translateIfNecessary(in, mpet)).isSameAs(out);
}
private <E extends IncorrectResultSizeDataAccessException> Consumer<E> sizeRequirements(
int expectedSize, int actualSize) {
return ex -> {
assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize);
assertThat(ex.getActualSize()).as("actual size").isEqualTo(actualSize);
};
}
private <E extends IncorrectResultSizeDataAccessException> Consumer<E> sizeRequirements(
int expectedSize) {
return ex -> {
assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize);
};
private <E extends IncorrectResultSizeDataAccessException> Consumer<E> sizeRequirements(int expectedSize) {
return ex -> assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize);
}
public static class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator {
// in to out

Loading…
Cancel
Save