@@ -297,7 +297,7 @@ public void addColumn(Column column) {
297297
298298 @ Internal
299299 public void columnRenamed (Column column ) {
300- for ( Map . Entry < String , Column > entry : columns .entrySet () ) {
300+ for ( var entry : columns .entrySet () ) {
301301 if ( entry .getValue () == column ) {
302302 columns .remove ( entry .getKey () );
303303 columns .put ( column .getCanonicalName (), column );
@@ -330,12 +330,10 @@ public Map<String, UniqueKey> getUniqueKeys() {
330330 private int sizeOfUniqueKeyMapOnLastCleanse ;
331331
332332 private void cleanseUniqueKeyMapIfNeeded () {
333- if ( uniqueKeys .size () = = sizeOfUniqueKeyMapOnLastCleanse ) {
334- // nothing to do
335- return ;
333+ if ( uniqueKeys .size () ! = sizeOfUniqueKeyMapOnLastCleanse ) {
334+ cleanseUniqueKeyMap ();
335+ sizeOfUniqueKeyMapOnLastCleanse = uniqueKeys . size () ;
336336 }
337- cleanseUniqueKeyMap ();
338- sizeOfUniqueKeyMapOnLastCleanse = uniqueKeys .size ();
339337 }
340338
341339 private void cleanseUniqueKeyMap () {
@@ -351,57 +349,47 @@ private void cleanseUniqueKeyMap() {
351349 if ( !uniqueKeys .isEmpty () ) {
352350 if ( uniqueKeys .size () == 1 ) {
353351 // we have to worry about condition 2 above, but not condition 1
354- final Map . Entry < String , UniqueKey > uniqueKeyEntry = uniqueKeys .entrySet ().iterator ().next ();
352+ final var uniqueKeyEntry = uniqueKeys .entrySet ().iterator ().next ();
355353 if ( isSameAsPrimaryKeyColumns ( uniqueKeyEntry .getValue () ) ) {
356354 uniqueKeys .remove ( uniqueKeyEntry .getKey () );
357355 }
358356 }
359357 else {
360358 // we have to check both conditions 1 and 2
361- final Iterator <Map .Entry <String ,UniqueKey >> uniqueKeyEntries = uniqueKeys .entrySet ().iterator ();
362- while ( uniqueKeyEntries .hasNext () ) {
363- final Map .Entry <String ,UniqueKey > uniqueKeyEntry = uniqueKeyEntries .next ();
364- final UniqueKey uniqueKey = uniqueKeyEntry .getValue ();
365- boolean removeIt = false ;
366-
367- // Never remove explicit unique keys based on column matching
368- if ( !uniqueKey .isExplicit () ) {
369- // condition 1 : check against other unique keys
370- for ( UniqueKey otherUniqueKey : uniqueKeys .values () ) {
371- // make sure it's not the same unique key
372- if ( uniqueKeyEntry .getValue () == otherUniqueKey ) {
373- continue ;
374- }
375- if ( otherUniqueKey .getColumns ().containsAll ( uniqueKey .getColumns () )
376- && uniqueKey .getColumns ().containsAll ( otherUniqueKey .getColumns () ) ) {
377- removeIt = true ;
378- break ;
379- }
380- }
381- }
359+ //uniqueKeys.remove( uniqueKeyEntry.getKey() );
360+ uniqueKeys .entrySet ().removeIf ( entry -> isRedundantUniqueKey ( entry .getValue () ) );
361+ }
362+ }
363+ }
382364
383- // condition 2 : check against pk
384- if ( !removeIt && isSameAsPrimaryKeyColumns ( uniqueKeyEntry .getValue () ) ) {
385- primaryKey .setOrderingUniqueKey (uniqueKeyEntry .getValue ());
386- removeIt = true ;
387- }
365+ public boolean isRedundantUniqueKey (UniqueKey uniqueKey ) {
388366
389- if ( removeIt ) {
390- //uniqueKeys.remove( uniqueKeyEntry.getKey() );
391- uniqueKeyEntries .remove ();
392- }
367+ // Never remove explicit unique keys based on column matching
368+ if ( !uniqueKey .isExplicit () ) {
369+ // condition 1 : check against other unique keys
370+ for ( UniqueKey otherUniqueKey : uniqueKeys .values () ) {
371+ // make sure it's not the same unique key
372+ if ( uniqueKey != otherUniqueKey
373+ && otherUniqueKey .getColumns ().containsAll ( uniqueKey .getColumns () )
374+ && uniqueKey .getColumns ().containsAll ( otherUniqueKey .getColumns () ) ) {
375+ return true ;
393376 }
394377 }
395378 }
379+
380+ // condition 2 : check against pk
381+ if ( isSameAsPrimaryKeyColumns ( uniqueKey ) ) {
382+ primaryKey .setOrderingUniqueKey ( uniqueKey );
383+ return true ;
384+ }
385+
386+ return false ;
396387 }
397388
398389 private boolean isSameAsPrimaryKeyColumns (UniqueKey uniqueKey ) {
399- if ( primaryKey == null || primaryKey .getColumns ().isEmpty () ) {
400- // happens for many-to-many tables
401- return false ;
402- }
403- return primaryKey .getColumns ().containsAll ( uniqueKey .getColumns () )
404- && primaryKey .getColumns ().size () == uniqueKey .getColumns ().size ();
390+ return primaryKey != null && !primaryKey .getColumns ().isEmpty () // happens for many-to-many tables
391+ && primaryKey .getColumns ().size () == uniqueKey .getColumns ().size ()
392+ && primaryKey .getColumns ().containsAll ( uniqueKey .getColumns () );
405393 }
406394
407395 @ Override
0 commit comments