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

devonfw / IDEasy / 19868191669

02 Dec 2025 05:46PM UTC coverage: 69.852% (+0.07%) from 69.787%
19868191669

push

github

web-flow
#1633: fix CVE check to only suggest unstable versions if the requested version was unstable (#1635)

3835 of 6021 branches covered (63.69%)

Branch coverage included in aggregate %.

9812 of 13516 relevant lines covered (72.6%)

3.16 hits per line

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

90.88
cli/src/main/java/com/devonfw/tools/ide/version/VersionSegment.java
1
package com.devonfw.tools.ide.version;
2

3
import java.util.Objects;
4

5
/**
6
 * Represents a single segment of a {@link VersionIdentifier}.
7
 */
8
public class VersionSegment implements VersionObject<VersionSegment> {
9

10
  /** Pattern to match a any version that matches the prefix. Value is: {@value} */
11
  public static final String PATTERN_MATCH_ANY_VERSION = "*!";
12

13
  /** Pattern to match a {@link VersionPhase#isStable() stable} version that matches the prefix. Value is: {@value} */
14
  public static final String PATTERN_MATCH_ANY_STABLE_VERSION = "*";
15

16
  private static final VersionSegment EMPTY = new VersionSegment("", "", "", "");
9✔
17

18
  private final String separator;
19

20
  private final VersionLetters letters;
21

22
  private final String pattern;
23

24
  private final String digits;
25

26
  private final int number;
27

28
  private VersionSegment next;
29

30
  /**
31
   * The constructor.
32
   *
33
   * @param separator the {@link #getSeparator() separator}.
34
   * @param letters the {@link #getLettersString() letters}.
35
   * @param digits the {@link #getDigits() digits}.
36
   */
37
  VersionSegment(String separator, String letters, String digits) {
38

39
    this(separator, letters, digits, "");
6✔
40
  }
1✔
41

42
  /**
43
   * The constructor.
44
   *
45
   * @param separator the {@link #getSeparator() separator}.
46
   * @param letters the {@link #getLettersString() letters}.
47
   * @param digits the {@link #getDigits() digits}.
48
   * @param pattern the {@link #getPattern() pattern}.
49
   */
50
  VersionSegment(String separator, String letters, String digits, String pattern) {
51

52
    super();
2✔
53
    this.separator = separator;
3✔
54
    boolean isAnyPattern = PATTERN_MATCH_ANY_VERSION.equals(pattern);
4✔
55
    if (isAnyPattern && letters.isEmpty()) {
5!
56
      this.letters = VersionLetters.UNSTABLE;
4✔
57
    } else {
58
      this.letters = VersionLetters.of(letters);
4✔
59
    }
60
    if (!pattern.isEmpty() && !isAnyPattern
7✔
61
        && !PATTERN_MATCH_ANY_STABLE_VERSION.equals(pattern)) {
2!
62
      throw new IllegalArgumentException("Invalid pattern: " + pattern);
×
63
    }
64
    this.pattern = pattern;
3✔
65
    this.digits = digits;
3✔
66
    if (this.digits.isEmpty()) {
4✔
67
      this.number = -1;
4✔
68
    } else {
69
      this.number = Integer.parseInt(this.digits);
5✔
70
    }
71
    if (EMPTY != null) {
2✔
72
      assert (!this.letters.isEmpty() || !this.digits.isEmpty() || !this.separator.isEmpty()
15✔
73
          || !this.pattern.isEmpty());
2!
74
    }
75
  }
1✔
76

77
  private VersionSegment(VersionSegment next, String separator, VersionLetters letters, String digits, int number, String pattern) {
78
    super();
2✔
79
    this.next = next;
3✔
80
    this.separator = separator;
3✔
81
    this.letters = letters;
3✔
82
    this.pattern = pattern;
3✔
83
    this.digits = digits;
3✔
84
    this.number = number;
3✔
85
  }
1✔
86

87
  /**
88
   * @return the separator {@link String} (e.g. "." or "-") or the empty {@link String} ("") for none.
89
   */
90
  public String getSeparator() {
91

92
    return this.separator;
3✔
93
  }
94

95
  /**
96
   * @return the letters or the empty {@link String} ("") for none. In canonical {@link VersionIdentifier}s letters indicate the development phase (e.g. "pre",
97
   *     "rc", "alpha", "beta", "milestone", "test", "dev", "SNAPSHOT", etc.). However, letters are technically any
98
   *     {@link Character#isLetter(char) letter characters} and may also be something like a code-name (e.g. "Cupcake", "Donut", "Eclair", "Froyo",
99
   *     "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean" in case of Android internals). Please note that in such case it is impossible to
100
   *     properly decide which version is greater than another versions. To avoid mistakes, the comparison supports a strict mode that will let the comparison
101
   *     fail in such case. However, by default (e.g. for {@link Comparable#compareTo(Object)}) the default {@link String#compareTo(String) string comparison}
102
   *     (lexicographical) is used to ensure a natural order.
103
   * @see #getPhase()
104
   */
105
  public String getLettersString() {
106

107
    return this.letters.getLetters();
4✔
108
  }
109

110
  /**
111
   * @return the {@link VersionLetters}.
112
   */
113
  public VersionLetters getLetters() {
114

115
    return this.letters;
3✔
116
  }
117

118
  /**
119
   * @return the {@link VersionPhase} for the {@link #getLettersString() letters}. Will be {@link VersionPhase#UNDEFINED} if unknown and hence never
120
   *     {@code null}.
121
   * @see #getLettersString()
122
   */
123
  public VersionPhase getPhase() {
124

125
    return this.letters.getPhase();
4✔
126
  }
127

128
  /**
129
   * @return the digits or the empty {@link String} ("") for none. This is the actual {@link #getNumber() number} part of this {@link VersionSegment}. So the
130
   *     {@link VersionIdentifier} "1.0.001" will have three segments: The first one with "1" as digits, the second with "0" as digits, and a third with "001"
131
   *     as digits. You can get the same value via {@link #getNumber()} but this {@link String} representation will preserve leading zeros.
132
   */
133
  public String getDigits() {
134

135
    return this.digits;
3✔
136
  }
137

138
  /**
139
   * @return the {@link #getDigits() digits} and integer number. Will be {@code -1} if no {@link #getDigits() digits} are present.
140
   */
141
  public int getNumber() {
142

143
    return this.number;
3✔
144
  }
145

146
  /**
147
   * @return the potential pattern that is {@link #PATTERN_MATCH_ANY_STABLE_VERSION}, {@link #PATTERN_MATCH_ANY_VERSION}, or for no pattern the empty
148
   *     {@link String}.
149
   */
150
  public String getPattern() {
151

152
    return this.pattern;
3✔
153
  }
154

155
  /**
156
   * @return {@code true} if {@link #getPattern() pattern} is NOT {@link String#isEmpty() empty}.
157
   */
158
  public boolean isPattern() {
159

160
    return !this.pattern.isEmpty();
8✔
161
  }
162

163
  /**
164
   * @return the next {@link VersionSegment} or {@code null} if this is the tail of the {@link VersionIdentifier}.
165
   */
166
  public VersionSegment getNextOrNull() {
167

168
    return this.next;
3✔
169
  }
170

171
  /**
172
   * @return the next {@link VersionSegment} or the {@link #ofEmpty() empty segment} if this is the tail of the {@link VersionIdentifier}.
173
   */
174
  public VersionSegment getNextOrEmpty() {
175

176
    if (this.next == null) {
3✔
177
      return EMPTY;
2✔
178
    }
179
    return this.next;
3✔
180
  }
181

182
  /**
183
   * @return {@code true} if this is the empty {@link VersionSegment}, {@code false} otherwise.
184
   */
185
  public boolean isEmpty() {
186

187
    return (this == EMPTY);
7✔
188
  }
189

190
  /**
191
   * A valid {@link VersionSegment} has to meet the following requirements:
192
   * <ul>
193
   * <li>The {@link #getSeparator() separator} may not be {@link String#length() longer} than a single character (e.g.
194
   * ".-_1" or "--1" are not considered valid).</li>
195
   * <li>The {@link #getSeparator() separator} may only contain the characters '.', '-', or '_' (e.g. " 1" or "ö1" are
196
   * not considered valid).</li>
197
   * <li>The combination of {@link #getPhase() phase} and {@link #getNumber() number} has to be
198
   * {@link VersionPhase#isValid(int) valid} (e.g. "pineapple-pen1" or "donut" are not considered valid).</li>
199
   * </ul>
200
   */
201
  @Override
202
  public boolean isValid() {
203

204
    if (!this.pattern.isEmpty()) {
4✔
205
      return false;
2✔
206
    }
207
    int separatorLen = this.separator.length();
4✔
208
    if (separatorLen > 1) {
3✔
209
      return false;
2✔
210
    } else if (separatorLen == 1) {
3✔
211
      if (!CharCategory.isValidSeparator(this.separator.charAt(0))) {
6✔
212
        return false;
2✔
213
      }
214
    }
215
    return this.letters.getPhase().isValid(this.number);
7✔
216
  }
217

218
  @Override
219
  public VersionComparisonResult compareVersion(VersionSegment other) {
220

221
    if (other == null) {
2!
222
      return VersionComparisonResult.GREATER_UNSAFE;
×
223
    }
224
    VersionComparisonResult lettersResult = this.letters.compareVersion(other.letters);
6✔
225
    if (!lettersResult.isEqual()) {
3✔
226
      return lettersResult;
2✔
227
    }
228
    if (!"_".equals(this.separator) && "_".equals(other.separator)) {
10✔
229
      if ("".equals(this.separator)) {
5!
230
        return VersionComparisonResult.LESS;
×
231
      } else {
232
        return VersionComparisonResult.GREATER;
2✔
233
      }
234
    } else if ("_".equals(this.separator) && !"_".equals(other.separator)) {
10!
235
      if ("".equals(other.separator)) {
5✔
236
        return VersionComparisonResult.GREATER;
2✔
237
      } else {
238
        return VersionComparisonResult.LESS;
2✔
239
      }
240
    }
241

242
    if (this.number != other.number) {
5✔
243
      if ((this.number < 0) && isPattern()) {
6✔
244
        return VersionComparisonResult.LESS_UNSAFE;
2✔
245
      } else if ((other.number < 0) && other.isPattern()) {
6!
246
        return VersionComparisonResult.GREATER_UNSAFE;
×
247
      } else if (this.number < other.number) {
5✔
248
        return VersionComparisonResult.LESS;
2✔
249
      } else {
250
        return VersionComparisonResult.GREATER;
2✔
251
      }
252
    } else if (this.separator.equals(other.separator)) {
6✔
253
      return VersionComparisonResult.EQUAL;
2✔
254
    } else {
255
      return VersionComparisonResult.EQUAL_UNSAFE;
2✔
256
    }
257
  }
258

259
  /**
260
   * Matches a {@link VersionSegment} with a potential {@link #getPattern() pattern} against another {@link VersionSegment}. This operation may not always be
261
   * symmetric.
262
   *
263
   * @param other the {@link VersionSegment} to match against.
264
   * @return the {@link VersionMatchResult} of the match.
265
   */
266
  public VersionMatchResult matches(VersionSegment other) {
267

268
    if (other == null) {
2!
269
      return VersionMatchResult.MISMATCH;
×
270
    }
271
    if (isEmpty() && other.isEmpty()) {
3!
272
      return VersionMatchResult.MATCH;
×
273
    }
274
    boolean isPattern = isPattern();
3✔
275
    if (isPattern) {
2✔
276
      if (!this.digits.isEmpty()) {
4✔
277
        if (this.number != other.number) {
5✔
278
          return VersionMatchResult.MISMATCH;
2✔
279
        }
280
      }
281
      if (!this.separator.isEmpty()) {
4✔
282
        if (!this.separator.equals(other.separator)) {
6✔
283
          return VersionMatchResult.MISMATCH;
2✔
284
        }
285
      }
286
    } else {
287
      if ((this.number != other.number) || !this.separator.equals(other.separator)) {
11!
288
        return VersionMatchResult.MISMATCH;
2✔
289
      }
290
    }
291
    VersionMatchResult result = this.letters.matches(other.letters, isPattern);
7✔
292
    if (isPattern && (result == VersionMatchResult.EQUAL)) {
5✔
293
      if (this.pattern.equals(PATTERN_MATCH_ANY_STABLE_VERSION)) {
5✔
294
        VersionLetters developmentPhase = other.getDevelopmentPhase();
3✔
295
        if (developmentPhase.isUnstable()) {
3✔
296
          return VersionMatchResult.MISMATCH;
2✔
297
        }
298
        return VersionMatchResult.MATCH;
2✔
299
      } else if (this.pattern.equals(PATTERN_MATCH_ANY_VERSION)) {
5!
300
        return VersionMatchResult.MATCH;
2✔
301
      } else {
302
        throw new IllegalStateException("Pattern=" + this.pattern);
×
303
      }
304
    }
305
    return result;
2✔
306
  }
307

308
  /**
309
   * @return the {@link VersionLetters} that represent a {@link VersionLetters#isDevelopmentPhase() development phase} searching from this
310
   *     {@link VersionSegment} to all {@link #getNextOrNull() next segments}. Will be {@link VersionPhase#NONE} if no
311
   *     {@link VersionPhase#isDevelopmentPhase() development phase} was found and {@link VersionPhase#UNDEFINED} if multiple
312
   *     {@link VersionPhase#isDevelopmentPhase() development phase}s have been found.
313
   * @see VersionIdentifier#getDevelopmentPhase()
314
   */
315
  protected VersionLetters getDevelopmentPhase() {
316

317
    VersionLetters result = VersionLetters.EMPTY;
2✔
318
    VersionSegment segment = this;
2✔
319
    while (segment != null) {
2✔
320
      if (segment.letters.isDevelopmentPhase()) {
4✔
321
        if (result == VersionLetters.EMPTY) {
3!
322
          result = segment.letters;
4✔
323
        } else {
324
          result = VersionLetters.UNDEFINED;
×
325
        }
326
      }
327
      segment = segment.next;
4✔
328
    }
329
    return result;
2✔
330
  }
331

332
  /**
333
   * {@link VersionIdentifier#incrementSegment(int, boolean)}  Increments a version} recursively per {@link VersionSegment}.
334
   *
335
   * @param digitKeepCount the number of leading {@link VersionSegment}s with {@link VersionSegment#getDigits() digits} to keep untouched. Will be {@code 0}
336
   *     for the segment to increment and negative for the segments to set to zero.
337
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
338
   * @return the new {@link VersionSegment}.
339
   */
340
  VersionSegment increment(int digitKeepCount, boolean keepLetters) {
341

342
    String separator = this.separator;
3✔
343
    VersionLetters letters = this.letters;
3✔
344
    String digits = this.digits;
3✔
345
    int number = this.number;
3✔
346
    String pattern = this.pattern;
3✔
347
    int nextSegmentKeepCount = digitKeepCount;
2✔
348
    if (this.number >= 0) {
3✔
349
      nextSegmentKeepCount--;
1✔
350
    }
351
    if ((digitKeepCount < 0) || ((digitKeepCount == 0) && (this.number >= 0))) {
7✔
352
      if (!keepLetters) {
2✔
353
        letters = VersionLetters.EMPTY;
2✔
354
      }
355
      if (number >= 0) {
2✔
356
        if (digitKeepCount == 0) {
2✔
357
          number++;
2✔
358
        } else {
359
          number = 0;
2✔
360
        }
361
        int digitsLength = digits.length();
3✔
362
        digits = Integer.toString(number);
3✔
363
        int leadingZeros = digitsLength - digits.length();
5✔
364
        if (leadingZeros > 0) {
2✔
365
          StringBuilder newDigits = new StringBuilder(digits);
5✔
366
          while (leadingZeros > 0) {
2✔
367
            newDigits.insert(0, "0");
5✔
368
            leadingZeros--;
2✔
369
          }
370
          digits = newDigits.toString();
3✔
371
        }
372
      } else if (!keepLetters) {
3✔
373
        if (this.next == null) {
3✔
374
          return null;
2✔
375
        }
376
        return this.next.increment(nextSegmentKeepCount, false);
6✔
377
      }
378
    }
379
    VersionSegment nextSegment = null;
2✔
380
    if (this.next != null) {
3✔
381
      nextSegment = this.next.increment(nextSegmentKeepCount, keepLetters);
6✔
382
    }
383
    return new VersionSegment(nextSegment, separator, letters, digits, number, pattern);
10✔
384
  }
385

386
  /**
387
   * @return the number of {@link VersionSegment}s with {@link VersionSegment#getDigits() digits}.
388
   */
389
  int countDigits() {
390

391
    int count = 0;
2✔
392
    if (this.number >= 0) {
3✔
393
      count = 1;
2✔
394
    }
395
    if (this.next != null) {
3✔
396
      count = count + this.next.countDigits();
6✔
397
    }
398
    return count;
2✔
399
  }
400

401
  @Override
402
  public boolean equals(Object obj) {
403

404
    if (obj == this) {
3!
405
      return true;
×
406
    } else if (!(obj instanceof VersionSegment)) {
3✔
407
      return false;
2✔
408
    }
409
    VersionSegment other = (VersionSegment) obj;
3✔
410
    if (!Objects.equals(this.digits, other.digits)) {
6✔
411
      return false;
2✔
412
    } else if (!Objects.equals(this.separator, other.separator)) {
6!
413
      return false;
×
414
    } else if (!Objects.equals(this.letters, other.letters)) {
6!
415
      return false;
×
416
    } else if (!Objects.equals(this.pattern, other.pattern)) {
6!
417
      return false;
×
418
    } else if (!Objects.equals(this.next, other.next)) {
6✔
419
      return false;
2✔
420
    }
421
    return true;
2✔
422
  }
423

424
  @Override
425
  public String toString() {
426

427
    return this.separator + this.letters + this.digits + this.pattern;
11✔
428
  }
429

430
  /**
431
   * @return the {@link #isEmpty() empty} {@link VersionSegment} instance.
432
   */
433
  public static VersionSegment ofEmpty() {
434

435
    return EMPTY;
2✔
436
  }
437

438
  static VersionSegment of(String version) {
439

440
    CharReader reader = new CharReader(version);
5✔
441
    VersionSegment start = null;
2✔
442
    VersionSegment current = null;
2✔
443
    while (reader.hasNext()) {
3✔
444
      VersionSegment segment = parseSegment(reader);
3✔
445
      if (current == null) {
2✔
446
        start = segment;
3✔
447
      } else {
448
        current.next = segment;
3✔
449
      }
450
      current = segment;
2✔
451
    }
1✔
452
    return start;
2✔
453
  }
454

455
  private static VersionSegment parseSegment(CharReader reader) {
456

457
    String separator = reader.readSeparator();
3✔
458
    String letters = reader.readLetters();
3✔
459
    String digits = reader.readDigits();
3✔
460
    String pattern = reader.readPattern();
3✔
461
    return new VersionSegment(separator, letters, digits, pattern);
8✔
462
  }
463

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