@@ -177,7 +177,7 @@ class GemVersion implements Comparable<GemVersion> {
177
177
gemVersions. first()
178
178
} else {
179
179
gemVersions[1 .. -1 ]. inject(gemVersions. first()) { range , value ->
180
- range. union (value)
180
+ range. intersect (value)
181
181
}
182
182
}
183
183
}
@@ -300,66 +300,53 @@ class GemVersion implements Comparable<GemVersion> {
300
300
* @since 2.0
301
301
*/
302
302
GemVersion intersect (GemVersion other ) {
303
- Boundary newLowBoundary
304
- String newLow
305
- switch (compare(low, other. low)) {
306
- case -1 :
307
- newLow = other. low
308
- newLowBoundary = other. lowBoundary
309
- break
310
- case 0 :
311
- newLowBoundary = (lowBoundary == EXCLUSIVE || other. lowBoundary == EXCLUSIVE ) ? EXCLUSIVE : INCLUSIVE
312
- newLow = low
313
- break
314
- case 1 :
315
- newLow = low
316
- newLowBoundary = lowBoundary
303
+ Tuple2<String , Boundary > newLowVersionSpec = intersect(low, lowBoundary, other. low, other. lowBoundary, true )
304
+ Tuple2<String , Boundary > newHighVersionSpec = intersect(high, highBoundary, other. high, other. highBoundary, false )
305
+ GemVersion intersection = new GemVersion (newLowVersionSpec. second, newLowVersionSpec. first, newHighVersionSpec. first, newHighVersionSpec. second)
306
+ if (intersection == this ) {
307
+ // is other a subset of this?
308
+ if (compare(this . low, other. low) >= 0 && compare(other. low, this . high) < 0 && compare(this . high, other. high) <= 0 ) {
309
+ return intersection
310
+ }
311
+ return NO_VERSION
317
312
}
318
-
319
- Boundary newHighBoundary
320
- String newHigh
321
-
322
- if (! high && other. high) {
323
- newHigh = other. high
324
- newHighBoundary = other. highBoundary
325
- } else if (high && ! other. high) {
326
- newHigh = high
327
- newHighBoundary = highBoundary
328
- } else if (! high && ! other. high) {
329
- newHigh = null
330
- newHighBoundary = highBoundary
331
- } else {
332
- switch (compare(high, other. high)) {
333
- case 1 :
334
- newHigh = other. high
335
- newHighBoundary = other. highBoundary
336
- break
337
- case 0 :
338
- newHighBoundary = (highBoundary == EXCLUSIVE || other. highBoundary == EXCLUSIVE ) ? EXCLUSIVE : INCLUSIVE
339
- newHigh = high
340
- break
341
- case -1 :
342
- newHigh = high
343
- newHighBoundary = highBoundary
313
+ if (intersection == other) {
314
+ // is this a subset of other?
315
+ if (compare(other. low, this . low) >= 0 && compare(this . low, other. high) < 0 && compare(other. high, this . high) <= 0 ) {
316
+ return intersection
344
317
}
318
+ return NO_VERSION
345
319
}
346
- return new GemVersion (newLowBoundary, newLow, newHigh, newHighBoundary)
320
+ return intersection
347
321
}
348
322
349
- /* * Creates a new GEM version requirement which that the lowest of two requirements and the highest
350
- * of those same requirement
351
- *
352
- * @param other Other GEM to combine with
353
- * @return New GEM version requirement.
354
- *
355
- * @since 2.0
356
- */
357
- GemVersion union (GemVersion other ) {
358
- List<GemVersion > pair = [this , other]
359
- GemVersion min = pair. min()
360
- GemVersion max = pair. max()
323
+ Tuple2<String ,Boundary > intersect (String version , Boundary boundary , String otherVersion , Boundary otherBoundary , boolean low ) {
324
+ Boundary newBoundary
325
+ String newVersion
326
+ if (! version && otherVersion) {
327
+ newVersion = otherVersion
328
+ newBoundary = otherBoundary
329
+ } else if (version && ! otherVersion) {
330
+ newVersion = version
331
+ newBoundary = boundary
332
+ } else if (! version && ! otherVersion) {
333
+ newVersion = null
334
+ newBoundary = boundary
335
+ } else {
336
+ int compareLow = low ? compare(version, otherVersion) : compare(otherVersion, version)
337
+ if (compareLow < 0 ) {
338
+ newVersion = otherVersion
339
+ newBoundary = otherBoundary
340
+ } else if (compareLow > 0 ) {
341
+ newVersion = version
342
+ newBoundary = boundary
343
+ } else {
344
+ newBoundary = (boundary == INCLUSIVE || otherBoundary == INCLUSIVE ) ? INCLUSIVE : EXCLUSIVE
345
+ newVersion = version
346
+ }
347
+ }
361
348
362
- new GemVersion (min . lowBoundary, min . low, max . high, max . highBoundary )
349
+ return new Tuple2 (newVersion, newBoundary )
363
350
}
364
351
365
352
/* * Allows for versions to be compared and sorted.
0 commit comments