From 12aaaff766b504a6cdb3ac2f0885350ee093d6a7 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 10 Aug 2022 14:19:43 +0200 Subject: [PATCH] HHH-15479 Add Results#addUnique(R result) --- .../sql/results/spi/ListResultsConsumer.java | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) 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 16ef70f0b3..4f52700fe4 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 @@ -7,14 +7,13 @@ package org.hibernate.sql.results.spi; import java.util.ArrayList; +import java.util.IdentityHashMap; import java.util.List; import java.util.Locale; -import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.internal.util.collections.IdentitySet; import org.hibernate.query.ResultListTransformer; import org.hibernate.query.spi.QueryOptions; import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl; @@ -115,7 +114,7 @@ public class ListResultsConsumer implements ResultsConsumer, R> { this.resultJavaType = resultJavaType; } - public boolean contains(R result) { + private boolean contains(R result) { for ( int i = 0; i < results.size(); i++ ) { if ( resultJavaType.areEqual( results.get( i ), result ) ) { return true; @@ -124,6 +123,14 @@ public class ListResultsConsumer implements ResultsConsumer, R> { return false; } + public boolean addUnique(R result) { + if ( contains( result ) ) { + return false; + } + results.add( result ); + return true; + } + public void add(R result) { results.add( result ); } @@ -134,20 +141,22 @@ public class ListResultsConsumer implements ResultsConsumer, R> { } private static class EntityResult extends Results { - private final Set added = new IdentitySet<>(); + private static final Object DUMP_VALUE = new Object(); + + private final IdentityHashMap added = new IdentityHashMap<>(); public EntityResult(JavaType resultJavaType) { super( resultJavaType ); } - public boolean contains(R result) { - return added.contains( result ); + public boolean addUnique(R result) { + if ( added.put( result, DUMP_VALUE ) == null ) { + super.add( result ); + return true; + } + return false; } - public void add(R result) { - super.add( result ); - added.add( result ); - } } @Override @@ -263,19 +272,14 @@ public class ListResultsConsumer implements ResultsConsumer, R> { Results results, RowProcessingStateStandardImpl rowProcessingState, boolean throwException) { - if ( results.contains( result ) ) { - if ( throwException && !rowProcessingState.hasCollectionInitializers ) { - throw new HibernateException( - String.format( - Locale.ROOT, - "Duplicate row was found and `%s` was specified", - UniqueSemantic.ASSERT - ) - ); - } - } - else { - results.add( result ); + if ( !results.addUnique( result ) && throwException && !rowProcessingState.hasCollectionInitializers ) { + throw new HibernateException( + String.format( + Locale.ROOT, + "Duplicate row was found and `%s` was specified", + UniqueSemantic.ASSERT + ) + ); } }