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

devonfw / IDEasy / 11932927810

20 Nov 2024 12:00PM UTC coverage: 67.249% (-0.04%) from 67.287%
11932927810

push

github

web-flow
#689: Added url and removed try-catch-block (#698)

2460 of 3999 branches covered (61.52%)

Branch coverage included in aggregate %.

6396 of 9170 relevant lines covered (69.75%)

3.08 hits per line

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

87.33
cli/src/main/java/com/devonfw/tools/ide/version/VersionIdentifier.java
1
package com.devonfw.tools.ide.version;
2

3
import java.util.Objects;
4

5
/**
6
 * Data-type to represent a {@link VersionIdentifier} in a structured way and allowing {@link #compareVersion(VersionIdentifier) comparison} of
7
 * {@link VersionIdentifier}s.
8
 */
9
public final class VersionIdentifier implements VersionObject<VersionIdentifier>, GenericVersionRange {
10

11
  /** {@link VersionIdentifier} "*" that will resolve to the latest stable version. */
12
  public static final VersionIdentifier LATEST = VersionIdentifier.of("*");
4✔
13

14
  private final VersionSegment start;
15

16
  private final VersionLetters developmentPhase;
17

18
  private final boolean valid;
19

20
  private VersionIdentifier(VersionSegment start) {
21

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

50
  /**
51
   * @return the first {@link VersionSegment} of this {@link VersionIdentifier}. To get other segments use {@link VersionSegment#getNextOrEmpty()} or
52
   *     {@link VersionSegment#getNextOrNull()}.
53
   */
54
  public VersionSegment getStart() {
55

56
    return this.start;
3✔
57
  }
58

59
  /**
60
   * A valid {@link VersionIdentifier} has to meet the following requirements:
61
   * <ul>
62
   * <li>All {@link VersionSegment segments} themselves are {@link VersionSegment#isValid() valid}.</li>
63
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
64
   * {@link VersionSegment#getSeparator() separator} (e.g. ".1.0" or "-1-2" are not considered valid).</li>
65
   * <li>The {@link #getStart() start} {@link VersionSegment segment} shall have an {@link String#isEmpty() empty}
66
   * {@link VersionSegment#getLettersString() letter-sequence} (e.g. "RC1" or "beta" are not considered valid).</li>
67
   * <li>Have at least one {@link VersionSegment segment} with a positive {@link VersionSegment#getNumber() number}
68
   * (e.g. "0.0.0" or "0.alpha" are not considered valid).</li>
69
   * <li>Have at max one {@link VersionSegment segment} with a {@link VersionSegment#getPhase() phase} that is a real
70
   * {@link VersionPhase#isDevelopmentPhase() development phase} (e.g. "1.alpha1.beta2" or "1.0.rc1-milestone2" are not
71
   * considered valid).</li>
72
   * <li>It is NOT a {@link #isPattern() pattern}.</li>
73
   * </ul>
74
   */
75
  @Override
76
  public boolean isValid() {
77

78
    return this.valid;
3✔
79
  }
80

81
  @Override
82
  public boolean isPattern() {
83

84
    VersionSegment segment = this.start;
3✔
85
    while (segment != null) {
2✔
86
      if (segment.isPattern()) {
3✔
87
        return true;
2✔
88
      }
89
      segment = segment.getNextOrNull();
4✔
90
    }
91
    return false;
2✔
92
  }
93

94
  /**
95
   * @return the {@link VersionLetters#isDevelopmentPhase() development phase} of this {@link VersionIdentifier}. Will be {@link VersionLetters#EMPTY} if no
96
   *     development phase is specified in any {@link VersionSegment} and will be {@link VersionLetters#UNDEFINED} if more than one
97
   *     {@link VersionLetters#isDevelopmentPhase() development phase} is specified (e.g. "1.0-alpha1.rc2").
98
   */
99
  public VersionLetters getDevelopmentPhase() {
100

101
    return this.developmentPhase;
3✔
102
  }
103

104
  @Override
105
  public VersionComparisonResult compareVersion(VersionIdentifier other) {
106

107
    if (other == null) {
2!
108
      return VersionComparisonResult.GREATER_UNSAFE;
×
109
    }
110
    VersionSegment thisSegment = this.start;
3✔
111
    VersionSegment otherSegment = other.start;
3✔
112
    VersionComparisonResult result = null;
2✔
113
    boolean unsafe = false;
2✔
114
    boolean todo = true;
2✔
115
    do {
116
      result = thisSegment.compareVersion(otherSegment);
4✔
117
      if (result.isEqual()) {
3✔
118
        if (thisSegment.isEmpty() && otherSegment.isEmpty()) {
6!
119
          todo = false;
3✔
120
        } else if (result.isUnsafe()) {
3!
121
          unsafe = true;
×
122
        }
123
      } else {
124
        todo = false;
2✔
125
      }
126
      thisSegment = thisSegment.getNextOrEmpty();
3✔
127
      otherSegment = otherSegment.getNextOrEmpty();
3✔
128
    } while (todo);
2✔
129
    if (unsafe) {
2!
130
      return result.withUnsafe();
×
131
    }
132
    return result;
2✔
133
  }
134

135
  /**
136
   * @param other the {@link VersionIdentifier} to be matched.
137
   * @return {@code true} if this {@link VersionIdentifier} is equal to the given {@link VersionIdentifier} or this {@link VersionIdentifier} is a pattern
138
   *     version (e.g. "17*" or "17.*") and the given {@link VersionIdentifier} matches to that pattern.
139
   */
140
  public boolean matches(VersionIdentifier other) {
141

142
    if (other == null) {
2✔
143
      return false;
2✔
144
    }
145
    VersionSegment thisSegment = this.start;
3✔
146
    VersionSegment otherSegment = other.start;
3✔
147
    while (true) {
148
      VersionMatchResult matchResult = thisSegment.matches(otherSegment);
4✔
149
      if (matchResult == VersionMatchResult.MATCH) {
3✔
150
        return true;
2✔
151
      } else if (matchResult == VersionMatchResult.MISMATCH) {
3✔
152
        return false;
2✔
153
      }
154
      thisSegment = thisSegment.getNextOrEmpty();
3✔
155
      otherSegment = otherSegment.getNextOrEmpty();
3✔
156
    }
1✔
157
  }
158

159
  @Override
160
  public VersionIdentifier getMin() {
161

162
    return this;
×
163
  }
164

165
  @Override
166
  public VersionIdentifier getMax() {
167

168
    return this;
×
169
  }
170

171
  @Override
172
  public boolean contains(VersionIdentifier version) {
173

174
    return matches(version);
4✔
175
  }
176

177
  @Override
178
  public int hashCode() {
179

180
    VersionSegment segment = this.start;
×
181
    int hash = 1;
×
182
    while (segment != null) {
×
183
      hash = hash * 31 + segment.hashCode();
×
184
      segment = segment.getNextOrNull();
×
185
    }
186
    return hash;
×
187
  }
188

189
  @Override
190
  public boolean equals(Object obj) {
191

192
    if (obj == this) {
3✔
193
      return true;
2✔
194
    } else if (!(obj instanceof VersionIdentifier)) {
3✔
195
      return false;
2✔
196
    }
197
    VersionIdentifier other = (VersionIdentifier) obj;
3✔
198
    return Objects.equals(this.start, other.start);
6✔
199
  }
200

201
  @Override
202
  public String toString() {
203

204
    StringBuilder sb = new StringBuilder();
4✔
205
    VersionSegment segment = this.start;
3✔
206
    while (segment != null) {
2✔
207
      sb.append(segment.toString());
5✔
208
      segment = segment.getNextOrNull();
4✔
209
    }
210
    return sb.toString();
3✔
211
  }
212

213
  /**
214
   * @param version the {@link #toString() string representation} of the {@link VersionIdentifier} to parse.
215
   * @return the parsed {@link VersionIdentifier}.
216
   */
217
  public static VersionIdentifier of(String version) {
218

219
    if (version == null) {
2✔
220
      return null;
2✔
221
    } else if (version.equals("latest")) {
4!
222
      return VersionIdentifier.LATEST;
×
223
    }
224
    VersionSegment startSegment = VersionSegment.of(version);
3✔
225
    if (startSegment == null) {
2✔
226
      return null;
2✔
227
    }
228
    return new VersionIdentifier(startSegment);
5✔
229
  }
230

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