44
55import 'dart:typed_data' ;
66
7+ import 'package:meta/meta.dart' show sealed;
8+
79import 'errors.dart' ;
810import 'package_config_json.dart' ;
911import 'util.dart' ;
@@ -15,7 +17,8 @@ import 'util.dart';
1517/// More members may be added to this class in the future,
1618/// so classes outside of this package must not implement [PackageConfig]
1719/// or any subclass of it.
18- abstract final class PackageConfig {
20+ @sealed
21+ abstract class PackageConfig {
1922 /// The lowest configuration version currently supported.
2023 static const int minVersion = 2 ;
2124
@@ -213,7 +216,8 @@ abstract final class PackageConfig {
213216}
214217
215218/// Configuration data for a single package.
216- abstract final class Package {
219+ @sealed
220+ abstract class Package {
217221 /// Creates a package with the provided properties.
218222 ///
219223 /// The [name] must be a valid package name.
@@ -301,7 +305,8 @@ abstract final class Package {
301305/// If errors during parsing are handled using an `onError` handler,
302306/// then an *invalid* language version may be represented by an
303307/// [InvalidLanguageVersion] object.
304- abstract final class LanguageVersion implements Comparable <LanguageVersion > {
308+ @sealed
309+ abstract class LanguageVersion implements Comparable <LanguageVersion > {
305310 /// The maximal value allowed by [major] and [minor] values;
306311 static const int maxValue = 0x7FFFFFFF ;
307312
@@ -368,40 +373,6 @@ abstract final class LanguageVersion implements Comparable<LanguageVersion> {
368373 @override
369374 int compareTo (LanguageVersion other);
370375
371- /// Whether this language version is less than [other] .
372- ///
373- /// If either version being compared is an [InvalidLanguageVersion] ,
374- /// a [StateError] is thrown. Verify versions are valid before comparing them.
375- ///
376- /// For details on how valid language versions are compared,
377- /// check out [LanguageVersion.compareTo] .
378- bool operator < (LanguageVersion other);
379-
380- /// Whether this language version is less than or equal to [other] .
381- ///
382- /// If either version being compared is an [InvalidLanguageVersion] ,
383- /// a [StateError] is thrown. Verify versions are valid before comparing them.
384- ///
385- /// For details on how valid language versions are compared,
386- /// check out [LanguageVersion.compareTo] .
387- bool operator <= (LanguageVersion other);
388-
389- /// Whether this language version is greater than [other] .
390- ///
391- /// Neither version being compared must be an [InvalidLanguageVersion] .
392- ///
393- /// For details on how valid language versions are compared,
394- /// check out [LanguageVersion.compareTo] .
395- bool operator > (LanguageVersion other);
396-
397- /// Whether this language version is greater than or equal to [other] .
398- ///
399- /// Neither version being compared must be an [InvalidLanguageVersion] .
400- ///
401- /// For details on how valid language versions are compared,
402- /// check out [LanguageVersion.compareTo] .
403- bool operator >= (LanguageVersion other);
404-
405376 /// Valid language versions with the same [major] and [minor] values are
406377 /// equal.
407378 ///
@@ -428,7 +399,8 @@ abstract final class LanguageVersion implements Comparable<LanguageVersion> {
428399/// which did not throw on an error.
429400/// The caller which provided the `onError` handler which was called
430401/// should be prepared to encounter invalid values.
431- abstract final class InvalidLanguageVersion implements LanguageVersion {
402+ @sealed
403+ abstract class InvalidLanguageVersion implements LanguageVersion {
432404 /// The value -1 for an invalid language version.
433405 @override
434406 int get major;
@@ -449,14 +421,83 @@ abstract final class InvalidLanguageVersion implements LanguageVersion {
449421 String toString ();
450422}
451423
424+ /// Relational operators for [LanguageVersion] .
425+ ///
426+ /// Compares valid versions with [LanguageVersion.compareTo] ,
427+ /// and rejects invalid versions.
428+ ///
429+ /// Versions should be verified as valid before using them.
430+ extension LanguageVersionRelationalOperators on LanguageVersion {
431+ /// Whether this language version is less than [other] .
432+ ///
433+ /// Neither version being compared must be an [InvalidLanguageVersion] .
434+ ///
435+ /// For details on how valid language versions are compared,
436+ /// check out [LanguageVersion.compareTo] .
437+ bool operator < (LanguageVersion other) {
438+ // Throw an error if comparing an invalid language version.
439+ if (this is InvalidLanguageVersion ) _throwThisInvalid ();
440+ if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
441+ return compareTo (other) < 0 ;
442+ }
443+
444+ /// Whether this language version is less than or equal to [other] .
445+ ///
446+ /// Neither version being compared must be an [InvalidLanguageVersion] .
447+ ///
448+ /// For details on how valid language versions are compared,
449+ /// check out [LanguageVersion.compareTo] .
450+ bool operator <= (LanguageVersion other) {
451+ // Throw an error if comparing an invalid language version.
452+ if (this is InvalidLanguageVersion ) _throwThisInvalid ();
453+ if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
454+ return compareTo (other) <= 0 ;
455+ }
456+
457+ /// Whether this language version is greater than [other] .
458+ ///
459+ /// Neither version being compared must be an [InvalidLanguageVersion] .
460+ ///
461+ /// For details on how valid language versions are compared,
462+ /// check out [LanguageVersion.compareTo] .
463+ bool operator > (LanguageVersion other) {
464+ // Throw an error if comparing an invalid language version.
465+ if (this is InvalidLanguageVersion ) _throwThisInvalid ();
466+ if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
467+ return compareTo (other) > 0 ;
468+ }
469+
470+ /// Whether this language version is greater than or equal to [other] .
471+ ///
472+ /// If either version being compared is an [InvalidLanguageVersion] ,
473+ /// a [StateError] is thrown. Verify versions are valid before comparing them.
474+ ///
475+ /// For details on how valid language versions are compared,
476+ /// check out [LanguageVersion.compareTo] .
477+ bool operator >= (LanguageVersion other) {
478+ // Throw an error if comparing an invalid language version.
479+ if (this is InvalidLanguageVersion ) _throwThisInvalid ();
480+ if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
481+ return compareTo (other) >= 0 ;
482+ }
483+
484+ static Never _throwThisInvalid () => throw UnsupportedError (
485+ 'Can\' t compare an invalid language version to another language version. '
486+ 'Verify language versions are valid before use.' );
487+ static Never _throwOtherInvalid () => throw UnsupportedError (
488+ 'Can\' t compare a language version to an invalid language version. '
489+ 'Verify language versions are valid before use.' );
490+ }
491+
452492// --------------------------------------------------------------------
453- // Implementation of interfaces.
493+ // Implementation of interfaces. Not exported by top-level libraries.
454494
455495const bool _disallowPackagesInsidePackageUriRoot = false ;
456496
457497// Implementations of the main data types exposed by the API of this package.
458498
459- final class SimplePackageConfig implements PackageConfig {
499+ @sealed
500+ class SimplePackageConfig implements PackageConfig {
460501 @override
461502 final int version;
462503 final Map <String , Package > _packages;
@@ -620,7 +661,8 @@ final class SimplePackageConfig implements PackageConfig {
620661}
621662
622663/// Configuration data for a single package.
623- final class SimplePackage implements Package {
664+ @sealed
665+ class SimplePackage implements Package {
624666 @override
625667 final String name;
626668 @override
@@ -779,7 +821,8 @@ LanguageVersion parseLanguageVersion(
779821 return SimpleLanguageVersion (major, minor, source);
780822}
781823
782- abstract final class _SimpleLanguageVersionBase implements LanguageVersion {
824+ @sealed
825+ abstract class _SimpleLanguageVersionBase implements LanguageVersion {
783826 @override
784827 int compareTo (LanguageVersion other) {
785828 var result = major - other.major;
@@ -788,7 +831,8 @@ abstract final class _SimpleLanguageVersionBase implements LanguageVersion {
788831 }
789832}
790833
791- final class SimpleLanguageVersion extends _SimpleLanguageVersionBase {
834+ @sealed
835+ class SimpleLanguageVersion extends _SimpleLanguageVersionBase {
792836 @override
793837 final int major;
794838 @override
@@ -805,66 +849,10 @@ final class SimpleLanguageVersion extends _SimpleLanguageVersionBase {
805849
806850 @override
807851 String toString () => _source ?? = '$major .$minor ' ;
808-
809- /// Whether this language version is less than [other] .
810- ///
811- /// Neither version being compared must be an [InvalidLanguageVersion] .
812- ///
813- /// For details on how valid language versions are compared,
814- /// check out [LanguageVersion.compareTo] .
815- @override
816- bool operator < (LanguageVersion other) {
817- // Throw an error if comparing with an invalid language version.
818- if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
819-
820- return compareTo (other) < 0 ;
821- }
822-
823- /// Whether this language version is less than or equal to [other] .
824- ///
825- /// Neither version being compared must be an [InvalidLanguageVersion] .
826- ///
827- /// For details on how valid language versions are compared,
828- /// check out [LanguageVersion.compareTo] .
829- @override
830- bool operator <= (LanguageVersion other) {
831- // Throw an error if comparing with an invalid language version.
832- if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
833- return compareTo (other) <= 0 ;
834- }
835-
836- /// Whether this language version is greater than [other] .
837- ///
838- /// Neither version being compared must be an [InvalidLanguageVersion] .
839- ///
840- /// For details on how valid language versions are compared,
841- /// check out [LanguageVersion.compareTo] .
842- @override
843- bool operator > (LanguageVersion other) {
844- if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
845- return compareTo (other) > 0 ;
846- }
847-
848- /// Whether this language version is greater than or equal to [other] .
849- ///
850- /// If either version being compared is an [InvalidLanguageVersion] ,
851- /// a [StateError] is thrown. Verify versions are valid before comparing them.
852- ///
853- /// For details on how valid language versions are compared,
854- /// check out [LanguageVersion.compareTo] .
855- @override
856- bool operator >= (LanguageVersion other) {
857- // Throw an error if comparing with an invalid language version.
858- if (other is InvalidLanguageVersion ) _throwOtherInvalid ();
859- return compareTo (other) >= 0 ;
860- }
861-
862- static Never _throwOtherInvalid () => throw StateError (
863- 'Can\' t compare a language version to an invalid language version. '
864- 'Verify language versions are valid after parsing.' );
865852}
866853
867- final class SimpleInvalidLanguageVersion extends _SimpleLanguageVersionBase
854+ @sealed
855+ class SimpleInvalidLanguageVersion extends _SimpleLanguageVersionBase
868856 implements InvalidLanguageVersion {
869857 final String ? _source;
870858 SimpleInvalidLanguageVersion (this ._source);
@@ -873,32 +861,8 @@ final class SimpleInvalidLanguageVersion extends _SimpleLanguageVersionBase
873861 @override
874862 int get minor => - 1 ;
875863
876- @override
877- bool operator < (LanguageVersion other) {
878- _throwThisInvalid ();
879- }
880-
881- @override
882- bool operator <= (LanguageVersion other) {
883- _throwThisInvalid ();
884- }
885-
886- @override
887- bool operator > (LanguageVersion other) {
888- _throwThisInvalid ();
889- }
890-
891- @override
892- bool operator >= (LanguageVersion other) {
893- _throwThisInvalid ();
894- }
895-
896864 @override
897865 String toString () => _source! ;
898-
899- static Never _throwThisInvalid () => throw StateError (
900- 'Can\' t compare an invalid language version to another language version. '
901- 'Verify language versions are valid after parsing.' );
902866}
903867
904868abstract class PackageTree {
0 commit comments