Skip to content

Commit

Permalink
HHH-16705 AttributeMappingsList should not implement Iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed May 26, 2023
1 parent 660b180 commit 4bb95b7
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @since 6.2
*/
@Incubating
public interface AttributeMappingsList extends Iterable<AttributeMapping> {
public interface AttributeMappingsList {

int size();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ default void applySqlSelections(
}

default int compare(Object value1, Object value2) {
for ( AttributeMapping attributeMapping : getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attributeMapping = attributeMappings.get( i );
final Getter getter = attributeMapping.getPropertyAccess().getGetter();
final int comparison = attributeMapping.compare( getter.get( value1 ), getter.get( value2 ) );
if ( comparison != 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ default boolean anyRequiresAggregateColumnWriter() {

@Override
default boolean hasPartitionedSelectionMapping() {
for ( AttributeMapping attributeMapping : getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeMapping.hasPartitionedSelectionMapping() ) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public AttributeMapping get(final int i) {
return list[i]; //intentional unguarded array access: let it explode
}

@Override
public Iterator<AttributeMapping> iterator() {
return new AttributeMappingIterator();
}

@Override
public void forEach(Consumer<? super AttributeMapping> attributeMappingConsumer) {
for ( AttributeMapping o : list ) {
Expand All @@ -57,16 +52,15 @@ public void indexedForEach(final IndexedConsumer<? super AttributeMapping> consu
}
}

private final class AttributeMappingIterator implements Iterator<AttributeMapping> {
//Intentionally not implementing Iterator
public final class AttributeMappingIterator {

private int idx = 0;

@Override
public boolean hasNext() {
return idx < ImmutableAttributeMappingList.this.list.length;
}

@Override
public AttributeMapping next() {
return ImmutableAttributeMappingList.this.list[idx++];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.hibernate.metamodel.mapping.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -19,7 +18,7 @@

/**
* This mutable representation of AttributeMappingsList is meant to
* exist temporarily to assit migration to the new contract.
* exist temporarily to assist migration to the new contract.
* @deprecated Please get rid of it: such collections should be immutable.
*/
@Deprecated
Expand All @@ -41,11 +40,6 @@ public AttributeMapping get(int idx) {
return asAttributeMapping( this.list.get( idx ) );
}

@Override
public Iterator<AttributeMapping> iterator() {
return new AttributeMappingIterator();
}

@Override
public void forEach(final Consumer<? super AttributeMapping> consumer) {
for ( int i = 0; i < list.size(); i++ ) {
Expand Down Expand Up @@ -99,20 +93,4 @@ private static AttributeMapping asAttributeMapping(final Object o) {
}
}

private final class AttributeMappingIterator implements Iterator<AttributeMapping> {

private Iterator iter = MutableAttributeMappingList.this.list.iterator();

@Override
public boolean hasNext() {
return iter.hasNext();
}

@Override
public AttributeMapping next() {
return asAttributeMapping( iter.next() );
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,8 @@ public int[] resolveAttributeIndexes(String[] attributeNames) {
Arrays.sort( attributeNames );

int index = 0;
for ( final AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
if ( isPrefix( attributeMapping, attributeNames[index] ) ) {
fields.add( attributeMapping.getStateArrayPosition() );
index++;
Expand Down Expand Up @@ -2445,7 +2446,8 @@ public int[] resolveDirtyAttributeIndexes(
// Sort attribute names so that we can traverse mappings efficiently
Arrays.sort( attributeNames );
int index = 0;
for ( final AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
final String attributeName = attributeMapping.getAttributeName();
if ( isPrefix( attributeMapping, attributeNames[index] ) ) {
final int position = attributeMapping.getStateArrayPosition();
Expand Down Expand Up @@ -3363,7 +3365,8 @@ protected InsertCoordinator buildInsertCoordinator() {

protected UpdateCoordinator buildUpdateCoordinator() {
// we only have updates to issue for entities with one or more singular attributes
for ( AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeMapping instanceof SingularAttributeMapping ) {
return new UpdateCoordinatorStandard( this, factory );
}
Expand Down Expand Up @@ -4835,7 +4838,8 @@ protected NaturalIdMapping generateNaturalIdMapping(MappingModelCreationProcess
// in the collected names. iterate here because it is already alphabetical

final List<SingularAttributeMapping> collectedAttrMappings = new ArrayList<>();
for ( AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeNames.contains( attributeMapping.getAttributeName() ) ) {
collectedAttrMappings.add( (SingularAttributeMapping) attributeMapping );
}
Expand Down Expand Up @@ -5798,7 +5802,8 @@ public void visitSuperTypeAttributeMappings(Consumer<? super AttributeMapping> a
@Override
public int forEachSelectable(int offset, SelectableConsumer selectableConsumer) {
int span = 0;
for ( AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
span += attributeMapping.forEachSelectable( span + offset, selectableConsumer );
}
return span;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.EntityRowIdMapping;
import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.mapping.SelectableMapping;
Expand Down Expand Up @@ -357,7 +358,9 @@ private void applyTableDeleteDetails(
applyOptimisticLocking( deleteGroupBuilder, loadedState, session );
final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) {
for ( AttributeMapping attributeMapping : persister.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = persister.getAttributeMappings();
for ( int m = 0; m < attributeMappings.size(); m++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( m );
final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hibernate.engine.jdbc.mutation.internal.MutationQueryOptions;
import org.hibernate.engine.jdbc.mutation.internal.NoBatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
Expand Down Expand Up @@ -1110,7 +1109,9 @@ private void applyTableUpdateDetails(
private void applyPartictionKeyRestriction(TableUpdateBuilder<?> tableUpdateBuilder) {
final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) {
for ( AttributeMapping attributeMapping : persister.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = persister.getAttributeMappings();
for ( int m = 0; m < attributeMappings.size(); m++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( m );
final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );
Expand Down Expand Up @@ -1624,7 +1625,9 @@ private MutationOperationGroup buildVersionUpdateGroup() {
private void addPartitionRestriction(TableUpdateBuilderStandard<JdbcMutationOperation> updateBuilder) {
final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) {
for ( AttributeMapping attributeMapping : persister.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = persister.getAttributeMappings();
for ( int m = 0; m < attributeMappings.size(); m++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( m );
final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.hibernate.metamodel.mapping.Association;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
Expand Down Expand Up @@ -166,7 +167,9 @@ else if ( modelPart instanceof DiscriminatedAssociationModelPart ) {
}
else {
final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart;
for ( AttributeMapping mapping : embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( !( mapping instanceof PluralAttributeMapping ) ) {
forEachCteColumn( prefix + "_" + mapping.getAttributeName(), mapping, consumer );
}
Expand All @@ -188,7 +191,9 @@ public static int determineModelPartStartIndex(EntityPersister entityDescriptor,
}
offset += discriminatorMapping.getJdbcTypeCount();
}
for ( AttributeMapping attribute : entityDescriptor.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = entityDescriptor.getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attribute = attributeMappings.get( i );
if ( !( attribute instanceof PluralAttributeMapping ) ) {
final int result = determineModelPartStartIndex( offset, attribute, modelPart );
if ( result < 0 ) {
Expand Down Expand Up @@ -216,7 +221,9 @@ private static int determineModelPartStartIndex(int offset, ModelPart modelPart,
}
else if ( modelPart instanceof EmbeddableValuedModelPart ) {
final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart;
for ( AttributeMapping mapping : embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping mapping = attributeMappings.get( i );
final int result = determineModelPartStartIndex( offset, mapping, modelPartToFind );
if ( result < 0 ) {
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void assertAccessType(SessionFactoryImplementor factory, Class<?> classU
.getMappingMetamodel()
.findEntityDescriptor( classUnderTest.getName() );
final AttributeMappingsList attributeMappings = entityDescriptor.getAttributeMappings();
final AttributeMapping attributeMapping = attributeMappings.iterator().next();
final AttributeMapping attributeMapping = attributeMappings.get( 0 );

final Getter accessGetter = attributeMapping.getPropertyAccess().getGetter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ private Fetchable getFetchable(String attributeName, Class entityClass) {
.getRuntimeMetamodels()
.getMappingMetamodel()
.findEntityDescriptor( entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings();
final AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null;
for ( AttributeMapping mapping : attributeMappings ){
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ void testFetchLoadPlanBuildingWithDeepSubgraph() {
private Fetchable getFetchable(String attributeName, Class entityClass) {
EntityPersister person = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(
entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings();
final AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null;
for ( AttributeMapping mapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ private Fetchable getFetchable(String attributeName, Class entityClass) {
.findEntityDescriptor( entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null;
for ( AttributeMapping mapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping;
}
Expand Down

0 comments on commit 4bb95b7

Please sign in to comment.