11package ai .timefold .solver .core .impl .score .stream .common .bi ;
22
3+ import java .util .ArrayList ;
4+ import java .util .Arrays ;
35import java .util .List ;
46import java .util .function .Function ;
57
1113import ai .timefold .solver .core .impl .score .stream .common .AbstractConstraintStreamTest ;
1214import ai .timefold .solver .core .impl .score .stream .common .ConstraintStreamImplSupport ;
1315import ai .timefold .solver .core .impl .score .stream .common .ConstraintStreamPrecomputeTest ;
14- import ai .timefold .solver .core .impl .util .Pair ;
1516import ai .timefold .solver .core .testdomain .score .lavish .TestdataLavishEntity ;
1617import ai .timefold .solver .core .testdomain .score .lavish .TestdataLavishEntityGroup ;
1718import ai .timefold .solver .core .testdomain .score .lavish .TestdataLavishSolution ;
@@ -354,9 +355,27 @@ public void filter_1_changed_forEachUnfilteredUniquePair() {
354355 assertMatch (entity2 , entity3 ));
355356 }
356357
358+ record Expected <A , B >(A a , B b , Object ... indicted ) {
359+ Expected <A , B > addIndicted (Object indictedObject ) {
360+ for (var object : indicted ) {
361+ if (object == indictedObject ) {
362+ return this ;
363+ }
364+ }
365+ var newIndictments = Arrays .copyOf (indicted , indicted .length + 1 );
366+ newIndictments [indicted .length ] = indictedObject ;
367+ return new Expected <>(a , b , newIndictments );
368+ }
369+ }
370+
371+ <A , B > Expected <A , B > expect (A a , B b , Object ... indicted ) {
372+ return new Expected <>(a , b , indicted );
373+ }
374+
357375 private <A , B > void assertPrecompute (TestdataLavishSolution solution ,
358- List <Pair <A , B >> expectedValues ,
376+ List <Expected <A , B >> expectedValues ,
359377 Function <PrecomputeFactory , BiConstraintStream <A , B >> entityStreamSupplier ) {
378+ expectedValues = new ArrayList <>(expectedValues );
360379 var scoreDirector =
361380 buildScoreDirector (factory -> factory .precompute (entityStreamSupplier )
362381 .ifExists (TestdataLavishEntity .class )
@@ -371,11 +390,16 @@ private <A, B> void assertPrecompute(TestdataLavishSolution solution,
371390 scoreDirector .beforeVariableChanged (entity , "value" );
372391 entity .setValue (solution .getFirstValue ());
373392 scoreDirector .afterVariableChanged (entity , "value" );
393+ var listIterator = expectedValues .listIterator ();
394+ while (listIterator .hasNext ()) {
395+ var expected = listIterator .next ();
396+ listIterator .set (expected .addIndicted (entity ));
397+ }
374398 }
375399
376400 assertScore (scoreDirector , expectedValues .stream ()
377- .map (pair -> new Object [] { pair . key (), pair . value () } )
378- . map ( AbstractConstraintStreamTest :: assertMatch )
401+ .map (expected -> assertMatch ( expected . a , expected . b )
402+ . withIndictedObjects ( expected . indicted ) )
379403 .toArray (AssertableMatch []::new ));
380404 }
381405
@@ -384,15 +408,19 @@ private <A, B> void assertPrecompute(TestdataLavishSolution solution,
384408 public void ifExists () {
385409 var solution = TestdataLavishSolution .generateEmptySolution ();
386410 var entityWithoutGroup = new TestdataLavishEntity ();
411+ entityWithoutGroup .setCode ("A" );
387412 var entityWithGroup = new TestdataLavishEntity ();
413+ entityWithGroup .setCode ("B" );
388414 var entityGroup = new TestdataLavishEntityGroup ();
415+ entityGroup .setCode ("C" );
389416 entityWithGroup .setEntityGroup (entityGroup );
390417 solution .getEntityList ().addAll (List .of (entityWithoutGroup , entityWithGroup ));
391418 solution .getEntityGroupList ().add (entityGroup );
392419 var value = new TestdataLavishValue ();
420+ value .setCode ("D" );
393421 solution .getValueList ().add (value );
394422
395- assertPrecompute (solution , List .of (new Pair <> (entityWithGroup , value )),
423+ assertPrecompute (solution , List .of (expect (entityWithGroup , value , entityWithGroup , value , entityGroup )),
396424 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
397425 .join (TestdataLavishValue .class )
398426 .ifExists (TestdataLavishEntityGroup .class , Joiners .equal (
@@ -413,7 +441,7 @@ public void ifNotExists() {
413441 var value = new TestdataLavishValue ();
414442 solution .getValueList ().add (value );
415443
416- assertPrecompute (solution , List .of (new Pair <>( entityWithoutGroup , value )),
444+ assertPrecompute (solution , List .of (expect ( entityWithoutGroup , value , entityWithoutGroup , value )),
417445 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
418446 .join (TestdataLavishValue .class )
419447 .ifNotExists (TestdataLavishEntityGroup .class , Joiners .equal (
@@ -434,7 +462,7 @@ public void groupBy() {
434462 var value = new TestdataLavishValue ();
435463 solution .getValueList ().add (value );
436464
437- assertPrecompute (solution , List .of (new Pair <> (entityGroup , 1L )),
465+ assertPrecompute (solution , List .of (expect (entityGroup , 1L )),
438466 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
439467 .filter (entity -> entity .getEntityGroup () != null )
440468 .groupBy (TestdataLavishEntity ::getEntityGroup , ConstraintCollectors .count ()));
@@ -453,8 +481,8 @@ public void flatten() {
453481 var value = new TestdataLavishValue ();
454482 solution .getValueList ().add (value );
455483
456- assertPrecompute (solution , List .of (new Pair <> (entityWithoutGroup , entityWithoutGroup ),
457- new Pair <> (entityWithGroup , entityWithoutGroup )),
484+ assertPrecompute (solution , List .of (expect (entityWithoutGroup , entityWithoutGroup ),
485+ expect (entityWithGroup , entityWithoutGroup )),
458486 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
459487 .flatten (List ::of ));
460488 }
@@ -475,8 +503,8 @@ record ValueHolder(int value) {
475503 var value = new TestdataLavishValue ();
476504 solution .getValueList ().add (value );
477505
478- assertPrecompute (solution , List .of (new Pair <> (entity1 , new ValueHolder (entity1 .getIntegerProperty ())),
479- new Pair <> (entity2 , new ValueHolder (entity2 .getIntegerProperty ()))),
506+ assertPrecompute (solution , List .of (expect (entity1 , new ValueHolder (entity1 .getIntegerProperty ())),
507+ expect (entity2 , new ValueHolder (entity2 .getIntegerProperty ()))),
480508 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
481509 .flatten (entity -> List .of (new ValueHolder (entity .getIntegerProperty ()))));
482510 }
@@ -494,8 +522,8 @@ public void flattenLast() {
494522 var value = new TestdataLavishValue ();
495523 solution .getValueList ().add (value );
496524
497- assertPrecompute (solution , List .of (new Pair <>( entityWithoutGroup , value ),
498- new Pair <>( entityWithGroup , value )),
525+ assertPrecompute (solution , List .of (expect ( entityWithoutGroup , value , entityWithoutGroup , value ),
526+ expect ( entityWithGroup , value , entityWithGroup , value )),
499527 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
500528 .groupBy (ConstraintCollectors .toList ())
501529 .flattenLast (entityList -> entityList )
@@ -518,8 +546,8 @@ record ValueHolder(int value) {
518546 var value = new TestdataLavishValue ();
519547 solution .getValueList ().add (value );
520548
521- assertPrecompute (solution , List .of (new Pair <> (new ValueHolder (1 ), value ),
522- new Pair <> (new ValueHolder (2 ), value )),
549+ assertPrecompute (solution , List .of (expect (new ValueHolder (1 ), value , entity1 , value ),
550+ expect (new ValueHolder (2 ), value , entity2 , value )),
523551 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
524552 .groupBy (ConstraintCollectors .toList ())
525553 .flattenLast (entityList -> entityList
@@ -544,8 +572,8 @@ public void map() {
544572 var value = new TestdataLavishValue ();
545573 solution .getValueList ().add (value );
546574
547- assertPrecompute (solution , List .of (new Pair <> (entityGroup , value ),
548- new Pair <> (entityGroup , value )),
575+ assertPrecompute (solution , List .of (expect (entityGroup , value , entityWithGroup1 , value ),
576+ expect (entityGroup , value , entityWithGroup2 , value )),
549577 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
550578 .join (TestdataLavishValue .class )
551579 .filter ((entity , joinedValue ) -> entity .getEntityGroup () != null )
@@ -558,15 +586,21 @@ public void map() {
558586 public void concat () {
559587 var solution = TestdataLavishSolution .generateEmptySolution ();
560588 var entityWithoutGroup = new TestdataLavishEntity ();
589+ entityWithoutGroup .setCode ("A" );
561590 var entityWithGroup = new TestdataLavishEntity ();
591+ entityWithGroup .setCode ("B" );
562592 var entityGroup = new TestdataLavishEntityGroup ();
593+ entityGroup .setCode ("C" );
563594 entityWithGroup .setEntityGroup (entityGroup );
564595 solution .getEntityList ().addAll (List .of (entityWithoutGroup , entityWithGroup ));
565596 solution .getEntityGroupList ().add (entityGroup );
566597 var value = new TestdataLavishValue ();
598+ value .setCode ("D" );
567599 solution .getValueList ().add (value );
568600
569- assertPrecompute (solution , List .of (new Pair <>(entityWithoutGroup , value ), new Pair <>(entityWithGroup , value )),
601+ assertPrecompute (solution , List .of (
602+ expect (entityWithoutGroup , value , entityWithoutGroup , value ),
603+ expect (entityWithGroup , value , entityWithGroup , value )),
570604 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
571605 .join (TestdataLavishValue .class )
572606 .filter ((entity , joinedValue ) -> entity .getEntityGroup () == null )
@@ -590,7 +624,7 @@ public void distinct() {
590624 var value = new TestdataLavishValue ();
591625 solution .getValueList ().add (value );
592626
593- assertPrecompute (solution , List .of (new Pair <> (entityGroup , value )),
627+ assertPrecompute (solution , List .of (expect (entityGroup , value , entityWithGroup1 , entityWithGroup2 , value )),
594628 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
595629 .join (TestdataLavishValue .class )
596630 .filter ((entity , joinedValue ) -> entity .getEntityGroup () != null )
@@ -615,9 +649,9 @@ public void complement() {
615649 solution .getValueList ().add (value );
616650
617651 assertPrecompute (solution , List .of (
618- new Pair <>( entityWithGroup1 , value ),
619- new Pair <>( entityWithGroup2 , value ),
620- new Pair <> (entityWithoutGroup , null )),
652+ expect ( entityWithGroup1 , value , entityWithGroup1 , value ),
653+ expect ( entityWithGroup2 , value , entityWithGroup2 , value ),
654+ expect (entityWithoutGroup , null , entityWithoutGroup )),
621655 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
622656 .join (TestdataLavishValue .class )
623657 .filter ((entity , joinedValue ) -> entity .getEntityGroup () != null )
0 commit comments