Skip to content

Commit 87855ea

Browse files
committed
Calculate all eagerly
1 parent 98b403e commit 87855ea

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

shared/src/main/java/eu/maveniverse/maven/toolbox/shared/internal/resolver/ScopeManagerImpl.java

+50-30
Original file line numberDiff line numberDiff line change
@@ -347,38 +347,12 @@ public Collection<ResolutionScope> getResolutionScopeUniverse() {
347347

348348
@Override
349349
public int getDependencyScopeWidth(DependencyScope dependencyScope) {
350-
int result = 0;
351-
if (dependencyScope.isTransitive()) {
352-
result += 1000;
353-
}
354-
for (BuildScope buildScope :
355-
buildScopeSource.query(translate(dependencyScope).getPresence())) {
356-
result += 1000
357-
/ buildScope.getProjectPaths().stream()
358-
.map(ProjectPath::order)
359-
.reduce(0, Integer::sum);
360-
}
361-
return result;
350+
return translate(dependencyScope).getWidth();
362351
}
363352

364353
@Override
365354
public Optional<BuildScope> getDependencyScopeMainProjectBuildScope(DependencyScope dependencyScope) {
366-
for (ProjectPath projectPath : buildScopeSource.allProjectPaths().stream()
367-
.sorted(Comparator.comparing(ProjectPath::order))
368-
.toList()) {
369-
for (BuildPath buildPath : buildScopeSource.allBuildPaths().stream()
370-
.sorted(Comparator.comparing(BuildPath::order))
371-
.toList()) {
372-
for (BuildScope buildScope :
373-
buildScopeSource.query(translate(dependencyScope).getPresence())) {
374-
if (buildScope.getProjectPaths().contains(projectPath)
375-
&& buildScope.getBuildPaths().contains(buildPath)) {
376-
return Optional.of(buildScope);
377-
}
378-
}
379-
}
380-
}
381-
return Optional.empty();
355+
return translate(dependencyScope).getMainBuildScope();
382356
}
383357

384358
@Override
@@ -434,6 +408,40 @@ private Set<DependencyScope> collectScopes(Collection<BuildScopeQuery> wantedPre
434408
return result;
435409
}
436410

411+
private int calculateDependencyScopeWidth(DependencyScopeImpl dependencyScope) {
412+
int result = 0;
413+
if (dependencyScope.isTransitive()) {
414+
result += 1000;
415+
}
416+
for (BuildScope buildScope :
417+
buildScopeSource.query(dependencyScope.getPresence())) {
418+
result += 1000
419+
/ buildScope.getProjectPaths().stream()
420+
.map(ProjectPath::order)
421+
.reduce(0, Integer::sum);
422+
}
423+
return result;
424+
}
425+
426+
private Optional<BuildScope> calculateMainProjectBuildScope(DependencyScopeImpl dependencyScope) {
427+
for (ProjectPath projectPath : buildScopeSource.allProjectPaths().stream()
428+
.sorted(Comparator.comparing(ProjectPath::order))
429+
.toList()) {
430+
for (BuildPath buildPath : buildScopeSource.allBuildPaths().stream()
431+
.sorted(Comparator.comparing(BuildPath::order))
432+
.toList()) {
433+
for (BuildScope buildScope :
434+
buildScopeSource.query(dependencyScope.getPresence())) {
435+
if (buildScope.getProjectPaths().contains(projectPath)
436+
&& buildScope.getBuildPaths().contains(buildPath)) {
437+
return Optional.of(buildScope);
438+
}
439+
}
440+
}
441+
}
442+
return Optional.empty();
443+
}
444+
437445
private Set<String> getDirectlyIncludedLabels(ResolutionScopeImpl resolutionScope) {
438446
return resolutionScope.getDirectlyIncluded().stream()
439447
.map(DependencyScope::getId)
@@ -479,15 +487,20 @@ public String toString() {
479487
return id;
480488
}
481489

482-
private static final class DependencyScopeImpl implements DependencyScope {
490+
private final class DependencyScopeImpl implements DependencyScope {
483491
private final String id;
484492
private final boolean transitive;
485493
private final Set<BuildScopeQuery> presence;
494+
private final Optional<BuildScope> mainBuildScope;
495+
private final int width;
496+
486497

487498
private DependencyScopeImpl(String id, boolean transitive, Collection<BuildScopeQuery> presence) {
488499
this.id = requireNonNull(id, "id");
489500
this.transitive = transitive;
490501
this.presence = Set.copyOf(presence);
502+
this.mainBuildScope = calculateMainProjectBuildScope(this);
503+
this.width = calculateDependencyScopeWidth(this);
491504
}
492505

493506
@Override
@@ -504,6 +517,14 @@ public Set<BuildScopeQuery> getPresence() {
504517
return presence;
505518
}
506519

520+
public Optional<BuildScope> getMainBuildScope() {
521+
return mainBuildScope;
522+
}
523+
524+
public int getWidth() {
525+
return width;
526+
}
527+
507528
@Override
508529
public boolean equals(Object o) {
509530
if (this == o) return true;
@@ -547,7 +568,6 @@ enum Mode {
547568
private final Set<BuildScopeQuery> wantedPresence;
548569
private final Set<DependencyScope> directlyIncluded;
549570
private final Set<DependencyScope> transitivelyExcluded;
550-
551571
private ResolutionScopeImpl(
552572
String id,
553573
Mode mode,

0 commit comments

Comments
 (0)