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

devonfw / IDEasy / 28107441918

24 Jun 2026 02:51PM UTC coverage: 71.421% (+0.04%) from 71.381%
28107441918

Pull #2057

github

web-flow
Merge c00e7f2c6 into 459b15501
Pull Request #2057: #1178: Clean up and Solution for new qualifier snapshot

4732 of 7320 branches covered (64.64%)

Branch coverage included in aggregate %.

12142 of 16306 relevant lines covered (74.46%)

3.15 hits per line

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

91.12
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
   * <li>If the snapshot flag is set for this segment, it must not be combined with digits in the same segment.</li>
199
   * {@link VersionPhase#isValid(int) valid} (e.g. "pineapple-pen1" or "donut" are not considered valid).</li>
200
   * </ul>
201
   */
202
  @Override
203
  public boolean isValid() {
204

205
    if (!this.pattern.isEmpty()) {
4✔
206
      return false;
2✔
207
    }
208
    int separatorLen = this.separator.length();
4✔
209
    if (separatorLen > 1) {
3✔
210
      return false;
2✔
211
    } else if (separatorLen == 1) {
3✔
212
      if (!CharCategory.isValidSeparator(this.separator.charAt(0))) {
6✔
213
        return false;
2✔
214
      }
215
    }
216
    if (this.letters.isSnapshot()) {
4✔
217
      if (this.number >= 0) {
3!
218
        return false;
×
219
      }
220
      return true;
2✔
221
    }
222

223
    return this.letters.getPhase().isValid(this.number);
7✔
224
  }
225

226
  @Override
227
  public VersionComparisonResult compareVersion(VersionSegment other) {
228

229
    if (other == null) {
2!
230
      return VersionComparisonResult.GREATER_UNSAFE;
×
231
    }
232

233
    // Check if one version segment is * or *! while the other has no pattern. If so, no further comparison is needed
234
    if (this.letters.isEmpty() && isPattern() && !other.letters.isEmpty()) {
11✔
235
      return VersionComparisonResult.GREATER_UNSAFE;
2✔
236
    } else if (other.letters.isEmpty() && other.isPattern() && !this.letters.isEmpty()) {
11✔
237
      return VersionComparisonResult.LESS_UNSAFE;
2✔
238
    }
239
    // Compare letters and phases
240
    VersionComparisonResult lettersResult = this.letters.compareVersion(other.letters);
6✔
241
    if (!lettersResult.isEqual()) {
3✔
242
      return lettersResult;
2✔
243
    }
244
    if (!"_".equals(this.separator) && "_".equals(other.separator)) {
10✔
245
      if ("".equals(this.separator)) {
5!
246
        return VersionComparisonResult.LESS;
×
247
      } else {
248
        return VersionComparisonResult.GREATER;
2✔
249
      }
250
    } else if ("_".equals(this.separator) && !"_".equals(other.separator)) {
10!
251
      if ("".equals(other.separator)) {
5✔
252
        return VersionComparisonResult.GREATER;
2✔
253
      } else {
254
        return VersionComparisonResult.LESS;
2✔
255
      }
256
    }
257

258
    // Compare version numbers
259
    if (this.number != other.number) {
5✔
260
      if ((this.number < 0) && isPattern()) {
6✔
261
        return VersionComparisonResult.GREATER_UNSAFE;
2✔
262
      } else if ((other.number < 0) && other.isPattern()) {
6✔
263
        return VersionComparisonResult.LESS_UNSAFE;
2✔
264
      } else if (this.number < other.number) {
5✔
265
        return VersionComparisonResult.LESS;
2✔
266
      } else {
267
        return VersionComparisonResult.GREATER;
2✔
268
      }
269
    } else if (this.number < 0 && (isPattern() || other.isPattern())) {
9✔
270
      // Numbers are equal and one of them is a pattern
271
      if (isPattern() && !other.isPattern()) {
6✔
272
        return VersionComparisonResult.GREATER_UNSAFE;
2✔
273
      } else if (!isPattern() && other.isPattern()) {
6!
274
        return VersionComparisonResult.LESS_UNSAFE;
2✔
275
      }
276
      // Both are patterns
277
      else if (this.pattern.equals(PATTERN_MATCH_ANY_STABLE_VERSION) && other.pattern.equals(PATTERN_MATCH_ANY_VERSION)) {
10✔
278
        return VersionComparisonResult.LESS_UNSAFE;
2✔
279
      } else if (this.pattern.equals(PATTERN_MATCH_ANY_VERSION) && other.pattern.equals(PATTERN_MATCH_ANY_STABLE_VERSION)) {
10!
280
        return VersionComparisonResult.GREATER_UNSAFE;
2✔
281
      } else if (this.pattern.equals(other.pattern)) {
6!
282
        return VersionComparisonResult.EQUAL;
2✔
283
      } else {
284
        return VersionComparisonResult.EQUAL_UNSAFE;
×
285
      }
286
    } else if (this.separator.equals(other.separator)) {
6!
287
      return VersionComparisonResult.EQUAL;
2✔
288
    } else {
289
      return VersionComparisonResult.EQUAL_UNSAFE;
×
290
    }
291
  }
292

293
  /**
294
   * Matches a {@link VersionSegment} with a potential {@link #getPattern() pattern} against another {@link VersionSegment}. This operation may not always be
295
   * symmetric.
296
   *
297
   * @param other the {@link VersionSegment} to match against.
298
   * @return the {@link VersionMatchResult} of the match.
299
   */
300
  public VersionMatchResult matches(VersionSegment other) {
301

302
    if (other == null) {
2!
303
      return VersionMatchResult.MISMATCH;
×
304
    }
305
    if (isEmpty() && other.isEmpty()) {
3!
306
      return VersionMatchResult.MATCH;
×
307
    }
308
    boolean isPattern = isPattern();
3✔
309
    if (isPattern) {
2✔
310
      if (!this.digits.isEmpty()) {
4✔
311
        if (this.number != other.number) {
5✔
312
          return VersionMatchResult.MISMATCH;
2✔
313
        }
314
      }
315
      if (!this.separator.isEmpty()) {
4✔
316
        if (!this.separator.equals(other.separator)) {
6✔
317
          return VersionMatchResult.MISMATCH;
2✔
318
        }
319
      }
320
    } else {
321
      if ((this.number != other.number) || !this.separator.equals(other.separator)) {
11!
322
        return VersionMatchResult.MISMATCH;
2✔
323
      }
324
    }
325
    VersionMatchResult result = this.letters.matches(other.letters, isPattern);
7✔
326
    if (isPattern && (result == VersionMatchResult.EQUAL)) {
5✔
327
      if (this.pattern.equals(PATTERN_MATCH_ANY_STABLE_VERSION)) {
5✔
328
        if (other.containsUnstableQualifier()) {
3✔
329
          return VersionMatchResult.MISMATCH;
2✔
330
        }
331
        return VersionMatchResult.MATCH;
2✔
332
      } else if (this.pattern.equals(PATTERN_MATCH_ANY_VERSION)) {
5!
333
        return VersionMatchResult.MATCH;
2✔
334
      } else {
335
        throw new IllegalStateException("Pattern=" + this.pattern);
×
336
      }
337
    }
338
    return result;
2✔
339
  }
340

341
  /**
342
   * @return {@code true} if this {@link VersionSegment} or any following segment contains an unstable qualifier.
343
   *     <p>
344
   *     An unstable qualifier is either:
345
   *     <ul>
346
   *       <li>a {@link VersionPhase} (e.g. alpha, beta ...)</li>
347
   *       <li>a pre-phase</li>
348
   *       <li>a snapshot flag</li>
349
   *     </ul>
350
   */
351
  protected boolean containsUnstableQualifier() {
352
    VersionSegment segment = this;
2✔
353
    while (segment != null) {
2✔
354
      if (segment.letters.isUnstable()) {
4✔
355
        return true;
2✔
356
      }
357
      segment = segment.next;
4✔
358
    }
359
    return false;
2✔
360
  }
361

362
  /**
363
   * {@link VersionIdentifier#incrementSegment(int, boolean)}  Increments a version} recursively per {@link VersionSegment}.
364
   *
365
   * @param digitKeepCount the number of leading {@link VersionSegment}s with {@link VersionSegment#getDigits() digits} to keep untouched. Will be {@code 0}
366
   *     for the segment to increment and negative for the segments to set to zero.
367
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
368
   * @return the new {@link VersionSegment}.
369
   */
370
  VersionSegment increment(int digitKeepCount, boolean keepLetters) {
371

372
    String separator = this.separator;
3✔
373
    VersionLetters letters = this.letters;
3✔
374
    String digits = this.digits;
3✔
375
    int number = this.number;
3✔
376
    String pattern = this.pattern;
3✔
377
    int nextSegmentKeepCount = digitKeepCount;
2✔
378
    if (this.number >= 0) {
3✔
379
      nextSegmentKeepCount--;
1✔
380
    }
381
    if ((digitKeepCount < 0) || ((digitKeepCount == 0) && (this.number >= 0))) {
7✔
382
      if (!keepLetters) {
2✔
383
        letters = VersionLetters.EMPTY;
2✔
384
      }
385
      if (number >= 0) {
2✔
386
        if (digitKeepCount == 0) {
2✔
387
          number++;
2✔
388
        } else {
389
          number = 0;
2✔
390
        }
391
        int digitsLength = digits.length();
3✔
392
        digits = Integer.toString(number);
3✔
393
        int leadingZeros = digitsLength - digits.length();
5✔
394
        if (leadingZeros > 0) {
2✔
395
          StringBuilder newDigits = new StringBuilder(digits);
5✔
396
          while (leadingZeros > 0) {
2✔
397
            newDigits.insert(0, "0");
5✔
398
            leadingZeros--;
2✔
399
          }
400
          digits = newDigits.toString();
3✔
401
        }
402
      } else if (!keepLetters) {
3✔
403
        if (this.next == null) {
3✔
404
          return null;
2✔
405
        }
406
        return this.next.increment(nextSegmentKeepCount, false);
6✔
407
      }
408
    }
409
    VersionSegment nextSegment = null;
2✔
410
    if (this.next != null) {
3✔
411
      nextSegment = this.next.increment(nextSegmentKeepCount, keepLetters);
6✔
412
    }
413
    return new VersionSegment(nextSegment, separator, letters, digits, number, pattern);
10✔
414
  }
415

416
  /**
417
   * @return the number of {@link VersionSegment}s with {@link VersionSegment#getDigits() digits}.
418
   */
419
  int countDigits() {
420

421
    int count = 0;
2✔
422
    if (this.number >= 0) {
3✔
423
      count = 1;
2✔
424
    }
425
    if (this.next != null) {
3✔
426
      count = count + this.next.countDigits();
6✔
427
    }
428
    return count;
2✔
429
  }
430

431
  @Override
432
  public boolean equals(Object obj) {
433

434
    if (obj == this) {
3!
435
      return true;
×
436
    } else if (!(obj instanceof VersionSegment)) {
3✔
437
      return false;
2✔
438
    }
439
    VersionSegment other = (VersionSegment) obj;
3✔
440
    if (!Objects.equals(this.digits, other.digits)) {
6✔
441
      return false;
2✔
442
    } else if (!Objects.equals(this.separator, other.separator)) {
6!
443
      return false;
×
444
    } else if (!Objects.equals(this.letters, other.letters)) {
6!
445
      return false;
×
446
    } else if (!Objects.equals(this.pattern, other.pattern)) {
6!
447
      return false;
×
448
    } else if (!Objects.equals(this.next, other.next)) {
6✔
449
      return false;
2✔
450
    }
451
    return true;
2✔
452
  }
453

454
  @Override
455
  public String toString() {
456

457
    return this.separator + this.letters + this.digits + this.pattern;
11✔
458
  }
459

460
  /**
461
   * @return the {@link #isEmpty() empty} {@link VersionSegment} instance.
462
   */
463
  public static VersionSegment ofEmpty() {
464

465
    return EMPTY;
2✔
466
  }
467

468
  static VersionSegment of(String version) {
469

470
    CharReader reader = new CharReader(version);
5✔
471
    VersionSegment start = null;
2✔
472
    VersionSegment current = null;
2✔
473
    while (reader.hasNext()) {
3✔
474
      VersionSegment segment = parseSegment(reader);
3✔
475
      if (current == null) {
2✔
476
        start = segment;
3✔
477
      } else {
478
        current.next = segment;
3✔
479
      }
480
      current = segment;
2✔
481
    }
1✔
482
    return start;
2✔
483
  }
484

485
  private static VersionSegment parseSegment(CharReader reader) {
486

487
    String separator = reader.readSeparator();
3✔
488
    String letters = reader.readLetters();
3✔
489
    String digits = reader.readDigits();
3✔
490
    String pattern = reader.readPattern();
3✔
491
    return new VersionSegment(separator, letters, digits, pattern);
8✔
492
  }
493

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

© 2026 Coveralls, Inc