@@ -410,80 +410,86 @@ abstract class InvalidLanguageVersion implements LanguageVersion {
410
410
/// Relational operators for [LanguageVersion] that
411
411
/// compare valid versions with [LanguageVersion.compareTo] .
412
412
///
413
- /// An [InvalidLanguageVersion] is not less or greater than other versions
414
- /// and is only equal to itself .
413
+ /// If either operand is an [InvalidLanguageVersion] , a [StateError] is thrown.
414
+ /// Versions should be verified as valid after parsing and before using them .
415
415
extension LanguageVersionRelationalOperators on LanguageVersion {
416
416
/// Whether this language version is less than [other] .
417
417
///
418
- /// An [InvalidLanguageVersion] is never less than or greater than
419
- /// any other language version .
418
+ /// If either version being compared is an [InvalidLanguageVersion] ,
419
+ /// a [StateError] is thrown. Verify versions are valid before comparing them .
420
420
///
421
421
/// For details on how valid language versions are compared,
422
422
/// check out [LanguageVersion.compareTo] .
423
423
bool operator < (LanguageVersion other) {
424
- // Account for invalid language versions which aren't
425
- // greater or less than any other version.
426
- if (this is InvalidLanguageVersion || other is InvalidLanguageVersion ) {
427
- return false ;
424
+ // Throw an error if comparing as or with an invalid language version.
425
+ if (this is InvalidLanguageVersion ) {
426
+ _throwThisInvalid ();
427
+ } else if (other is InvalidLanguageVersion ) {
428
+ _throwOtherInvalid ();
428
429
}
429
430
430
431
return compareTo (other) < 0 ;
431
432
}
432
433
433
434
/// Whether this language version is less than or equal to [other] .
434
435
///
435
- /// An [InvalidLanguageVersion] is never less than or greater than
436
- /// any other language version. It is equal only to itself .
436
+ /// If either version being compared is an [InvalidLanguageVersion] ,
437
+ /// a [StateError] is thrown. Verify versions are valid before comparing them .
437
438
///
438
439
/// For details on how valid language versions are compared,
439
440
/// check out [LanguageVersion.compareTo] .
440
441
bool operator <= (LanguageVersion other) {
441
- // Account for invalid language versions only being equal to themselves.
442
- if (identical (this , other)) {
443
- return true ;
444
- }
445
-
446
- if (this is InvalidLanguageVersion || other is InvalidLanguageVersion ) {
447
- return false ;
442
+ // Throw an error if comparing as or with an invalid language version.
443
+ if (this is InvalidLanguageVersion ) {
444
+ _throwThisInvalid ();
445
+ } else if (other is InvalidLanguageVersion ) {
446
+ _throwOtherInvalid ();
448
447
}
449
448
450
449
return compareTo (other) <= 0 ;
451
450
}
452
451
453
452
/// Whether this language version is greater than [other] .
454
453
///
455
- /// An [InvalidLanguageVersion] is never less than or greater than
456
- /// any other language version .
454
+ /// If either version being compared is an [InvalidLanguageVersion] ,
455
+ /// a [StateError] is thrown. Verify versions are valid before comparing them .
457
456
///
458
457
/// For details on how valid language versions are compared,
459
458
/// check out [LanguageVersion.compareTo] .
460
459
bool operator > (LanguageVersion other) {
461
- // Account for invalid language versions which aren't
462
- // greater or less than any other version.
463
- if (this is InvalidLanguageVersion || other is InvalidLanguageVersion ) {
464
- return false ;
460
+ // Throw an error if comparing as or with an invalid language version.
461
+ if (this is InvalidLanguageVersion ) {
462
+ _throwThisInvalid ();
463
+ } else if (other is InvalidLanguageVersion ) {
464
+ _throwOtherInvalid ();
465
465
}
466
466
467
467
return compareTo (other) > 0 ;
468
468
}
469
469
470
470
/// Whether this language version is greater than or equal to [other] .
471
471
///
472
- /// An [InvalidLanguageVersion] is never less than or greater than
473
- /// any other language version. It is equal only to itself .
472
+ /// If either version being compared is an [InvalidLanguageVersion] ,
473
+ /// a [StateError] is thrown. Verify versions are valid before comparing them .
474
474
///
475
475
/// For details on how valid language versions are compared,
476
476
/// check out [LanguageVersion.compareTo] .
477
477
bool operator >= (LanguageVersion other) {
478
- // Account for invalid language versions only being equal to themselves.
479
- if (identical (this , other)) {
480
- return true ;
481
- }
482
-
483
- if (this is InvalidLanguageVersion || other is InvalidLanguageVersion ) {
484
- return false ;
478
+ // Throw an error if comparing as or with an invalid language version.
479
+ if (this is InvalidLanguageVersion ) {
480
+ _throwThisInvalid ();
481
+ } else if (other is InvalidLanguageVersion ) {
482
+ _throwOtherInvalid ();
485
483
}
486
484
487
485
return compareTo (other) >= 0 ;
488
486
}
487
+
488
+ static Never _throwThisInvalid () => throw StateError (
489
+ 'Can\' t compare an invalid language version to another language version. '
490
+ 'Verify language versions are valid after parsing.' );
491
+
492
+ static Never _throwOtherInvalid () => throw StateError (
493
+ 'Can\' t compare a language version to an invalid language version. '
494
+ 'Verify language versions are valid after parsing.' );
489
495
}
0 commit comments