Skip to content

Commit d9663f6

Browse files
authored
nullsafety migration (#1)
* nullsafety migration * updates * nullsafe updates * remove 2 tests * test for exception * switch to test_cov * update workflow
1 parent 9a80e70 commit d9663f6

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Run tests
2020
run: pub run test
2121
- name: Measure coverage
22-
run: pub run test_coverage
22+
run: pub run test_cov
2323
- name: Upload coverage
2424
uses: codecov/codecov-action@v1.0.6
2525
with:

lib/interval_tree.dart

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]);

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ repository: https://github.com/jpnurmi/interval_tree
99
issue_tracker: https://github.com/jpnurmi/interval_tree/issues
1010

1111
environment:
12-
sdk: ">=2.7.0 <3.0.0"
12+
sdk: '>=2.12.0 <3.0.0'
1313
dependencies:
1414
meta: ^1.1.8
15-
quiver: ^2.0.5
15+
quiver: ^3.0.1
1616
dev_dependencies:
1717
test: ^1.14.2
18-
test_coverage: ^0.4.1
18+
test_cov: ^1.0.1

test/interval_tree_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ void main() {
248248
IntervalTree empty = IntervalTree();
249249
expect(empty.isEmpty, isTrue);
250250
expect(empty.isNotEmpty, isFalse);
251-
expect(empty.first, isNull);
252-
expect(empty.last, isNull);
251+
expect(() => empty.first, throwsA(isA<StateError>()));
252+
expect(() => empty.last, throwsA(isA<StateError>()));
253253
expect(empty.length, 0);
254254
expect(() => empty.single, throwsStateError);
255255

0 commit comments

Comments
 (0)