Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Side by Side Diff: lib/src/version_range.dart

Issue 2035983002: Make VersionRange implement Comparable. (Closed) Base URL: git@github.com:dart-lang/pub_semver@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'utils.dart';
5 import 'version.dart'; 6 import 'version.dart';
6 import 'version_constraint.dart'; 7 import 'version_constraint.dart';
7 import 'version_union.dart'; 8 import 'version_union.dart';
8 9
9 /// Constrains versions to a fall within a given range. 10 /// Constrains versions to a fall within a given range.
10 /// 11 ///
11 /// If there is a minimum, then this only allows versions that are at that 12 /// If there is a minimum, then this only allows versions that are at that
12 /// minimum or greater. If there is a maximum, then only versions less than 13 /// minimum or greater. If there is a maximum, then only versions less than
13 /// that are allowed. In other words, this allows `>= min, < max`. 14 /// that are allowed. In other words, this allows `>= min, < max`.
14 class VersionRange implements VersionConstraint { 15 ///
16 /// Version ranges are ordered first by their lower bounds, then by their upper
17 /// bounds. For example, `>=1.0.0 <2.0.0` is before `>=1.5.0 <2.0.0` is before
18 /// `>=1.5.0 <3.0.0`.
19 class VersionRange implements Comparable<VersionRange>, VersionConstraint {
15 /// The minimum end of the range. 20 /// The minimum end of the range.
16 /// 21 ///
17 /// If [includeMin] is `true`, this will be the minimum allowed version. 22 /// If [includeMin] is `true`, this will be the minimum allowed version.
18 /// Otherwise, it will be the highest version below the range that is not 23 /// Otherwise, it will be the highest version below the range that is not
19 /// allowed. 24 /// allowed.
20 /// 25 ///
21 /// This may be `null` in which case the range has no minimum end and allows 26 /// This may be `null` in which case the range has no minimum end and allows
22 /// any version less than the maximum. 27 /// any version less than the maximum.
23 final Version min; 28 final Version min;
24 29
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 unionIncludeMax = true; 313 unionIncludeMax = true;
309 } 314 }
310 315
311 return new VersionRange(min: unionMin, max: unionMax, 316 return new VersionRange(min: unionMin, max: unionMax,
312 includeMin: unionIncludeMin, includeMax: unionIncludeMax); 317 includeMin: unionIncludeMin, includeMax: unionIncludeMax);
313 } 318 }
314 319
315 return new VersionConstraint.unionOf([this, other]); 320 return new VersionConstraint.unionOf([this, other]);
316 } 321 }
317 322
323 int compareTo(VersionRange other) {
324 if (min == null) {
325 if (other.min == null) return compareMax(this, other);
326 return -1;
327 } else if (other.min == null) {
328 return 1;
329 }
330
331 var result = min.compareTo(other.min);
332 if (result != 0) return result;
333 if (includeMin != other.includeMin) return includeMin ? -1 : 1;
334
335 return compareMax(this, other);
336 }
337
318 String toString() { 338 String toString() {
319 var buffer = new StringBuffer(); 339 var buffer = new StringBuffer();
320 340
321 if (min != null) { 341 if (min != null) {
322 buffer.write(includeMin ? '>=' : '>'); 342 buffer.write(includeMin ? '>=' : '>');
323 buffer.write(min); 343 buffer.write(min);
324 } 344 }
325 345
326 if (max != null) { 346 if (max != null) {
327 if (min != null) buffer.write(' '); 347 if (min != null) buffer.write(' ');
328 buffer.write(includeMax ? '<=' : '<'); 348 buffer.write(includeMax ? '<=' : '<');
329 buffer.write(max); 349 buffer.write(max);
330 } 350 }
331 351
332 if (min == null && max == null) buffer.write('any'); 352 if (min == null && max == null) buffer.write('any');
333 return buffer.toString(); 353 return buffer.toString();
334 } 354 }
335 } 355 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698