• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 13062819017

30 Jan 2025 10:58PM UTC coverage: 68.293% (-0.3%) from 68.557%
13062819017

Pull #1002

github

web-flow
Merge e11b708f0 into aaf4eb4e0
Pull Request #1002: #786: Upgrade commandlet

2864 of 4597 branches covered (62.3%)

Branch coverage included in aggregate %.

7399 of 10431 relevant lines covered (70.93%)

3.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.48
cli/src/main/java/com/devonfw/tools/ide/url/model/UrlMetadata.java
1
package com.devonfw.tools.ide.url.model;
2

3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9

10
import com.devonfw.tools.ide.cli.CliException;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.url.model.folder.UrlEdition;
13
import com.devonfw.tools.ide.url.model.folder.UrlRepository;
14
import com.devonfw.tools.ide.url.model.folder.UrlTool;
15
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
16
import com.devonfw.tools.ide.version.GenericVersionRange;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

19
/**
20
 * Service to {@link #getEdition(String, String) load} an {@link UrlEdition} to get access to its versions.
21
 */
22
public class UrlMetadata {
23

24
  private final IdeContext context;
25

26
  private final UrlRepository repository;
27

28
  private final Map<String, List<VersionIdentifier>> toolEdition2VersionMap;
29

30
  /**
31
   * The constructor.
32
   *
33
   * @param context the owning {@link IdeContext}.
34
   */
35
  public UrlMetadata(IdeContext context) {
36

37
    super();
2✔
38
    this.context = context;
3✔
39
    this.repository = new UrlRepository(this.context.getUrlsPath());
8✔
40
    this.toolEdition2VersionMap = new HashMap<>();
5✔
41
  }
1✔
42

43
  /**
44
   * @param tool the name of the {@link UrlTool}.
45
   * @param edition the name of the {@link UrlEdition}.
46
   * @return the {@link UrlEdition}. Will be lazily loaded.
47
   */
48
  public UrlEdition getEdition(String tool, String edition) {
49

50
    UrlTool urlTool = this.repository.getOrCreateChild(tool);
6✔
51
    UrlEdition urlEdition = urlTool.getOrCreateChild(edition);
5✔
52
    return urlEdition;
2✔
53
  }
54

55
  /**
56
   * @param tool the name of the {@link UrlTool}.
57
   * @return the sorted {@link List} of {@link String editions} .
58
   */
59
  public List<String> getSortedEditions(String tool) {
60

61
    List<String> list = new ArrayList<>();
4✔
62
    UrlTool urlTool = this.repository.getChild(tool);
6✔
63
    if (urlTool == null) {
2✔
64
      this.context.warning("Can't get sorted editions for tool {} because it does not exist in {}.", tool, this.repository.getPath());
17✔
65
    } else {
66
      for (UrlEdition urlEdition : urlTool.getChildren()) {
11✔
67
        list.add(urlEdition.getName());
5✔
68
      }
1✔
69
    }
70
    Collections.sort(list);
2✔
71
    return Collections.unmodifiableList(list);
3✔
72
  }
73

74
  /**
75
   * @param tool the name of the {@link UrlTool}.
76
   * @param edition the name of the {@link UrlEdition}.
77
   * @return the {@link List} of {@link VersionIdentifier}s sorted descending so the latest version comes first and the oldest comes last.
78
   */
79
  public List<VersionIdentifier> getSortedVersions(String tool, String edition) {
80

81
    String key = tool + "/" + edition;
4✔
82
    return this.toolEdition2VersionMap.computeIfAbsent(key, k -> computeSortedVersions(tool, edition));
15✔
83
  }
84

85
  private List<VersionIdentifier> computeSortedVersions(String tool, String edition) {
86

87
    List<VersionIdentifier> list = new ArrayList<>();
4✔
88
    UrlEdition urlEdition = getEdition(tool, edition);
5✔
89
    urlEdition.load(false);
3✔
90
    for (UrlVersion urlVersion : urlEdition.getChildren()) {
11✔
91
      VersionIdentifier versionIdentifier = urlVersion.getVersionIdentifier();
3✔
92
      list.add(versionIdentifier);
4✔
93
    }
1✔
94
    Collections.sort(list, Comparator.reverseOrder());
3✔
95
    return Collections.unmodifiableList(list);
3✔
96
  }
97

98
  /**
99
   * @param tool the name of the {@link UrlTool}.
100
   * @param edition the name of the {@link UrlEdition}.
101
   * @param version the {@link GenericVersionRange} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for
102
   *     the latest version.
103
   * @return the latest matching {@link VersionIdentifier} for the given {@code tool} and {@code edition}.
104
   */
105
  public VersionIdentifier getVersion(String tool, String edition, GenericVersionRange version) {
106
    List<VersionIdentifier> versions = getSortedVersions(tool, edition);
5✔
107
    return resolveVersionPattern(version, versions);
5✔
108
  }
109

110
  /**
111
   * Resolves a version pattern against a list of available versions.
112
   *
113
   * @param version the version pattern to resolve
114
   * @param versions the available versions, sorted in descending order
115
   * @return the resolved version
116
   */
117
  public VersionIdentifier resolveVersionPattern(GenericVersionRange version, List<VersionIdentifier> versions) {
118
    if (version == null) {
2!
119
      version = VersionIdentifier.LATEST;
×
120
    }
121
    if (!version.isPattern()) {
3✔
122
      return (VersionIdentifier) version;
3✔
123
    }
124
    for (VersionIdentifier vi : versions) {
10!
125
      if (version.contains(vi)) {
4!
126
        this.context.debug("Resolved version pattern {} to version {}", version, vi);
14✔
127
        return vi;
2✔
128
      }
129
    }
×
130
    throw new CliException(
×
131
        "Could not find any version matching '" + version + "' - there are " + versions.size() + " version(s) available but none matched!");
×
132
  }
133

134
  /**
135
   * @param tool the name of the {@link UrlTool}.
136
   * @param edition the name of the {@link UrlEdition}.
137
   * @param version the {@link GenericVersionRange} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for
138
   *     the latest version.
139
   * @return the latest matching {@link UrlVersion} for the given {@code tool} and {@code edition}.
140
   */
141
  public UrlVersion getVersionFolder(String tool, String edition, GenericVersionRange version) {
142

143
    VersionIdentifier resolvedVersion = getVersion(tool, edition, version);
6✔
144
    UrlVersion urlVersion = getEdition(tool, edition).getChild(resolvedVersion.toString());
9✔
145
    if (urlVersion == null) {
2!
146
      throw new CliException("Version " + version + " for tool " + tool + " does not exist in edition " + edition + ".");
×
147
    }
148
    return urlVersion;
2✔
149
  }
150

151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc