Skip to content

Commit

Permalink
Merge pull request #1 from albertms10/null-safety
Browse files Browse the repository at this point in the history
Migrate to null-safety
  • Loading branch information
albertms10 authored Mar 6, 2021
2 parents 9215379 + 4a80b5d commit 411c540
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 51 deletions.
1 change: 1 addition & 0 deletions lib/music_notes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library music_notes;

import 'dart:math' as math show min;

import 'package:collection/collection.dart' show IterableExtension;
import 'package:quiver/core.dart';

part 'src/classes/circle_of_fifths.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/classes/enharmonic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ abstract class Enharmonic<T extends MusicItem> {
final Set<T> items;

Enharmonic(this.items)
: assert(items != null && items.isNotEmpty),
: assert(items.isNotEmpty),
assert(
items.every((item) => item.semitones == items.first.semitones),
'${T}s are not enharmonic.',
Expand Down
6 changes: 3 additions & 3 deletions lib/src/classes/enharmonic_interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class EnharmonicInterval extends Enharmonic<Interval> {

return {
if (QualitiesValues.exists(intervalBelow, semitones))
Interval.fromDesiredSemitones(intervalBelow, semitones),
Interval.fromDesiredSemitones(intervalBelow!, semitones),
if (QualitiesValues.exists(intervalAbove, semitones))
Interval.fromDesiredSemitones(intervalAbove, semitones)
Interval.fromDesiredSemitones(intervalAbove!, semitones)
};
}

Expand All @@ -51,7 +51,7 @@ class EnharmonicInterval extends Enharmonic<Interval> {
/// EnharmonicInterval.getInterval(7, Qualities.Disminuida)
/// == const Interval(Intervals.Quinta, Qualities.Disminuida)
/// ```
static Interval getInterval(int semitones, [Qualities preferredQuality]) {
static Interval getInterval(int semitones, [Qualities? preferredQuality]) {
final enharmonicIntervals =
EnharmonicInterval.fromSemitones(semitones).items;

Expand Down
6 changes: 3 additions & 3 deletions lib/src/classes/enharmonic_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class EnharmonicNote extends Enharmonic<Note> {

return {
Note(
NotesValues.fromValue(semitones - 1),
NotesValues.fromValue(semitones - 1)!,
Accidentals.Sostingut,
),
Note(
NotesValues.fromValue(semitones + 1),
NotesValues.fromValue(semitones + 1)!,
Accidentals.Bemoll,
),
};
Expand All @@ -50,7 +50,7 @@ class EnharmonicNote extends Enharmonic<Note> {
/// EnharmonicNote.getNote(5, Accidentals.Bemoll)
/// == const Note(Notes.Fa, Accidentals.Bemoll)
/// ```
static Note getNote(int semitones, [Accidentals preferredAccidental]) {
static Note getNote(int semitones, [Accidentals? preferredAccidental]) {
final enharmonicNotes = EnharmonicNote.fromSemitones(semitones).items;

return enharmonicNotes.firstWhere(
Expand Down
4 changes: 1 addition & 3 deletions lib/src/classes/interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ class Interval implements MusicItem {
final Qualities quality;
final bool descending;

const Interval(this.interval, this.quality, {this.descending = false})
: assert(interval != null),
assert(quality != null);
const Interval(this.interval, this.quality, {this.descending = false});

Interval.fromDelta(Intervals interval, int delta)
: this(interval, qualityFromDelta(interval, delta));
Expand Down
8 changes: 4 additions & 4 deletions lib/src/classes/key_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ part of music_notes;

class KeySignature {
final int number;
final Accidentals accidental;
final Accidentals? accidental;

const KeySignature(this.number, [this.accidental])
: assert(number != null),
assert(number >= 0),
: assert(number >= 0),
assert(number > 0 ? accidental != null : true);

KeySignature.fromDistance(int distance)
Expand Down Expand Up @@ -46,7 +45,8 @@ class KeySignature {
final n = i == iterations
? Music.nModValueExcludeZero(number, notesValues)
: notesValues;
list.add('$n × ${accidental.increment(i - 1).toText()}');

list.add('$n × ${accidental!.increment(i - 1)!.toText()}');
}

return list.join(', ');
Expand Down
24 changes: 14 additions & 10 deletions lib/src/classes/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ part of music_notes;

class Note implements MusicItem {
final Notes note;
final Accidentals accidental;
final Accidentals? accidental;

const Note(this.note, [this.accidental]) : assert(note != null);
const Note(this.note, [this.accidental]);

Note.copy(Note note) : this(note.note, note.accidental);

Note.fromSemitones(int semitones, [Accidentals preferredAccidental])
Note.fromSemitones(int semitones, [Accidentals? preferredAccidental])
: this.copy(EnharmonicNote.getNote(semitones, preferredAccidental));

/// Returns the [Note] from the [Tonality] given its [accidentals] number,
Expand All @@ -22,8 +22,11 @@ class Note implements MusicItem {
/// Note.fromTonalityAccidentals(0, Modes.Menor)
/// == const Note(Notes.La)
/// ```
static Note fromTonalityAccidentals(int accidentals, Modes mode,
[Accidentals accidental]) {
static Note fromTonalityAccidentals(
int accidentals,
Modes mode, [
Accidentals? accidental,
]) {
final note = fromRawAccidentals(accidentals, accidental);

return mode == Modes.Major
Expand All @@ -46,7 +49,7 @@ class Note implements MusicItem {
/// Note.fromRawAccidentals(0)
/// == const Note(Notes.La)
/// ```
static Note fromRawAccidentals(int accidentals, [Accidentals accidental]) =>
static Note fromRawAccidentals(int accidentals, [Accidentals? accidental]) =>
Note.fromSemitones(
Interval(
Intervals.Quinta,
Expand All @@ -57,7 +60,7 @@ class Note implements MusicItem {
1,
(accidental == Accidentals.Bemoll && accidentals > 8) ||
(accidental == Accidentals.Sostingut && accidentals > 10)
? accidental.incremented
? accidental!.incremented
: accidental,
);

Expand All @@ -72,7 +75,7 @@ class Note implements MusicItem {
int get semitones => Music.modValueExcludeZero(note.value + accidentalValue);

/// Returns this [Note]’s [accidental] value.
int get accidentalValue => accidental != null ? accidental.value : 0;
int get accidentalValue => accidental?.value ?? 0;

/// Returns the `delta` difference between this [Note] and [note].
int semitonesDelta(Note note) => Music.modValue(note.semitones - semitones);
Expand Down Expand Up @@ -156,7 +159,8 @@ class Note implements MusicItem {
/// const Note(Notes.La).transposeBySemitones(5)
/// == const Note(Notes.Re)
/// ```
Note transposeBySemitones(int semitones, [Accidentals preferredAccidental]) =>
Note transposeBySemitones(int semitones,
[Accidentals? preferredAccidental]) =>
Note.fromSemitones(this.semitones + semitones, preferredAccidental);

/// Returns the [Note] transposed by [interval].
Expand Down Expand Up @@ -185,7 +189,7 @@ class Note implements MusicItem {

@override
String toString() =>
note.toText() + (accidental != null ? ' ${accidental.toText()}' : '');
note.toText() + (accidental != null ? ' ${accidental!.toText()}' : '');

@override
bool operator ==(other) =>
Expand Down
3 changes: 1 addition & 2 deletions lib/src/classes/relative_tonalities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class RelativeTonalities {
final Set<Tonality> tonalities;

RelativeTonalities(this.tonalities)
: assert(tonalities != null),
assert(tonalities.length == 2),
: assert(tonalities.length == 2),
assert(
tonalities.every(
(tonality) =>
Expand Down
6 changes: 2 additions & 4 deletions lib/src/classes/tonality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ class Tonality {
final Note note;
final Modes mode;

const Tonality(this.note, this.mode)
: assert(note != null),
assert(mode != null);
const Tonality(this.note, this.mode);

Tonality.copy(Tonality tonality) : this(tonality.note, tonality.mode);

Tonality.fromAccidentals(int accidentals, Modes mode,
[Accidentals accidental])
[Accidentals? accidental])
: this(
Note.fromTonalityAccidentals(accidentals, mode, accidental),
mode,
Expand Down
16 changes: 8 additions & 8 deletions lib/src/enums/accidentals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ extension AccidentalsValues on Accidentals {
/// AccidentalsValues.fromValue(-2) == Accidentals.DobleBemoll
/// AccidentalsValues.fromValue(3) == Accidentals.TripleSostingut
/// ```
static Accidentals fromValue(int value) => accidentalsValues.keys.firstWhere(
static Accidentals? fromValue(int value) =>
accidentalsValues.keys.firstWhereOrNull(
(accidental) =>
Music.modValue(value + 3) - 3 == accidentalsValues[accidental],
orElse: () => null,
);

/// Returns the value of this [Accidentals] enum item in [accidentalsValues].
Expand All @@ -53,7 +53,7 @@ extension AccidentalsValues on Accidentals {
/// Accidentals.Bemoll.value == -1
/// Accidentals.DobleSostingut.value == 2
/// ```
int get value => accidentalsValues[this];
int get value => accidentalsValues[this]!;

/// Returns the symbol of this [Accidentals] enum item.
///
Expand All @@ -62,7 +62,7 @@ extension AccidentalsValues on Accidentals {
/// Accidentals.Bemoll.symbol == '♭'
/// Accidentals.DobleSostingut.symbol == '𝄪'
/// ```
String get symbol => accidentalsSymbols[this];
String get symbol => accidentalsSymbols[this]!;

/// Returns the incremented [Accidentals] enum item of this.
///
Expand All @@ -71,7 +71,7 @@ extension AccidentalsValues on Accidentals {
/// Accidentals.DobleBemoll.incremented == Accidentals.TripleBemoll
/// Accidentals.Sostingut.incremented == Accidentals.DobleSostingut
/// ```
Accidentals get incremented => increment(1);
Accidentals get incremented => increment(1)!;

/// Returns the decremented [Accidentals] enum item of this.
///
Expand All @@ -80,7 +80,7 @@ extension AccidentalsValues on Accidentals {
/// Accidentals.DobleBemoll.decremented == Accidentals.Bemoll
/// Accidentals.Sostingut.decremented == Accidentals.Becaire
/// ```
Accidentals get decremented => decrement(1);
Accidentals get decremented => decrement(1)!;

/// Returns the incremented [Accidentals] enum item of this by [n].
///
Expand All @@ -89,7 +89,7 @@ extension AccidentalsValues on Accidentals {
/// Accidentals.Bemoll.increment(2) == Accidentals.TripleBemoll
/// Accidentals.Sostingut.increment(1) == Accidentals.DobleSostingut
/// ```
Accidentals increment(int n) =>
Accidentals? increment(int n) =>
fromValue((value.abs() + n) * (value > 0 ? 1 : -1));

/// Returns the decremented [Accidentals] enum item of this by [n].
Expand All @@ -102,5 +102,5 @@ extension AccidentalsValues on Accidentals {
/// Accidentals.Sostingut.decrement(1) == Accidentals.Becaire
/// Accidentals.DobleBemoll.decrement(4) == Accidentals.DobleSostingut
/// ```
Accidentals decrement(int n) => increment(-n);
Accidentals? decrement(int n) => increment(-n);
}
10 changes: 5 additions & 5 deletions lib/src/enums/enums_to_string.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
part of music_notes;

extension on Notes {
String toText() => toString().split('.')[1];
String toText() => toString().split('.').last;
}

extension on Accidentals {
String toText() => toString().split('.')[1];
String toText() => toString().split('.').last;
}

extension on Modes {
String toText() => toString().split('.')[1];
String toText() => toString().split('.').last;
}

extension on Intervals {
String toText() => toString().split('.')[1];
String toText() => toString().split('.').last;
}

extension on Qualities {
String toText() => toString().split('.')[1];
String toText() => toString().split('.').last;
}
6 changes: 3 additions & 3 deletions lib/src/enums/intervals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ extension IntervalsValues on Intervals {
/// IntervalsValues.fromSemitones(0) == Intervals.Unison
/// IntervalsValues.fromSemitones(4) == null
/// ```
static Intervals fromSemitones(int semitones) => Intervals.values.firstWhere(
static Intervals? fromSemitones(int semitones) =>
Intervals.values.firstWhereOrNull(
(interval) =>
Music.modValueExcludeZero(semitones) ==
intervalsQualitiesIndex[interval],
orElse: () => null,
);

/// Returns an [Intervals] enum item that matches [ordinal].
Expand Down Expand Up @@ -79,7 +79,7 @@ extension IntervalsValues on Intervals {
/// ```
int get semitones =>
intervalsQualitiesIndex[this] ??
Music.chromaticDivisions + intervalsQualitiesIndex[inverted];
Music.chromaticDivisions + intervalsQualitiesIndex[inverted]!;

/// Returns the ordinal number of this [Intervals] enum item.
///
Expand Down
5 changes: 2 additions & 3 deletions lib/src/enums/notes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ extension NotesValues on Notes {
/// NotesValues.fromValue(8) == Notes.Sol
/// NotesValues.fromValue(11) == null
/// ```
static Notes fromValue(int value) => notesValues.keys.firstWhere(
static Notes? fromValue(int value) => notesValues.keys.firstWhereOrNull(
(note) => Music.modValueExcludeZero(value) == notesValues[note],
orElse: () => null,
);

/// Returns a [Notes] enum item that matches [ordinal].
Expand Down Expand Up @@ -65,7 +64,7 @@ extension NotesValues on Notes {
/// Notes.Sol.value == 8
/// Notes.Si.value == 12
/// ```
int get value => notesValues[this];
int get value => notesValues[this]!;

/// Returns an [Intervals] enum item that conforms an interval
/// between this [Notes] enum item and [note] in ascending manner by default.
Expand Down
4 changes: 3 additions & 1 deletion lib/src/enums/qualities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ extension QualitiesValues on Qualities {
/// QualitiesValues.exists(Intervals.Sexta, 8) == true
/// QualitiesValues.exists(Intervals.Sexta, 10) == false
/// ```
static bool exists(Intervals interval, int semitones) {
static bool exists(Intervals? interval, int semitones) {
if (interval == null) return false;

final delta = semitones - interval.semitones + (interval.isPerfect ? 0 : 1);
return delta > 0 && delta <= intervalQualitiesSet(interval).length;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ author: albertms10
repository: https://github.com/albertms10/music_notes.git

environment:
sdk: '>=2.10.0 <3.0.0'
sdk: ">=2.12.0-0 <3.0.0"

dependencies:
quiver: ^3.0.0
Expand Down

0 comments on commit 411c540

Please sign in to comment.