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

devonfw / IDEasy / 11061200804

26 Sep 2024 10:27PM UTC coverage: 66.053% (-0.05%) from 66.107%
11061200804

push

github

web-flow
#593: #651: #564: #439: fixed bugs, refactored tool dependencies (#652)

2312 of 3848 branches covered (60.08%)

Branch coverage included in aggregate %.

6078 of 8854 relevant lines covered (68.65%)

3.03 hits per line

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

85.37
cli/src/main/java/com/devonfw/tools/ide/version/VersionRange.java
1
package com.devonfw.tools.ide.version;
2

3
import java.util.Objects;
4

5
/**
6
 * Container for a range of versions. The lower and upper bounds can be exclusive or inclusive. If a bound is null, it means that this direction is unbounded.
7
 * The boolean defining whether this bound is inclusive or exclusive is ignored in this case.
8
 */
9
public final class VersionRange implements Comparable<VersionRange>, GenericVersionRange {
10

11
  /** The unbounded {@link VersionRange} instance. */
12
  public static final VersionRange UNBOUNDED = new VersionRange(null, null, BoundaryType.OPEN);
8✔
13

14
  private final VersionIdentifier min;
15

16
  private final VersionIdentifier max;
17

18
  private final BoundaryType boundaryType;
19

20
  private static final String VERSION_SEPARATOR = ",";
21

22
  /**
23
   * The constructor.
24
   *
25
   * @param min the {@link #getMin() minimum}.
26
   * @param max the {@link #getMax() maximum}.
27
   * @param boundaryType the {@link BoundaryType} defining whether the boundaries of the range are inclusive or exclusive.
28
   */
29
  private VersionRange(VersionIdentifier min, VersionIdentifier max, BoundaryType boundaryType) {
30

31
    super();
2✔
32
    Objects.requireNonNull(boundaryType);
3✔
33
    this.min = min;
3✔
34
    this.max = max;
3✔
35
    this.boundaryType = boundaryType;
3✔
36
    if ((min != null) && (max != null) && min.isGreater(max)) {
8✔
37
      throw new IllegalArgumentException(toString());
6✔
38
    } else if ((min == null) && !boundaryType.isLeftExclusive()) {
5✔
39
      throw new IllegalArgumentException(toString());
6✔
40
    } else if ((max == null) && !boundaryType.isRightExclusive()) {
5✔
41
      throw new IllegalArgumentException(toString());
6✔
42
    }
43

44
  }
1✔
45

46
  @Override
47
  public VersionIdentifier getMin() {
48

49
    return this.min;
3✔
50
  }
51

52
  @Override
53
  public VersionIdentifier getMax() {
54

55
    return this.max;
3✔
56
  }
57

58
  @Override
59
  public BoundaryType getBoundaryType() {
60

61
    return this.boundaryType;
3✔
62
  }
63

64
  @Override
65
  public boolean isPattern() {
66

67
    return true;
×
68
  }
69

70
  @Override
71
  public boolean contains(VersionIdentifier version) {
72

73
    if (this.min != null) {
3!
74
      VersionComparisonResult compareMin = version.compareVersion(this.min);
5✔
75
      if (compareMin.isLess()) {
3✔
76
        return false;
2✔
77
      } else if (compareMin.isEqual() && this.boundaryType.isLeftExclusive() && !version.isPattern()) {
10✔
78
        return false;
2✔
79
      }
80
    }
81
    if (this.max != null) {
3!
82
      VersionComparisonResult compareMax = version.compareVersion(this.max);
5✔
83
      if (compareMax.isGreater()) {
3✔
84
        return false;
2✔
85
      } else if (compareMax.isEqual() && this.boundaryType.isRightExclusive() && !version.isPattern()) {
10✔
86
        return false;
2✔
87
      }
88
    }
89
    return true;
2✔
90
  }
91

92
  @Override
93
  public int compareTo(VersionRange o) {
94

95
    if (this.min == null) {
3!
96
      if (o == null) {
×
97
        return 1; // should never happen
×
98
      } else if (o.min == null) {
×
99
        return 0;
×
100
      }
101
      return -1;
×
102
    }
103
    int compareMins = this.min.compareTo(o.min);
6✔
104
    if (compareMins == 0) {
2✔
105
      return this.boundaryType.isLeftExclusive() == o.boundaryType.isLeftExclusive() ? 0
10✔
106
          : this.boundaryType.isLeftExclusive() ? 1 : -1;
7✔
107
    } else {
108
      return compareMins;
2✔
109
    }
110
  }
111

112
  @Override
113
  public boolean equals(Object obj) {
114

115
    if (this == obj) {
3✔
116
      return true;
2✔
117
    } else if ((obj == null) || (getClass() != obj.getClass())) {
7!
118
      return false;
2✔
119
    }
120
    VersionRange o = (VersionRange) obj;
3✔
121
    if (this.boundaryType != o.boundaryType) {
5✔
122
      return false;
2✔
123
    } else if (!Objects.equals(this.min, o.min)) {
6✔
124
      return false;
2✔
125
    } else if (!Objects.equals(this.max, o.max)) {
6✔
126
      return false;
2✔
127
    }
128
    return true;
2✔
129
  }
130

131
  @Override
132
  public String toString() {
133

134
    StringBuilder sb = new StringBuilder();
4✔
135
    sb.append(this.boundaryType.getPrefix());
6✔
136
    if (this.min != null) {
3✔
137
      sb.append(this.min);
5✔
138
    }
139
    sb.append(VERSION_SEPARATOR);
4✔
140
    if (this.max != null) {
3✔
141
      sb.append(this.max);
5✔
142
    }
143
    sb.append(this.boundaryType.getSuffix());
6✔
144
    return sb.toString();
3✔
145
  }
146

147
  /**
148
   * @param value the {@link #toString() string representation} of a {@link VersionRange} to parse.
149
   * @return the parsed {@link VersionRange}.
150
   */
151
  public static VersionRange of(String value) {
152

153
    Boolean isleftExclusive = null;
2✔
154
    Boolean isRightExclusive = null;
2✔
155
    if (value.startsWith(BoundaryType.START_EXCLUDING_PREFIX)) {
4✔
156
      isleftExclusive = Boolean.TRUE;
2✔
157
      value = value.substring(BoundaryType.START_EXCLUDING_PREFIX.length());
6✔
158
    } else if (value.startsWith(BoundaryType.START_INCLUDING_PREFIX)) {
4✔
159
      isleftExclusive = Boolean.FALSE;
2✔
160
      value = value.substring(BoundaryType.START_INCLUDING_PREFIX.length());
5✔
161
    }
162
    if (value.endsWith(BoundaryType.END_EXCLUDING_SUFFIX)) {
4✔
163
      isRightExclusive = Boolean.TRUE;
2✔
164
      value = value.substring(0, value.length() - BoundaryType.END_EXCLUDING_SUFFIX.length());
10✔
165
    } else if (value.endsWith(BoundaryType.END_INCLUDING_SUFFIX)) {
4✔
166
      isRightExclusive = Boolean.FALSE;
2✔
167
      value = value.substring(0, value.length() - BoundaryType.END_INCLUDING_SUFFIX.length());
9✔
168
    }
169
    VersionIdentifier min = null;
2✔
170
    VersionIdentifier max = null;
2✔
171
    int index = value.indexOf(VERSION_SEPARATOR);
4✔
172
    if (index < 0) {
2!
173
      min = VersionIdentifier.of(value);
×
174
      max = min;
×
175
    } else {
176
      String minString = value.substring(0, index);
5✔
177
      if (!minString.isEmpty()) {
3✔
178
        min = VersionIdentifier.of(minString);
3✔
179
      }
180
      String maxString = value.substring(index + 1);
6✔
181
      if (!maxString.isEmpty()) {
3✔
182
        max = VersionIdentifier.of(maxString);
3✔
183
      }
184
    }
185
    if (isleftExclusive == null) {
2✔
186
      isleftExclusive = Boolean.valueOf(min == null);
7✔
187
    }
188
    if (isRightExclusive == null) {
2✔
189
      isRightExclusive = Boolean.valueOf(max == null);
7✔
190
    }
191
    if ((min == null) && (max == null) && isleftExclusive && isRightExclusive) {
10✔
192
      return UNBOUNDED;
2✔
193
    }
194
    return new VersionRange(min, max, BoundaryType.of(isleftExclusive.booleanValue(), isRightExclusive.booleanValue()));
11✔
195
  }
196

197
  /**
198
   * @param min the {@link #getMin() minimum}.
199
   * @param max the {@link #getMax() maximum}.
200
   * @param type the {@link BoundaryType} defining whether the boundaries of the range are inclusive or exclusive.
201
   * @return the {@link VersionRange} created from the given values.
202
   */
203
  public static VersionRange of(VersionIdentifier min, VersionIdentifier max, BoundaryType type) {
204

205
    if (type == null) {
2!
206
      type = BoundaryType.of(min == null, max == null);
×
207
    }
208
    if ((min == null) && (max == null)) {
2!
209
      assert (type == BoundaryType.OPEN);
×
210
      return UNBOUNDED;
×
211
    }
212
    return new VersionRange(min, max, type);
7✔
213
  }
214

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

© 2026 Coveralls, Inc