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

devonfw / IDEasy / 18597783582

17 Oct 2025 03:45PM UTC coverage: 68.522% (+0.09%) from 68.437%
18597783582

Pull #1467

github

web-flow
Merge 4e5b40da9 into 46d29352c
Pull Request #1467: #907 Add yarn and corepack

3467 of 5539 branches covered (62.59%)

Branch coverage included in aggregate %.

9054 of 12734 relevant lines covered (71.1%)

3.13 hits per line

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

85.96
cli/src/main/java/com/devonfw/tools/ide/version/VersionIdentifier.java
1
package com.devonfw.tools.ide.version;
2

3
import java.util.List;
4
import java.util.Objects;
5

6
import com.devonfw.tools.ide.cli.CliException;
7
import com.devonfw.tools.ide.log.IdeLogger;
8
import com.devonfw.tools.ide.tool.ToolCommandlet;
9
import com.fasterxml.jackson.annotation.JsonCreator;
10
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
11

12
/**
13
 * Data-type to represent a {@link VersionIdentifier} in a structured way and allowing {@link #compareVersion(VersionIdentifier) comparison} of
14
 * {@link VersionIdentifier}s.
15
 */
16
public final class VersionIdentifier implements VersionObject<VersionIdentifier>, GenericVersionRange {
17

18
  /** {@link VersionIdentifier} "*" that will resolve to the latest stable version. */
19
  public static final VersionIdentifier LATEST = VersionIdentifier.of("*");
3✔
20

21
  /** {@link VersionIdentifier} "*!" that will resolve to the latest snapshot. */
22
  public static final VersionIdentifier LATEST_UNSTABLE = VersionIdentifier.of("*!");
4✔
23

24
  private final VersionSegment start;
25

26
  private final VersionLetters developmentPhase;
27

28
  private final boolean valid;
29

30
  private VersionIdentifier(VersionSegment start) {
31

32
    super();
2✔
33
    Objects.requireNonNull(start);
3✔
34
    this.start = start;
3✔
35
    boolean isValid = this.start.getSeparator().isEmpty() && this.start.getLettersString().isEmpty();
14✔
36
    boolean hasPositiveNumber = false;
2✔
37
    VersionLetters dev = VersionLetters.EMPTY;
2✔
38
    VersionSegment segment = this.start;
3✔
39
    while (segment != null) {
2✔
40
      if (!segment.isValid()) {
3✔
41
        isValid = false;
3✔
42
      } else if (segment.getNumber() > 0) {
3✔
43
        hasPositiveNumber = true;
2✔
44
      }
45
      VersionLetters segmentLetters = segment.getLetters();
3✔
46
      if (segmentLetters.isDevelopmentPhase()) {
3✔
47
        if (dev.isEmpty()) {
3✔
48
          dev = segmentLetters;
3✔
49
        } else {
50
          dev = VersionLetters.UNDEFINED;
2✔
51
          isValid = false;
2✔
52
        }
53
      }
54
      segment = segment.getNextOrNull();
3✔
55
    }
1✔
56
    this.developmentPhase = dev;
3✔
57
    this.valid = isValid && hasPositiveNumber;
9✔
58
  }
1✔
59

60
  /**
61
   * Resolves a version pattern against a list of available versions.
62
   *
63
   * @param version the version pattern to resolve
64
   * @param versions the
65
   *     {@link com.devonfw.tools.ide.tool.repository.ToolRepository#getSortedVersions(String, String, ToolCommandlet) available versions, sorted in descending
66
   *     order}.
67
   * @param logger the {@link IdeLogger}.
68
   * @return the resolved version
69
   */
70
  public static VersionIdentifier resolveVersionPattern(GenericVersionRange version, List<VersionIdentifier> versions, IdeLogger logger) {
71
    if (version == null) {
2!
72
      version = LATEST;
×
73
    }
74
    if (!version.isPattern()) {
3✔
75
      for (VersionIdentifier vi : versions) {
10!
76
        if (vi.equals(version)) {
4✔
77
          logger.debug("Resolved version {} to version {}", version, vi);
13✔
78
          return vi;
2✔
79
        }
80
      }
1✔
81
    }
82
    for (VersionIdentifier vi : versions) {
10!
83
      if (version.contains(vi)) {
4✔
84
        logger.debug("Resolved version pattern {} to version {}", version, vi);
13✔
85
        return vi;
2✔
86
      }
87
    }
1✔
88
    throw new CliException(
×
89
        "Could not find any version matching '" + version + "' - there are " + versions.size() + " version(s) available but none matched!");
×
90
  }
91

92
  /**
93
   * @return the first {@link VersionSegment} of this {@link VersionIdentifier}. To get other segments use {@link VersionSegment#getNextOrEmpty()} or
94
   *     {@link VersionSegment#getNextOrNull()}.
95
   */
96
  public VersionSegment getStart() {
97

98
    return this.start;
3✔
99
  }
100

101
  /**
102
   * A valid {@link VersionIdentifier} has to meet the following requirements:
103
   * <ul>
104
   * <li>All {@link VersionSegment segments} themselves are {@link VersionSegment#isValid() valid}.</li>
105
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
106
   * {@link VersionSegment#getSeparator() separator} (e.g. ".1.0" or "-1-2" are not considered valid).</li>
107
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
108
   * {@link VersionSegment#getLettersString() letter-sequence} (e.g. "RC1" or "beta" are not considered valid).</li>
109
   * <li>Have at least one {@link VersionSegment segment} with a positive {@link VersionSegment#getNumber() number}
110
   * (e.g. "0.0.0" or "0.alpha" are not considered valid).</li>
111
   * <li>Have at max one {@link VersionSegment segment} with a {@link VersionSegment#getPhase() phase} that is a real
112
   * {@link VersionPhase#isDevelopmentPhase() development phase} (e.g. "1.alpha1.beta2" or "1.0.rc1-milestone2" are not
113
   * considered valid).</li>
114
   * <li>It is NOT a {@link #isPattern() pattern}.</li>
115
   * </ul>
116
   */
117
  @Override
118
  public boolean isValid() {
119

120
    return this.valid;
3✔
121
  }
122

123
  @Override
124
  public boolean isPattern() {
125

126
    VersionSegment segment = this.start;
3✔
127
    while (segment != null) {
2✔
128
      if (segment.isPattern()) {
3✔
129
        return true;
2✔
130
      }
131
      segment = segment.getNextOrNull();
4✔
132
    }
133
    return false;
2✔
134
  }
135

136
  /**
137
   * @return the {@link VersionLetters#isDevelopmentPhase() development phase} of this {@link VersionIdentifier}. Will be {@link VersionLetters#EMPTY} if no
138
   *     development phase is specified in any {@link VersionSegment} and will be {@link VersionLetters#UNDEFINED} if more than one
139
   *     {@link VersionLetters#isDevelopmentPhase() development phase} is specified (e.g. "1.0-alpha1.rc2").
140
   */
141
  public VersionLetters getDevelopmentPhase() {
142

143
    return this.developmentPhase;
3✔
144
  }
145

146
  @Override
147
  public VersionComparisonResult compareVersion(VersionIdentifier other) {
148

149
    if (other == null) {
2!
150
      return VersionComparisonResult.GREATER_UNSAFE;
×
151
    }
152
    VersionSegment thisSegment = this.start;
3✔
153
    VersionSegment otherSegment = other.start;
3✔
154
    VersionComparisonResult result = null;
2✔
155
    boolean unsafe = false;
2✔
156
    boolean todo = true;
2✔
157
    do {
158
      result = thisSegment.compareVersion(otherSegment);
4✔
159
      if (result.isEqual()) {
3✔
160
        if (thisSegment.isEmpty() && otherSegment.isEmpty()) {
6!
161
          todo = false;
3✔
162
        } else if (result.isUnsafe()) {
3!
163
          unsafe = true;
×
164
        }
165
      } else {
166
        todo = false;
2✔
167
      }
168
      thisSegment = thisSegment.getNextOrEmpty();
3✔
169
      otherSegment = otherSegment.getNextOrEmpty();
3✔
170
    } while (todo);
2✔
171
    if (unsafe) {
2!
172
      return result.withUnsafe();
×
173
    }
174
    return result;
2✔
175
  }
176

177
  /**
178
   * @param other the {@link VersionIdentifier} to be matched.
179
   * @return {@code true} if this {@link VersionIdentifier} is equal to the given {@link VersionIdentifier} or this {@link VersionIdentifier} is a pattern
180
   *     version (e.g. "17*" or "17.*") and the given {@link VersionIdentifier} matches to that pattern.
181
   */
182
  public boolean matches(VersionIdentifier other) {
183

184
    if (other == null) {
2✔
185
      return false;
2✔
186
    }
187
    VersionSegment thisSegment = this.start;
3✔
188
    VersionSegment otherSegment = other.start;
3✔
189
    while (true) {
190
      VersionMatchResult matchResult = thisSegment.matches(otherSegment);
4✔
191
      if (matchResult == VersionMatchResult.MATCH) {
3✔
192
        return true;
2✔
193
      } else if (matchResult == VersionMatchResult.MISMATCH) {
3✔
194
        return false;
2✔
195
      }
196
      thisSegment = thisSegment.getNextOrEmpty();
3✔
197
      otherSegment = otherSegment.getNextOrEmpty();
3✔
198
    }
1✔
199
  }
200

201
  @Override
202
  public VersionIdentifier getMin() {
203

204
    return this;
×
205
  }
206

207
  @Override
208
  public VersionIdentifier getMax() {
209

210
    return this;
×
211
  }
212

213
  @Override
214
  public boolean contains(VersionIdentifier version) {
215

216
    return matches(version);
4✔
217
  }
218

219
  @Override
220
  public int hashCode() {
221

222
    VersionSegment segment = this.start;
×
223
    int hash = 1;
×
224
    while (segment != null) {
×
225
      hash = hash * 31 + segment.hashCode();
×
226
      segment = segment.getNextOrNull();
×
227
    }
228
    return hash;
×
229
  }
230

231
  @Override
232
  public boolean equals(Object obj) {
233

234
    if (obj == this) {
3✔
235
      return true;
2✔
236
    } else if (!(obj instanceof VersionIdentifier)) {
3✔
237
      return false;
2✔
238
    }
239
    VersionIdentifier other = (VersionIdentifier) obj;
3✔
240
    return Objects.equals(this.start, other.start);
6✔
241
  }
242

243
  @Override
244
  @JsonSerialize
245
  public String toString() {
246

247
    StringBuilder sb = new StringBuilder();
4✔
248
    VersionSegment segment = this.start;
3✔
249
    while (segment != null) {
2✔
250
      sb.append(segment.toString());
5✔
251
      segment = segment.getNextOrNull();
4✔
252
    }
253
    return sb.toString();
3✔
254
  }
255

256
  /**
257
   * @param version the {@link #toString() string representation} of the {@link VersionIdentifier} to parse.
258
   * @return the parsed {@link VersionIdentifier}.
259
   */
260
  @JsonCreator
261
  public static VersionIdentifier of(String version) {
262

263
    if (version == null) {
2✔
264
      return null;
2✔
265
    } else if (version.equals("latest")) {
4!
266
      return VersionIdentifier.LATEST;
×
267
    }
268
    VersionSegment startSegment = VersionSegment.of(version);
3✔
269
    if (startSegment == null) {
2✔
270
      return null;
2✔
271
    }
272
    return new VersionIdentifier(startSegment);
5✔
273
  }
274

275
}
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

© 2025 Coveralls, Inc