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

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

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

85.71
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> {
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
  /**
82
   * Determines if this {@link VersionIdentifier} is a pattern (e.g. "17*" or "17.*").
83
   *
84
   * @return {@code true} if this {@link VersionIdentifier} is a pattern and not a normal version or in other words if it {@link #getStart() has} a
85
   * {@link VersionSegment segment} that {@link VersionSegment#isPattern() is a pattern}, {@code false} otherwise.
86
   */
87
  public boolean isPattern() {
88

89
    VersionSegment segment = this.start;
3✔
90
    while (segment != null) {
2✔
91
      if (segment.isPattern()) {
3✔
92
        return true;
2✔
93
      }
94
      segment = segment.getNextOrNull();
4✔
95
    }
96
    return false;
2✔
97
  }
98

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

106
    return this.developmentPhase;
3✔
107
  }
108

109
  @Override
110
  public VersionComparisonResult compareVersion(VersionIdentifier other) {
111

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

140
  /**
141
   * @param other the {@link VersionIdentifier} to be matched.
142
   * @return {@code true} if this {@link VersionIdentifier} is equal to the given {@link VersionIdentifier} or this {@link VersionIdentifier} is a pattern
143
   * version (e.g. "17*" or "17.*") and the given {@link VersionIdentifier} matches to that pattern.
144
   */
145
  public boolean matches(VersionIdentifier other) {
146

147
    if (other == null) {
2!
148
      return false;
×
149
    }
150
    VersionSegment thisSegment = this.start;
3✔
151
    VersionSegment otherSegment = other.start;
3✔
152
    boolean todo = true;
2✔
153
    do {
154
      VersionMatchResult matchResult = thisSegment.matches(otherSegment);
4✔
155
      if (matchResult == VersionMatchResult.MATCH) {
3✔
156
        return true;
2✔
157
      } else if (matchResult == VersionMatchResult.MISMATCH) {
3✔
158
        return false;
2✔
159
      }
160
      thisSegment = thisSegment.getNextOrEmpty();
3✔
161
      otherSegment = otherSegment.getNextOrEmpty();
3✔
162
    } while (todo);
2!
163
    return true;
×
164
  }
165

166
  @Override
167
  public int hashCode() {
168

169
    VersionSegment segment = this.start;
×
170
    int hash = 1;
×
171
    while (segment != null) {
×
172
      hash = hash * 31 + segment.hashCode();
×
173
      segment = segment.getNextOrNull();
×
174
    }
175
    return hash;
×
176
  }
177

178
  @Override
179
  public boolean equals(Object obj) {
180

181
    if (obj == this) {
3!
182
      return true;
×
183
    } else if (!(obj instanceof VersionIdentifier)) {
3✔
184
      return false;
2✔
185
    }
186
    VersionIdentifier other = (VersionIdentifier) obj;
3✔
187
    return Objects.equals(this.start, other.start);
6✔
188
  }
189

190
  @Override
191
  public String toString() {
192

193
    StringBuilder sb = new StringBuilder();
4✔
194
    VersionSegment segment = this.start;
3✔
195
    while (segment != null) {
2✔
196
      sb.append(segment.toString());
5✔
197
      segment = segment.getNextOrNull();
4✔
198
    }
199
    return sb.toString();
3✔
200
  }
201

202
  /**
203
   * @param version the {@link #toString() string representation} of the {@link VersionIdentifier} to parse.
204
   * @return the parsed {@link VersionIdentifier}.
205
   */
206
  public static VersionIdentifier of(String version) {
207

208
    if (version.equals("latest")) {
4!
209
      return VersionIdentifier.LATEST;
×
210
    }
211
    VersionSegment startSegment = VersionSegment.of(version);
3✔
212
    if (startSegment == null) {
2✔
213
      return null;
2✔
214
    }
215
    return new VersionIdentifier(startSegment);
5✔
216
  }
217

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