@@ -150,7 +150,7 @@ class Interval extends Comparable<Interval> {
150150 /// final b = Interval(3, 5);
151151 /// print(b.intersection(a)); // null
152152 ///
153- Interval intersection (Interval other) {
153+ Interval ? intersection (Interval other) {
154154 if (! intersects (other)) return null ;
155155 return Interval (_max (start, other.start), _min (end, other.end));
156156 }
@@ -177,7 +177,7 @@ class Interval extends Comparable<Interval> {
177177 /// print(a.difference(b)); // [[1, 2], [4, 5]]
178178 /// print(b.difference(a)); // null
179179 ///
180- Iterable <Interval > difference (Interval other) {
180+ Iterable <Interval >? difference (Interval other) {
181181 if (other.contains (this )) return null ;
182182 if (! other.intersects (this )) return [this ];
183183 if (other.start > start && other.end >= end) {
@@ -324,24 +324,24 @@ class IntervalTree with IterableMixin<Interval> {
324324 }
325325
326326 /// Creates a tree from [intervals] .
327- factory IntervalTree .of (Iterable <Interval > intervals) =>
327+ factory IntervalTree .of (Iterable <Interval ? > intervals) =>
328328 IntervalTree ()..addAll (intervals);
329329
330330 /// Adds an [interval] into this tree.
331- void add (dynamic interval) {
332- Interval iv = _asInterval (interval);
331+ void add (dynamic ? interval) {
332+ Interval ? iv = _asInterval (interval);
333333 if (iv == null ) return ;
334334
335335 bool joined = false ;
336- BidirectionalIterator <Interval > it = _tree.fromIterator (iv);
336+ BidirectionalIterator <Interval ? > it = _tree.fromIterator (iv);
337337 while (it.movePrevious ()) {
338338 final union = _tryJoin (it.current, iv);
339339 if (union == null ) break ;
340340 it = _tree.fromIterator (iv = union, inclusive: false );
341341 joined = true ;
342342 }
343343
344- it = _tree.fromIterator (iv, inclusive: false );
344+ it = _tree.fromIterator (iv! , inclusive: false );
345345 while (it.moveNext ()) {
346346 final union = _tryJoin (it.current, iv);
347347 if (union == null ) break ;
@@ -350,13 +350,12 @@ class IntervalTree with IterableMixin<Interval> {
350350 }
351351
352352 if (! joined) {
353- _tree.add (iv);
353+ _tree.add (iv! );
354354 }
355355 }
356356
357357 /// Adds all [intervals] into this tree.
358358 void addAll (Iterable intervals) {
359- if (intervals == null ) return ;
360359 for (final interval in intervals) {
361360 add (interval);
362361 }
@@ -365,9 +364,8 @@ class IntervalTree with IterableMixin<Interval> {
365364 /// Removes an [interval] from this tree.
366365 void remove (dynamic interval) {
367366 final iv = _asInterval (interval);
368- if (iv == null ) return ;
369367
370- BidirectionalIterator <Interval > it = _tree.fromIterator (iv);
368+ BidirectionalIterator <Interval > it = _tree.fromIterator (iv! );
371369 while (it.movePrevious ()) {
372370 final current = it.current;
373371 if (! _trySplit (it.current, iv)) break ;
@@ -384,7 +382,6 @@ class IntervalTree with IterableMixin<Interval> {
384382
385383 /// Removes all [intervals] from this tree.
386384 void removeAll (Iterable intervals) {
387- if (intervals == null ) return ;
388385 for (final interval in intervals) {
389386 remove (interval);
390387 }
@@ -423,15 +420,13 @@ class IntervalTree with IterableMixin<Interval> {
423420 @override
424421 bool contains (dynamic interval) {
425422 final iv = _asInterval (interval);
426- if (iv == null ) return false ;
427-
428- BidirectionalIterator <Interval > it = _tree.fromIterator (iv);
429- while (it.movePrevious () && iv.intersects (it.current)) {
430- if (it.current.contains (iv)) return true ;
423+ BidirectionalIterator <Interval ?> it = _tree.fromIterator (iv! );
424+ while (it.movePrevious () && iv.intersects (it.current! )) {
425+ if (it.current! .contains (iv)) return true ;
431426 }
432427 it = _tree.fromIterator (iv, inclusive: false );
433- while (it.moveNext () && it.current.intersects (iv)) {
434- if (it.current.contains (iv)) return true ;
428+ while (it.moveNext () && it.current! .intersects (iv)) {
429+ if (it.current! .contains (iv)) return true ;
435430 }
436431 return false ;
437432 }
@@ -448,11 +443,11 @@ class IntervalTree with IterableMixin<Interval> {
448443 @override
449444 bool get isNotEmpty => _tree.isNotEmpty;
450445
451- /// Returns the first interval in tree, or `null` if this tree is empty .
446+ /// Returns the first interval in tree, otherwise throw StateError .
452447 @override
453448 Interval get first => _tree.first;
454449
455- /// Returns the first interval in tree, or `null` if this tree is empty .
450+ /// Returns the first interval in tree, otherwise throw StateError .
456451 @override
457452 Interval get last => _tree.last;
458453
@@ -468,17 +463,17 @@ class IntervalTree with IterableMixin<Interval> {
468463 @override
469464 String toString () => 'IntervalTree' + super .toString ();
470465
471- Interval _asInterval (dynamic interval) {
466+ Interval ? _asInterval (dynamic ? interval) {
472467 if (interval is Iterable ) {
473468 if (interval.length != 2 || interval.first is Iterable ) {
474469 throw ArgumentError ('$interval is not an interval' );
475470 }
476471 return Interval (interval.first, interval.last);
477472 }
478- return interval as Interval ;
473+ return interval;
479474 }
480475
481- Interval _tryJoin (Interval a, Interval b) {
476+ Interval ? _tryJoin (Interval ? a, Interval ? b) {
482477 if (a == null || b == null ) return null ;
483478 if (a.contains (b)) return a;
484479 if (! a.intersects (b)) return null ;
@@ -490,7 +485,6 @@ class IntervalTree with IterableMixin<Interval> {
490485 }
491486
492487 bool _trySplit (Interval a, Interval b) {
493- if (a == null || b == null ) return false ;
494488 if (! a.intersects (b)) return false ;
495489 _tree.remove (a);
496490 _tree.addAll ([...? a.difference (b)]);
0 commit comments