@@ -432,15 +432,20 @@ static ColumnInfo of(AggregatePath path) {
432
432
}
433
433
434
434
/**
435
- * A group of {@link ColumnInfo} values referenced by there respective {@link AggregatePath}. This is relevant for
436
- * composite ids and references to such ids.
435
+ * A group of {@link ColumnInfo} values referenced by there respective {@link AggregatePath}. It is used in a similar
436
+ * way as {@literal ColumnInfo} when one needs to consider more than a single column. This is relevant for composite
437
+ * ids and references to such ids.
438
+ *
439
+ * @author Jens Schauder
437
440
**/
438
441
class ColumnInfos {
439
442
440
443
private final AggregatePath basePath ;
441
444
private final Map <AggregatePath , ColumnInfo > columnInfos ;
442
445
443
446
/**
447
+ * Creates a new ColumnInfos instances based on the arguments.
448
+ *
444
449
* @param basePath The path on which all other paths in the other argument are based on. For the typical case of a
445
450
* composite id, this would be the path to the composite ids.
446
451
* @param columnInfos A map, mapping {@literal AggregatePath} instances to the respective {@literal ColumnInfo}
@@ -451,27 +456,58 @@ private ColumnInfos(AggregatePath basePath, Map<AggregatePath, ColumnInfo> colum
451
456
this .columnInfos = columnInfos ;
452
457
}
453
458
454
- public static ColumnInfos empty (AggregatePath base ) {
455
- return new ColumnInfos (base , new HashMap <>());
459
+ /**
460
+ * An empty {@literal ColumnInfos} instance with a fixed base path. Useful as a base when collecting
461
+ * {@link ColumnInfo} instances into an {@literal ColumnInfos} instance.
462
+ *
463
+ * @param basePath The path on which paths in the {@literal ColumnInfos} or derived objects will be based on.
464
+ * @return an empty instance save the {@literal basePath}.
465
+ */
466
+ public static ColumnInfos empty (AggregatePath basePath ) {
467
+ return new ColumnInfos (basePath , new HashMap <>());
456
468
}
457
469
470
+ /**
471
+ * If this instance contains exactly one {@link ColumnInfo} it will be returned.
472
+ *
473
+ * @return the unique {@literal ColumnInfo} if present.
474
+ * @throws IllegalStateException if the number of contained {@literal ColumnInfo} instances is not exactly 1.
475
+ */
458
476
public ColumnInfo unique () {
459
477
460
478
Collection <ColumnInfo > values = columnInfos .values ();
461
479
Assert .state (values .size () == 1 , "ColumnInfo is not unique" );
462
480
return values .iterator ().next ();
463
481
}
464
482
483
+ /**
484
+ * Any of the contained {@link ColumnInfo} instances.
485
+ *
486
+ * @return a {@link ColumnInfo} instance.
487
+ * @throws java.util.NoSuchElementException if no instance is available.
488
+ */
465
489
public ColumnInfo any () {
466
490
467
491
Collection <ColumnInfo > values = columnInfos .values ();
468
492
return values .iterator ().next ();
469
493
}
470
494
495
+ /**
496
+ * Checks if {@literal this} instance is empty, i.e. does not contain any {@link ColumnInfo} instance.
497
+ *
498
+ * @return {@literal true} iff the collection of {@literal ColumnInfo} is empty.
499
+ */
471
500
public boolean isEmpty () {
472
501
return columnInfos .isEmpty ();
473
502
}
474
503
504
+ /**
505
+ * Applies a function to all the {@link ColumnInfo} instances and returns the result in a list.
506
+ *
507
+ * @param mapper the function to be applied
508
+ * @return the list of results from mapper.
509
+ * @param <T> the type returned by {@literal mapper} and contained in the resulting {@literal List}
510
+ */
475
511
public <T > List <T > toList (Function <ColumnInfo , T > mapper ) {
476
512
return columnInfos .values ().stream ().map (mapper ).toList ();
477
513
}
@@ -506,55 +542,125 @@ public <T> T reduce(T identity, BiFunction<AggregatePath, ColumnInfo, T> accumul
506
542
return result ;
507
543
}
508
544
545
+ /**
546
+ * Calls the consumer for each pair of {@link AggregatePath} and {@literal ColumnInfo}.
547
+ *
548
+ * @param consumer the function to call.
549
+ */
509
550
public void forEach (BiConsumer <AggregatePath , ColumnInfo > consumer ) {
510
551
columnInfos .forEach (consumer );
511
552
}
512
553
554
+ /**
555
+ * Calls the {@literal mapper} for each pair one pair of {@link AggregatePath} and {@link ColumnInfo}, if there is
556
+ * any.
557
+ *
558
+ * @param mapper the function to call.
559
+ * @return the result of the mapper
560
+ * @throws java.util.NoSuchElementException if this {@literal ColumnInfo} is empty.
561
+ */
513
562
public <T > T any (BiFunction <AggregatePath , ColumnInfo , T > mapper ) {
514
563
515
564
Map .Entry <AggregatePath , ColumnInfo > any = columnInfos .entrySet ().iterator ().next ();
516
565
return mapper .apply (any .getKey (), any .getValue ());
517
566
}
518
567
568
+ /**
569
+ * Gets the {@link ColumnInfo} for the provided {@link AggregatePath}
570
+ *
571
+ * @param path for which to return the {@literal ColumnInfo}
572
+ * @return {@literal ColumnInfo} for the given path.
573
+ */
519
574
public ColumnInfo get (AggregatePath path ) {
520
575
return columnInfos .get (path );
521
576
}
522
577
578
+ /**
579
+ * Constructs an {@link AggregatePath} from the {@literal basePath} and the provided argument.
580
+ *
581
+ * @param ap {@literal AggregatePath} to be appended to the {@literal basePath}.
582
+ * @return the combined (@literal AggregatePath}
583
+ */
523
584
public AggregatePath fullPath (AggregatePath ap ) {
524
585
return basePath .append (ap );
525
586
}
526
587
588
+ /**
589
+ * Number of {@literal ColumnInfo} elements in this instance.
590
+ *
591
+ * @return the size of the collection of {@literal ColumnInfo}.
592
+ */
527
593
public int size () {
528
594
return columnInfos .size ();
529
595
}
530
596
}
531
597
598
+ /**
599
+ * A builder for {@link ColumnInfos} instances.
600
+ *
601
+ * @author Jens Schauder
602
+ */
532
603
class ColumInfosBuilder {
533
604
private final AggregatePath basePath ;
534
605
535
606
private final Map <AggregatePath , ColumnInfo > columnInfoMap = new TreeMap <>();
536
607
608
+ /**
609
+ * Start construction with just the {@literal basePath} which all other paths are build upon.
610
+ *
611
+ * @param basePath must not be null.
612
+ */
537
613
public ColumInfosBuilder (AggregatePath basePath ) {
538
614
this .basePath = basePath ;
539
615
}
540
616
617
+ /**
618
+ * Adds a {@link ColumnInfo} to the {@link ColumnInfos} under construction.
619
+ *
620
+ * @param path referencing the {@literal ColumnInfo}.
621
+ * @param name of the column.
622
+ * @param alias alias for the column.
623
+ */
541
624
void add (AggregatePath path , SqlIdentifier name , SqlIdentifier alias ) {
542
625
add (path , new ColumnInfo (name , alias ));
543
626
}
544
627
628
+ /**
629
+ * Adds a {@link ColumnInfo} to the {@link ColumnInfos} under construction.
630
+ *
631
+ * @param property referencing the {@literal ColumnInfo}.
632
+ * @param name of the column.
633
+ * @param alias alias for the column.
634
+ */
545
635
public void add (RelationalPersistentProperty property , SqlIdentifier name , SqlIdentifier alias ) {
546
636
add (basePath .append (property ), name , alias );
547
637
}
548
638
639
+ /**
640
+ * Adds a {@link ColumnInfo} to the {@link ColumnInfos} under construction.
641
+ *
642
+ * @param path the path referencing the {@literal ColumnInfo}
643
+ * @param columnInfo the {@literal ColumnInfo} added.
644
+ *
645
+ */
646
+ public void add (AggregatePath path , ColumnInfo columnInfo ) {
647
+ columnInfoMap .put (path .substract (basePath ), columnInfo );
648
+ }
649
+ /**
650
+ * Build the final {@link ColumnInfos} instance.
651
+ * @return a {@literal ColumnInfos} instance containing all the added {@link ColumnInfo} instances.
652
+ */
549
653
ColumnInfos build () {
550
654
return new ColumnInfos (basePath , columnInfoMap );
551
655
}
552
656
553
- public void add (AggregatePath path , ColumnInfo columnInfo ) {
554
- columnInfoMap .put (path .substract (basePath ), columnInfo );
555
- }
556
657
}
557
658
659
+ /**
660
+ * Substract the {@literal basePath} from {@literal this} {@literal AggregatePath} by removing the {@literal basePath} from the beginning of {@literal this}.
661
+ * @param basePath the path to be removed.
662
+ * @return an AggregatePath that ends like the original {@literal AggregatePath} but has {@literal basePath} removed from the beginning.
663
+ */
558
664
@ Nullable
559
665
AggregatePath substract (@ Nullable AggregatePath basePath );
560
666
0 commit comments