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

devonfw / IDEasy / 24984011544

27 Apr 2026 08:14AM UTC coverage: 70.718% (+0.08%) from 70.641%
24984011544

Pull #1856

github

web-flow
Merge f59d7af1d into 344d6c0f7
Pull Request #1856: #1643 improve ux on syntax error

4403 of 6878 branches covered (64.02%)

Branch coverage included in aggregate %.

11348 of 15395 relevant lines covered (73.71%)

3.12 hits per line

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

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

3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Objects;
6
import java.util.stream.Collectors;
7

8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10

11
import com.devonfw.tools.ide.cli.CliException;
12
import com.devonfw.tools.ide.tool.ToolCommandlet;
13
import com.fasterxml.jackson.annotation.JsonCreator;
14
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
15

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

22
  private static final Logger LOG = LoggerFactory.getLogger(VersionIdentifier.class);
3✔
23

24
  /** {@link VersionIdentifier} "*" that will resolve to the latest stable version. */
25
  public static final VersionIdentifier LATEST = new VersionIdentifier(VersionSegment.of("*"));
6✔
26

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

30
  private final VersionSegment start;
31

32
  private final VersionLetters developmentPhase;
33

34
  private final boolean valid;
35

36
  private VersionIdentifier(VersionSegment start) {
37

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

66
  /**
67
   * Resolves a version pattern against a list of available versions.
68
   *
69
   * @param version the version pattern to resolve
70
   * @param versions the
71
   *     {@link com.devonfw.tools.ide.tool.repository.ToolRepository#getSortedVersions(String, String, ToolCommandlet) available versions, sorted in descending
72
   *     order}.
73
   * @return the resolved version
74
   */
75
  public static VersionIdentifier resolveVersionPattern(GenericVersionRange version, List<VersionIdentifier> versions) {
76
    if (version == null) {
2!
77
      version = LATEST;
×
78
    }
79
    if (!version.isPattern()) {
3✔
80
      for (VersionIdentifier vi : versions) {
10!
81
        if (vi.equals(version)) {
4✔
82
          LOG.debug("Resolved version {} to version {}", version, vi);
5✔
83
          return vi;
2✔
84
        }
85
      }
1✔
86
    }
87
    for (VersionIdentifier vi : versions) {
10✔
88
      if (version.contains(vi)) {
4✔
89
        LOG.debug("Resolved version pattern {} to version {}", version, vi);
5✔
90
        return vi;
2✔
91
      }
92
    }
1✔
93
    List<VersionIdentifier> closest = findClosestVersions(version, versions, 5);
5✔
94
    String closestStr = closest.stream().map(Object::toString).collect(Collectors.joining(", "));
9✔
95
    throw new CliException(
5✔
96
        "Could not find any version matching '" + version + "' - there are " + versions.size()
5✔
97
            + " version(s) available but none matched!\nDid you mean one of: " + closestStr + "?");
98
  }
99

100
  private static List<VersionIdentifier> findClosestVersions(GenericVersionRange version, List<VersionIdentifier> versions, int maxCount) {
101

102
    if (version instanceof VersionIdentifier vi && !vi.isPattern()) {
9!
103
      long requestedMajor = vi.getStart().getNumber();
×
104
      List<VersionIdentifier> majorMatches = new ArrayList<>();
×
105
      for (VersionIdentifier v : versions) {
×
106
        if (v.getStart().getNumber() == requestedMajor) {
×
107
          majorMatches.add(v);
×
108
          if (majorMatches.size() >= maxCount) {
×
109
            break;
×
110
          }
111
        }
112
      }
×
113
      if (!majorMatches.isEmpty()) {
×
114
        return majorMatches;
×
115
      }
116
    }
117
    return versions.size() <= maxCount ? versions : versions.subList(0, maxCount);
7!
118
  }
119

120
  /**
121
   * @return the first {@link VersionSegment} of this {@link VersionIdentifier}. To get other segments use {@link VersionSegment#getNextOrEmpty()} or
122
   *     {@link VersionSegment#getNextOrNull()}.
123
   */
124
  public VersionSegment getStart() {
125

126
    return this.start;
3✔
127
  }
128

129
  /**
130
   * A valid {@link VersionIdentifier} has to meet the following requirements:
131
   * <ul>
132
   * <li>All {@link VersionSegment segments} themselves are {@link VersionSegment#isValid() valid}.</li>
133
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
134
   * {@link VersionSegment#getSeparator() separator} (e.g. ".1.0" or "-1-2" are not considered valid).</li>
135
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
136
   * {@link VersionSegment#getLettersString() letter-sequence} (e.g. "RC1" or "beta" are not considered valid).</li>
137
   * <li>Have at least one {@link VersionSegment segment} with a positive {@link VersionSegment#getNumber() number}
138
   * (e.g. "0.0.0" or "0.alpha" are not considered valid).</li>
139
   * <li>Have at max one {@link VersionSegment segment} with a {@link VersionSegment#getPhase() phase} that is a real
140
   * {@link VersionPhase#isDevelopmentPhase() development phase} (e.g. "1.alpha1.beta2" or "1.0.rc1-milestone2" are not
141
   * considered valid).</li>
142
   * <li>It is NOT a {@link #isPattern() pattern}.</li>
143
   * </ul>
144
   */
145
  @Override
146
  public boolean isValid() {
147

148
    return this.valid;
3✔
149
  }
150

151
  @Override
152
  public boolean isPattern() {
153

154
    VersionSegment segment = this.start;
3✔
155
    while (segment != null) {
2✔
156
      if (segment.isPattern()) {
3✔
157
        return true;
2✔
158
      }
159
      segment = segment.getNextOrNull();
4✔
160
    }
161
    return false;
2✔
162
  }
163

164

165
  /**
166
   * @return {@code true} if this is a stable version, {@code false} otherwise.
167
   * @see VersionLetters#isStable()
168
   */
169
  public boolean isStable() {
170

171
    return this.developmentPhase.isStable();
4✔
172
  }
173

174
  /**
175
   * @return the {@link VersionLetters#isDevelopmentPhase() development phase} of this {@link VersionIdentifier}. Will be {@link VersionLetters#EMPTY} if no
176
   *     development phase is specified in any {@link VersionSegment} and will be {@link VersionLetters#UNDEFINED} if more than one
177
   *     {@link VersionLetters#isDevelopmentPhase() development phase} is specified (e.g. "1.0-alpha1.rc2").
178
   */
179
  public VersionLetters getDevelopmentPhase() {
180

181
    return this.developmentPhase;
3✔
182
  }
183

184
  @Override
185
  public VersionComparisonResult compareVersion(VersionIdentifier other) {
186

187
    if (other == null) {
2✔
188
      return VersionComparisonResult.GREATER_UNSAFE;
2✔
189
    }
190
    VersionSegment thisSegment = this.start;
3✔
191
    VersionSegment otherSegment = other.start;
3✔
192
    VersionComparisonResult result = null;
2✔
193
    boolean unsafe = false;
2✔
194
    boolean todo = true;
2✔
195
    do {
196
      result = thisSegment.compareVersion(otherSegment);
4✔
197
      if (result.isEqual()) {
3✔
198
        if (thisSegment.isEmpty() && otherSegment.isEmpty()) {
6!
199
          todo = false;
3✔
200
        } else if (result.isUnsafe()) {
3!
201
          unsafe = true;
×
202
        }
203
      } else {
204
        todo = false;
2✔
205
      }
206
      thisSegment = thisSegment.getNextOrEmpty();
3✔
207
      otherSegment = otherSegment.getNextOrEmpty();
3✔
208
    } while (todo);
2✔
209
    if (unsafe) {
2!
210
      return result.withUnsafe();
×
211
    }
212
    return result;
2✔
213
  }
214

215
  /**
216
   * @param other the {@link VersionIdentifier} to be matched.
217
   * @return {@code true} if this {@link VersionIdentifier} is equal to the given {@link VersionIdentifier} or this {@link VersionIdentifier} is a pattern
218
   *     version (e.g. "17*" or "17.*") and the given {@link VersionIdentifier} matches to that pattern.
219
   */
220
  public boolean matches(VersionIdentifier other) {
221

222
    if (other == null) {
2✔
223
      return false;
2✔
224
    }
225
    VersionSegment thisSegment = this.start;
3✔
226
    VersionSegment otherSegment = other.start;
3✔
227
    while (true) {
228
      VersionMatchResult matchResult = thisSegment.matches(otherSegment);
4✔
229
      if (matchResult == VersionMatchResult.MATCH) {
3✔
230
        return true;
2✔
231
      } else if (matchResult == VersionMatchResult.MISMATCH) {
3✔
232
        return false;
2✔
233
      }
234
      thisSegment = thisSegment.getNextOrEmpty();
3✔
235
      otherSegment = otherSegment.getNextOrEmpty();
3✔
236
    }
1✔
237
  }
238

239
  /**
240
   * Increment the specified segment. For examples see {@code VersionIdentifierTest.testIncrement()}.
241
   *
242
   * @param digitNumber the index of the {@link VersionSegment} to increment. All segments before will remain untouched and all following segments will be
243
   *     set to zero.
244
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
245
   * @return the incremented {@link VersionIdentifier}.
246
   */
247
  public VersionIdentifier incrementSegment(int digitNumber, boolean keepLetters) {
248

249
    if (isPattern()) {
3!
250
      throw new IllegalStateException("Cannot increment version pattern: " + toString());
×
251
    }
252
    VersionSegment newStart = this.start.increment(digitNumber, keepLetters);
6✔
253
    return new VersionIdentifier(newStart);
5✔
254
  }
255

256
  /**
257
   * Increment the first digit (major version).
258
   *
259
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
260
   * @return the incremented {@link VersionIdentifier}.
261
   * @see #incrementSegment(int, boolean)
262
   */
263
  public VersionIdentifier incrementMajor(boolean keepLetters) {
264
    return incrementSegment(0, keepLetters);
5✔
265
  }
266

267
  /**
268
   * Increment the second digit (minor version).
269
   *
270
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
271
   * @return the incremented {@link VersionIdentifier}.
272
   * @see #incrementSegment(int, boolean)
273
   */
274
  public VersionIdentifier incrementMinor(boolean keepLetters) {
275
    return incrementSegment(1, keepLetters);
5✔
276
  }
277

278
  /**
279
   * Increment the third digit (patch or micro version).
280
   *
281
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
282
   * @return the incremented {@link VersionIdentifier}.
283
   * @see #incrementSegment(int, boolean)
284
   */
285
  public VersionIdentifier incrementPatch(boolean keepLetters) {
286
    return incrementSegment(2, keepLetters);
5✔
287
  }
288

289
  /**
290
   * Increment the last segment.
291
   *
292
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
293
   * @return the incremented {@link VersionIdentifier}.
294
   * @see #incrementSegment(int, boolean)
295
   */
296
  public VersionIdentifier incrementLastDigit(boolean keepLetters) {
297

298
    return incrementSegment(this.start.countDigits() - 1, keepLetters);
9✔
299
  }
300

301
  @Override
302
  public VersionIdentifier getMin() {
303

304
    return this;
×
305
  }
306

307
  @Override
308
  public VersionIdentifier getMax() {
309

310
    return this;
×
311
  }
312

313
  @Override
314
  public boolean contains(VersionIdentifier version) {
315

316
    return matches(version);
4✔
317
  }
318

319
  @Override
320
  public int hashCode() {
321

322
    VersionSegment segment = this.start;
×
323
    int hash = 1;
×
324
    while (segment != null) {
×
325
      hash = hash * 31 + segment.hashCode();
×
326
      segment = segment.getNextOrNull();
×
327
    }
328
    return hash;
×
329
  }
330

331
  @Override
332
  public boolean equals(Object obj) {
333

334
    if (obj == this) {
3✔
335
      return true;
2✔
336
    } else if (!(obj instanceof VersionIdentifier)) {
3✔
337
      return false;
2✔
338
    }
339
    VersionIdentifier other = (VersionIdentifier) obj;
3✔
340
    return Objects.equals(this.start, other.start);
6✔
341
  }
342

343
  @Override
344
  @JsonSerialize
345
  public String toString() {
346

347
    StringBuilder sb = new StringBuilder();
4✔
348
    VersionSegment segment = this.start;
3✔
349
    while (segment != null) {
2✔
350
      sb.append(segment.toString());
5✔
351
      segment = segment.getNextOrNull();
4✔
352
    }
353
    return sb.toString();
3✔
354
  }
355

356
  /**
357
   * @param version the {@link #toString() string representation} of the {@link VersionIdentifier} to parse.
358
   * @return the parsed {@link VersionIdentifier}.
359
   */
360
  @JsonCreator
361
  public static VersionIdentifier of(String version) {
362

363
    if (version == null) {
2✔
364
      return null;
2✔
365
    }
366
    version = version.trim();
3✔
367
    if (version.equals("latest") || version.equals("*")) {
8!
368
      return VersionIdentifier.LATEST;
2✔
369
    }
370
    assert !version.contains(" ") && !version.contains("\n") && !version.contains("\t") : version;
13!
371
    VersionSegment startSegment = VersionSegment.of(version);
3✔
372
    if (startSegment == null) {
2✔
373
      return null;
2✔
374
    }
375
    return new VersionIdentifier(startSegment);
5✔
376
  }
377

378
  /**
379
   * @param v1 the first {@link VersionIdentifier}.
380
   * @param v2 the second {@link VersionIdentifier}.
381
   * @param treatNullAsNegativeInfinity {@code true} to treat {@code null} as negative infinity, {@code false} otherwise (positive infinity).
382
   * @return the null-safe {@link #compareVersion(VersionIdentifier) comparison} of the two {@link VersionIdentifier}s.
383
   */
384
  public static VersionComparisonResult compareVersion(VersionIdentifier v1, VersionIdentifier v2, boolean treatNullAsNegativeInfinity) {
385

386
    if (v1 == null) {
2✔
387
      if (v2 == null) {
2!
388
        return VersionComparisonResult.EQUAL;
×
389
      } else if (treatNullAsNegativeInfinity) {
2✔
390
        return VersionComparisonResult.LESS;
2✔
391
      }
392
      return VersionComparisonResult.GREATER;
2✔
393
    } else if (v2 == null) {
2✔
394
      if (treatNullAsNegativeInfinity) {
2✔
395
        return VersionComparisonResult.GREATER;
2✔
396
      }
397
      return VersionComparisonResult.LESS;
2✔
398
    }
399
    return v1.compareVersion(v2);
4✔
400
  }
401

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