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

devonfw / IDEasy / 19436066693

17 Nov 2025 04:03PM UTC coverage: 68.946% (+0.1%) from 68.834%
19436066693

push

github

web-flow
add support to increment versions (#1595)

Co-authored-by: Malte Brunnlieb <maybeec@users.noreply.github.com>

3520 of 5593 branches covered (62.94%)

Branch coverage included in aggregate %.

9204 of 12862 relevant lines covered (71.56%)

3.14 hits per line

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

85.56
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
  /**
202
   * Increment the specified segment. For examples see {@code VersionIdentifierTest.testIncrement()}.
203
   *
204
   * @param segmentNumber the index of the {@link VersionSegment} to increment. All segments before will remain untouched and all following segments will be
205
   *     set to zero.
206
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
207
   * @return the incremented {@link VersionIdentifier}.
208
   */
209
  public VersionIdentifier incrementSegment(int segmentNumber, boolean keepLetters) {
210

211
    if (isPattern()) {
3!
212
      throw new IllegalStateException("Cannot increment version pattern: " + toString());
×
213
    }
214
    VersionSegment newStart = this.start.increment(segmentNumber, keepLetters);
6✔
215
    return new VersionIdentifier(newStart);
5✔
216
  }
217

218
  /**
219
   * Increment the first segment (major version).
220
   *
221
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
222
   * @return the incremented {@link VersionIdentifier}.
223
   * @see #incrementSegment(int, boolean)
224
   */
225
  public VersionIdentifier incrementMajor(boolean keepLetters) {
226
    return incrementSegment(0, keepLetters);
5✔
227
  }
228

229
  /**
230
   * Increment the second segment (minor version).
231
   *
232
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
233
   * @return the incremented {@link VersionIdentifier}.
234
   * @see #incrementSegment(int, boolean)
235
   */
236
  public VersionIdentifier incrementMinor(boolean keepLetters) {
237
    return incrementSegment(1, keepLetters);
5✔
238
  }
239

240
  /**
241
   * Increment the third segment (patch or micro version).
242
   *
243
   * @param keepLetters {@code true} to keep {@link VersionSegment#getLetters() letters} from modified segments, {@code false} to drop them.
244
   * @return the incremented {@link VersionIdentifier}.
245
   * @see #incrementSegment(int, boolean)
246
   */
247
  public VersionIdentifier incrementPatch(boolean keepLetters) {
248
    return incrementSegment(2, keepLetters);
5✔
249
  }
250

251
  @Override
252
  public VersionIdentifier getMin() {
253

254
    return this;
×
255
  }
256

257
  @Override
258
  public VersionIdentifier getMax() {
259

260
    return this;
×
261
  }
262

263
  @Override
264
  public boolean contains(VersionIdentifier version) {
265

266
    return matches(version);
4✔
267
  }
268

269
  @Override
270
  public int hashCode() {
271

272
    VersionSegment segment = this.start;
×
273
    int hash = 1;
×
274
    while (segment != null) {
×
275
      hash = hash * 31 + segment.hashCode();
×
276
      segment = segment.getNextOrNull();
×
277
    }
278
    return hash;
×
279
  }
280

281
  @Override
282
  public boolean equals(Object obj) {
283

284
    if (obj == this) {
3✔
285
      return true;
2✔
286
    } else if (!(obj instanceof VersionIdentifier)) {
3✔
287
      return false;
2✔
288
    }
289
    VersionIdentifier other = (VersionIdentifier) obj;
3✔
290
    return Objects.equals(this.start, other.start);
6✔
291
  }
292

293
  @Override
294
  @JsonSerialize
295
  public String toString() {
296

297
    StringBuilder sb = new StringBuilder();
4✔
298
    VersionSegment segment = this.start;
3✔
299
    while (segment != null) {
2✔
300
      sb.append(segment.toString());
5✔
301
      segment = segment.getNextOrNull();
4✔
302
    }
303
    return sb.toString();
3✔
304
  }
305

306
  /**
307
   * @param version the {@link #toString() string representation} of the {@link VersionIdentifier} to parse.
308
   * @return the parsed {@link VersionIdentifier}.
309
   */
310
  @JsonCreator
311
  public static VersionIdentifier of(String version) {
312

313
    if (version == null) {
2✔
314
      return null;
2✔
315
    } else if (version.equals("latest")) {
4!
316
      return VersionIdentifier.LATEST;
×
317
    }
318
    VersionSegment startSegment = VersionSegment.of(version);
3✔
319
    if (startSegment == null) {
2✔
320
      return null;
2✔
321
    }
322
    return new VersionIdentifier(startSegment);
5✔
323
  }
324

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