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

devonfw / IDEasy / 13344873653

15 Feb 2025 12:10PM UTC coverage: 67.959% (-0.5%) from 68.482%
13344873653

Pull #1021

github

web-flow
Merge 91daf7140 into c70643978
Pull Request #1021: #786: support ide upgrade to automatically update to the latest version of IDEasy

2966 of 4793 branches covered (61.88%)

Branch coverage included in aggregate %.

7690 of 10887 relevant lines covered (70.63%)

3.07 hits per line

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

84.71
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

10
/**
11
 * Data-type to represent a {@link VersionIdentifier} in a structured way and allowing {@link #compareVersion(VersionIdentifier) comparison} of
12
 * {@link VersionIdentifier}s.
13
 */
14
public final class VersionIdentifier implements VersionObject<VersionIdentifier>, GenericVersionRange {
15

16
  /** {@link VersionIdentifier} "*" that will resolve to the latest stable version. */
17
  public static final VersionIdentifier LATEST = VersionIdentifier.of("*");
3✔
18

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

22
  private final VersionSegment start;
23

24
  private final VersionLetters developmentPhase;
25

26
  private final boolean valid;
27

28
  private VersionIdentifier(VersionSegment start) {
29

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

58
  /**
59
   * Resolves a version pattern against a list of available versions.
60
   *
61
   * @param version the version pattern to resolve
62
   * @param versions the
63
   *     {@link com.devonfw.tools.ide.tool.repository.ToolRepository#getSortedVersions(String, String, ToolCommandlet) available versions, sorted in descending
64
   *     order}.
65
   * @param logger the {@link IdeLogger}.
66
   * @return the resolved version
67
   */
68
  public static VersionIdentifier resolveVersionPattern(GenericVersionRange version, List<VersionIdentifier> versions, IdeLogger logger) {
69
    if (version == null) {
2!
70
      version = LATEST;
×
71
    }
72
    if (!version.isPattern()) {
3✔
73
      return (VersionIdentifier) version;
3✔
74
    }
75
    for (VersionIdentifier vi : versions) {
10!
76
      if (version.contains(vi)) {
4!
77
        logger.debug("Resolved version pattern {} to version {}", version, vi);
13✔
78
        return vi;
2✔
79
      }
80
    }
×
81
    throw new CliException(
×
82
        "Could not find any version matching '" + version + "' - there are " + versions.size() + " version(s) available but none matched!");
×
83
  }
84

85
  /**
86
   * @return the first {@link VersionSegment} of this {@link VersionIdentifier}. To get other segments use {@link VersionSegment#getNextOrEmpty()} or
87
   *     {@link VersionSegment#getNextOrNull()}.
88
   */
89
  public VersionSegment getStart() {
90

91
    return this.start;
3✔
92
  }
93

94
  /**
95
   * A valid {@link VersionIdentifier} has to meet the following requirements:
96
   * <ul>
97
   * <li>All {@link VersionSegment segments} themselves are {@link VersionSegment#isValid() valid}.</li>
98
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
99
   * {@link VersionSegment#getSeparator() separator} (e.g. ".1.0" or "-1-2" are not considered valid).</li>
100
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
101
   * {@link VersionSegment#getLettersString() letter-sequence} (e.g. "RC1" or "beta" are not considered valid).</li>
102
   * <li>Have at least one {@link VersionSegment segment} with a positive {@link VersionSegment#getNumber() number}
103
   * (e.g. "0.0.0" or "0.alpha" are not considered valid).</li>
104
   * <li>Have at max one {@link VersionSegment segment} with a {@link VersionSegment#getPhase() phase} that is a real
105
   * {@link VersionPhase#isDevelopmentPhase() development phase} (e.g. "1.alpha1.beta2" or "1.0.rc1-milestone2" are not
106
   * considered valid).</li>
107
   * <li>It is NOT a {@link #isPattern() pattern}.</li>
108
   * </ul>
109
   */
110
  @Override
111
  public boolean isValid() {
112

113
    return this.valid;
3✔
114
  }
115

116
  @Override
117
  public boolean isPattern() {
118

119
    VersionSegment segment = this.start;
3✔
120
    while (segment != null) {
2✔
121
      if (segment.isPattern()) {
3✔
122
        return true;
2✔
123
      }
124
      segment = segment.getNextOrNull();
4✔
125
    }
126
    return false;
2✔
127
  }
128

129
  /**
130
   * @return the {@link VersionLetters#isDevelopmentPhase() development phase} of this {@link VersionIdentifier}. Will be {@link VersionLetters#EMPTY} if no
131
   *     development phase is specified in any {@link VersionSegment} and will be {@link VersionLetters#UNDEFINED} if more than one
132
   *     {@link VersionLetters#isDevelopmentPhase() development phase} is specified (e.g. "1.0-alpha1.rc2").
133
   */
134
  public VersionLetters getDevelopmentPhase() {
135

136
    return this.developmentPhase;
3✔
137
  }
138

139
  @Override
140
  public VersionComparisonResult compareVersion(VersionIdentifier other) {
141

142
    if (other == null) {
2!
143
      return VersionComparisonResult.GREATER_UNSAFE;
×
144
    }
145
    VersionSegment thisSegment = this.start;
3✔
146
    VersionSegment otherSegment = other.start;
3✔
147
    VersionComparisonResult result = null;
2✔
148
    boolean unsafe = false;
2✔
149
    boolean todo = true;
2✔
150
    do {
151
      result = thisSegment.compareVersion(otherSegment);
4✔
152
      if (result.isEqual()) {
3✔
153
        if (thisSegment.isEmpty() && otherSegment.isEmpty()) {
6!
154
          todo = false;
3✔
155
        } else if (result.isUnsafe()) {
3!
156
          unsafe = true;
×
157
        }
158
      } else {
159
        todo = false;
2✔
160
      }
161
      thisSegment = thisSegment.getNextOrEmpty();
3✔
162
      otherSegment = otherSegment.getNextOrEmpty();
3✔
163
    } while (todo);
2✔
164
    if (unsafe) {
2!
165
      return result.withUnsafe();
×
166
    }
167
    return result;
2✔
168
  }
169

170
  /**
171
   * @param other the {@link VersionIdentifier} to be matched.
172
   * @return {@code true} if this {@link VersionIdentifier} is equal to the given {@link VersionIdentifier} or this {@link VersionIdentifier} is a pattern
173
   *     version (e.g. "17*" or "17.*") and the given {@link VersionIdentifier} matches to that pattern.
174
   */
175
  public boolean matches(VersionIdentifier other) {
176

177
    if (other == null) {
2✔
178
      return false;
2✔
179
    }
180
    VersionSegment thisSegment = this.start;
3✔
181
    VersionSegment otherSegment = other.start;
3✔
182
    while (true) {
183
      VersionMatchResult matchResult = thisSegment.matches(otherSegment);
4✔
184
      if (matchResult == VersionMatchResult.MATCH) {
3✔
185
        return true;
2✔
186
      } else if (matchResult == VersionMatchResult.MISMATCH) {
3✔
187
        return false;
2✔
188
      }
189
      thisSegment = thisSegment.getNextOrEmpty();
3✔
190
      otherSegment = otherSegment.getNextOrEmpty();
3✔
191
    }
1✔
192
  }
193

194
  @Override
195
  public VersionIdentifier getMin() {
196

197
    return this;
×
198
  }
199

200
  @Override
201
  public VersionIdentifier getMax() {
202

203
    return this;
×
204
  }
205

206
  @Override
207
  public boolean contains(VersionIdentifier version) {
208

209
    return matches(version);
4✔
210
  }
211

212
  @Override
213
  public int hashCode() {
214

215
    VersionSegment segment = this.start;
×
216
    int hash = 1;
×
217
    while (segment != null) {
×
218
      hash = hash * 31 + segment.hashCode();
×
219
      segment = segment.getNextOrNull();
×
220
    }
221
    return hash;
×
222
  }
223

224
  @Override
225
  public boolean equals(Object obj) {
226

227
    if (obj == this) {
3✔
228
      return true;
2✔
229
    } else if (!(obj instanceof VersionIdentifier)) {
3✔
230
      return false;
2✔
231
    }
232
    VersionIdentifier other = (VersionIdentifier) obj;
3✔
233
    return Objects.equals(this.start, other.start);
6✔
234
  }
235

236
  @Override
237
  public String toString() {
238

239
    StringBuilder sb = new StringBuilder();
4✔
240
    VersionSegment segment = this.start;
3✔
241
    while (segment != null) {
2✔
242
      sb.append(segment.toString());
5✔
243
      segment = segment.getNextOrNull();
4✔
244
    }
245
    return sb.toString();
3✔
246
  }
247

248
  /**
249
   * @param version the {@link #toString() string representation} of the {@link VersionIdentifier} to parse.
250
   * @return the parsed {@link VersionIdentifier}.
251
   */
252
  public static VersionIdentifier of(String version) {
253

254
    if (version == null) {
2✔
255
      return null;
2✔
256
    } else if (version.equals("latest")) {
4!
257
      return VersionIdentifier.LATEST;
×
258
    }
259
    VersionSegment startSegment = VersionSegment.of(version);
3✔
260
    if (startSegment == null) {
2✔
261
      return null;
2✔
262
    }
263
    return new VersionIdentifier(startSegment);
5✔
264
  }
265

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