Skip to content

Commit c31cb54

Browse files
committed
added comments
1 parent 74d2de4 commit c31cb54

File tree

1 file changed

+113
-7
lines changed
  • spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping

1 file changed

+113
-7
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/AggregatePath.java

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,20 @@ static ColumnInfo of(AggregatePath path) {
432432
}
433433

434434
/**
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
437440
**/
438441
class ColumnInfos {
439442

440443
private final AggregatePath basePath;
441444
private final Map<AggregatePath, ColumnInfo> columnInfos;
442445

443446
/**
447+
* Creates a new ColumnInfos instances based on the arguments.
448+
*
444449
* @param basePath The path on which all other paths in the other argument are based on. For the typical case of a
445450
* composite id, this would be the path to the composite ids.
446451
* @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
451456
this.columnInfos = columnInfos;
452457
}
453458

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<>());
456468
}
457469

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+
*/
458476
public ColumnInfo unique() {
459477

460478
Collection<ColumnInfo> values = columnInfos.values();
461479
Assert.state(values.size() == 1, "ColumnInfo is not unique");
462480
return values.iterator().next();
463481
}
464482

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+
*/
465489
public ColumnInfo any() {
466490

467491
Collection<ColumnInfo> values = columnInfos.values();
468492
return values.iterator().next();
469493
}
470494

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+
*/
471500
public boolean isEmpty() {
472501
return columnInfos.isEmpty();
473502
}
474503

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+
*/
475511
public <T> List<T> toList(Function<ColumnInfo, T> mapper) {
476512
return columnInfos.values().stream().map(mapper).toList();
477513
}
@@ -506,55 +542,125 @@ public <T> T reduce(T identity, BiFunction<AggregatePath, ColumnInfo, T> accumul
506542
return result;
507543
}
508544

545+
/**
546+
* Calls the consumer for each pair of {@link AggregatePath} and {@literal ColumnInfo}.
547+
*
548+
* @param consumer the function to call.
549+
*/
509550
public void forEach(BiConsumer<AggregatePath, ColumnInfo> consumer) {
510551
columnInfos.forEach(consumer);
511552
}
512553

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+
*/
513562
public <T> T any(BiFunction<AggregatePath, ColumnInfo, T> mapper) {
514563

515564
Map.Entry<AggregatePath, ColumnInfo> any = columnInfos.entrySet().iterator().next();
516565
return mapper.apply(any.getKey(), any.getValue());
517566
}
518567

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+
*/
519574
public ColumnInfo get(AggregatePath path) {
520575
return columnInfos.get(path);
521576
}
522577

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+
*/
523584
public AggregatePath fullPath(AggregatePath ap) {
524585
return basePath.append(ap);
525586
}
526587

588+
/**
589+
* Number of {@literal ColumnInfo} elements in this instance.
590+
*
591+
* @return the size of the collection of {@literal ColumnInfo}.
592+
*/
527593
public int size() {
528594
return columnInfos.size();
529595
}
530596
}
531597

598+
/**
599+
* A builder for {@link ColumnInfos} instances.
600+
*
601+
* @author Jens Schauder
602+
*/
532603
class ColumInfosBuilder {
533604
private final AggregatePath basePath;
534605

535606
private final Map<AggregatePath, ColumnInfo> columnInfoMap = new TreeMap<>();
536607

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+
*/
537613
public ColumInfosBuilder(AggregatePath basePath) {
538614
this.basePath = basePath;
539615
}
540616

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+
*/
541624
void add(AggregatePath path, SqlIdentifier name, SqlIdentifier alias) {
542625
add(path, new ColumnInfo(name, alias));
543626
}
544627

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+
*/
545635
public void add(RelationalPersistentProperty property, SqlIdentifier name, SqlIdentifier alias) {
546636
add(basePath.append(property), name, alias);
547637
}
548638

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+
*/
549653
ColumnInfos build() {
550654
return new ColumnInfos(basePath, columnInfoMap);
551655
}
552656

553-
public void add(AggregatePath path, ColumnInfo columnInfo) {
554-
columnInfoMap.put(path.substract(basePath), columnInfo);
555-
}
556657
}
557658

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+
*/
558664
@Nullable
559665
AggregatePath substract(@Nullable AggregatePath basePath);
560666

0 commit comments

Comments
 (0)