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

devonfw / IDEasy / 28658969240

03 Jul 2026 11:56AM UTC coverage: 71.41% (+0.05%) from 71.356%
28658969240

Pull #2057

github

web-flow
Merge cb5d1bae2 into 2504d944a
Pull Request #2057: #1178: Clean up and Solution for new qualifier snapshot

4722 of 7308 branches covered (64.61%)

Branch coverage included in aggregate %.

12135 of 16298 relevant lines covered (74.46%)

3.15 hits per line

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

91.6
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
      return this.number < 0;
7✔
218
    }
219

220
    return this.letters.getPhase().isValid(this.number);
7✔
221
  }
222

223
  @Override
224
  public VersionComparisonResult compareVersion(VersionSegment other) {
225

226
    if (other == null) {
2!
227
      return VersionComparisonResult.GREATER_UNSAFE;
×
228
    }
229

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

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

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

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

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

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

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

413
  /**
414
   * @return the number of {@link VersionSegment}s with {@link VersionSegment#getDigits() digits}.
415
   */
416
  int countDigits() {
417

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

428
  @Override
429
  public boolean equals(Object obj) {
430

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

451
  @Override
452
  public String toString() {
453

454
    return this.separator + this.letters + this.digits + this.pattern;
11✔
455
  }
456

457
  /**
458
   * @return the {@link #isEmpty() empty} {@link VersionSegment} instance.
459
   */
460
  public static VersionSegment ofEmpty() {
461

462
    return EMPTY;
2✔
463
  }
464

465
  static VersionSegment of(String version) {
466

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

482
  private static VersionSegment parseSegment(CharReader reader) {
483

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

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