diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java index 2ba285842a83..8b5bc2d6f4b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java @@ -90,20 +90,9 @@ public enum UniqueSemantic { } private final UniqueSemantic uniqueSemantic; - private final ResultHandler resultHandler; public ListResultsConsumer(UniqueSemantic uniqueSemantic) { this.uniqueSemantic = uniqueSemantic; - - if ( uniqueSemantic == UniqueSemantic.FILTER ) { - resultHandler = ListResultsConsumer::deDuplicationHandling; - } - else if ( uniqueSemantic == UniqueSemantic.ASSERT ) { - resultHandler = ListResultsConsumer::duplicationErrorHandling; - } - else { - resultHandler = ListResultsConsumer::applyAll; - } } private static class Results { @@ -114,19 +103,12 @@ public Results(JavaType resultJavaType) { this.resultJavaType = resultJavaType; } - private boolean contains(R result) { + public boolean addUnique(R result) { for ( int i = 0; i < results.size(); i++ ) { if ( resultJavaType.areEqual( results.get( i ), result ) ) { - return true; + return false; } } - return false; - } - - public boolean addUnique(R result) { - if ( contains( result ) ) { - return false; - } results.add( result ); return true; } @@ -156,7 +138,6 @@ public boolean addUnique(R result) { } return false; } - } @Override @@ -180,15 +161,7 @@ public List consume( typeConfiguration ); - final ResultHandler resultHandlerToUse; - - boolean isEnityResultType = domainResultJavaType instanceof EntityJavaType; - if ( uniqueSemantic == UniqueSemantic.ALLOW && isEnityResultType ) { - resultHandlerToUse = ListResultsConsumer::deDuplicationHandling; - } - else { - resultHandlerToUse = this.resultHandler; - } + final boolean isEnityResultType = domainResultJavaType instanceof EntityJavaType; final Results results; if ( ( uniqueSemantic == UniqueSemantic.ALLOW || uniqueSemantic == UniqueSemantic.FILTER ) && isEnityResultType ) { @@ -198,10 +171,33 @@ public List consume( results = new Results<>( domainResultJavaType ); } - while ( rowProcessingState.next() ) { - final R row = rowReader.readRow( rowProcessingState, processingOptions ); - resultHandlerToUse.handle( row, results, rowProcessingState ); - rowProcessingState.finishRowProcessing(); + if ( this.uniqueSemantic == UniqueSemantic.FILTER + || this.uniqueSemantic == UniqueSemantic.ASSERT && rowProcessingState.hasCollectionInitializers + || this.uniqueSemantic == UniqueSemantic.ALLOW && isEnityResultType ) { + while ( rowProcessingState.next() ) { + results.addUnique( rowReader.readRow( rowProcessingState, processingOptions ) ); + rowProcessingState.finishRowProcessing(); + } + } + else if ( this.uniqueSemantic == UniqueSemantic.ASSERT ) { + while ( rowProcessingState.next() ) { + if ( !results.addUnique( rowReader.readRow( rowProcessingState, processingOptions ) ) ) { + throw new HibernateException( + String.format( + Locale.ROOT, + "Duplicate row was found and `%s` was specified", + UniqueSemantic.ASSERT + ) + ); + } + rowProcessingState.finishRowProcessing(); + } + } + else { + while ( rowProcessingState.next() ) { + results.add( rowReader.readRow( rowProcessingState, processingOptions ) ); + rowProcessingState.finishRowProcessing(); + } } try { @@ -245,63 +241,6 @@ public List consume( throw new IllegalStateException( "Should not reach this" ); } - /** - * Essentially a tri-consumer for applying the different duplication strategies. - * - * @see UniqueSemantic - */ - @FunctionalInterface - private interface ResultHandler { - void handle(R result, Results results, RowProcessingStateStandardImpl rowProcessingState); - } - - public static void deDuplicationHandling( - R result, - Results results, - RowProcessingStateStandardImpl rowProcessingState) { - withDuplicationCheck( - result, - results, - rowProcessingState, - false - ); - } - - private static void withDuplicationCheck( - R result, - Results results, - RowProcessingStateStandardImpl rowProcessingState, - boolean throwException) { - if ( !results.addUnique( result ) && throwException && !rowProcessingState.hasCollectionInitializers ) { - throw new HibernateException( - String.format( - Locale.ROOT, - "Duplicate row was found and `%s` was specified", - UniqueSemantic.ASSERT - ) - ); - } - } - - public static void duplicationErrorHandling( - R result, - Results results, - RowProcessingStateStandardImpl rowProcessingState) { - withDuplicationCheck( - result, - results, - rowProcessingState, - true - ); - } - - public static void applyAll( - R result, - Results results, - RowProcessingStateStandardImpl rowProcessingState) { - results.add( result ); - } - private JavaType resolveDomainResultJavaType( Class domainResultResultJavaType, List> resultJavaTypes,