diff --git a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java index c94ba2211b7..0fe38231848 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java +++ b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java @@ -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 singleResult(@Nullable Stream results) throws IncorrectResultSizeDataAccessException { @@ -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 singleResult(@Nullable Iterator 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 { * @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 Optional optionalResult(@Nullable Collection results) throws IncorrectResultSizeDataAccessException { return Optional.ofNullable(singleResult(results)); @@ -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 Optional optionalResult(@Nullable Stream results) throws IncorrectResultSizeDataAccessException { return Optional.ofNullable(singleResult(results)); @@ -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 Optional optionalResult(@Nullable Iterator results) throws IncorrectResultSizeDataAccessException { return Optional.ofNullable(singleResult(results)); diff --git a/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java b/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java index 0cfbbb2036f..59b1dde4f49 100644 --- a/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java +++ b/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java @@ -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 { assertThat(DataAccessUtils.translateIfNecessary(in, mpet)).isSameAs(out); } + private Consumer sizeRequirements( int expectedSize, int actualSize) { + return ex -> { assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize); assertThat(ex.getActualSize()).as("actual size").isEqualTo(actualSize); }; } - private Consumer sizeRequirements( - int expectedSize) { - return ex -> { - assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize); - }; + private Consumer sizeRequirements(int expectedSize) { + return ex -> assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize); } + public static class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator { // in to out