From cfdb07ca8d2d2443863747e09c7b3e6eadfbb99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Divi=C5=A1?= <45868547+michaldivis@users.noreply.github.com> Date: Fri, 3 Nov 2023 23:12:45 +0100 Subject: [PATCH] fix: refactoring to resolve compiler warnings (#25) --- src/DarkMusicConcepts.Units/Bpm.cs | 26 ++++++++++++- src/DarkMusicConcepts.Units/Frequency.cs | 24 ++++++++++++ src/DarkMusicConcepts.Units/MidiNumber.cs | 24 ++++++++++++ src/DarkMusicConcepts.Units/MidiVelocity.cs | 24 ++++++++++++ src/DarkMusicConcepts.Units/Time.cs | 26 ++++++++++++- src/DarkMusicConcepts.Units/Unit.cs | 39 +++++-------------- .../UnitsTests/TimeTests.cs | 2 +- .../UnitsTests/UnitTests.cs | 24 ++++++++++++ 8 files changed, 155 insertions(+), 34 deletions(-) diff --git a/src/DarkMusicConcepts.Units/Bpm.cs b/src/DarkMusicConcepts.Units/Bpm.cs index 576a148..9649ad3 100644 --- a/src/DarkMusicConcepts.Units/Bpm.cs +++ b/src/DarkMusicConcepts.Units/Bpm.cs @@ -8,6 +8,30 @@ public class Bpm : Unit public static Bpm Min { get; } = From(MinValue); public static Bpm Max { get; } = From(MaxValue); + private Bpm(double value) : base(value) + { + } + protected override double GetMinValue() => MinValue; protected override double GetMaxValue() => MaxValue; -} + + public static Bpm From(double value) + { + var bpm = new Bpm(value); + + bpm.Validate(); + + return bpm; + } + + public static bool TryFrom(double value, out Bpm bpm) + { + var x = new Bpm(value); + + bpm = x.TryValidate() + ? x + : null!; + + return bpm is not null; + } +} \ No newline at end of file diff --git a/src/DarkMusicConcepts.Units/Frequency.cs b/src/DarkMusicConcepts.Units/Frequency.cs index e0dcebe..42ac56d 100644 --- a/src/DarkMusicConcepts.Units/Frequency.cs +++ b/src/DarkMusicConcepts.Units/Frequency.cs @@ -14,6 +14,10 @@ public class Frequency : Unit public static Frequency Min { get; } = From(MinValue); public static Frequency Max { get; } = From(MaxValue); + private Frequency(double value) : base(value) + { + } + protected override double GetMinValue() => MinValue; protected override double GetMaxValue() => MaxValue; @@ -21,4 +25,24 @@ public override string ToString() { return $"{Value} Hz"; } + + public static Frequency From(double value) + { + var frequency = new Frequency(value); + + frequency.Validate(); + + return frequency; + } + + public static bool TryFrom(double value, out Frequency frequency) + { + var x = new Frequency(value); + + frequency = x.TryValidate() + ? x + : null!; + + return frequency is not null; + } } diff --git a/src/DarkMusicConcepts.Units/MidiNumber.cs b/src/DarkMusicConcepts.Units/MidiNumber.cs index 1c2cd27..34c6800 100644 --- a/src/DarkMusicConcepts.Units/MidiNumber.cs +++ b/src/DarkMusicConcepts.Units/MidiNumber.cs @@ -13,6 +13,30 @@ public class MidiNumber : Unit public static MidiNumber Min { get; } = From(MinValue); public static MidiNumber Max { get; } = From(MaxValue); + private MidiNumber(int value) : base(value) + { + } + protected override int GetMinValue() => MinValue; protected override int GetMaxValue() => MaxValue; + + public static MidiNumber From(int value) + { + var midiNumber = new MidiNumber(value); + + midiNumber.Validate(); + + return midiNumber; + } + + public static bool TryFrom(int value, out MidiNumber midiNumber) + { + var x = new MidiNumber(value); + + midiNumber = x.TryValidate() + ? x + : null!; + + return midiNumber is not null; + } } diff --git a/src/DarkMusicConcepts.Units/MidiVelocity.cs b/src/DarkMusicConcepts.Units/MidiVelocity.cs index f83b8ec..2d48f6b 100644 --- a/src/DarkMusicConcepts.Units/MidiVelocity.cs +++ b/src/DarkMusicConcepts.Units/MidiVelocity.cs @@ -12,6 +12,30 @@ public class MidiVelocity : Unit public static MidiVelocity Min { get; } = From(MinValue); public static MidiVelocity Max { get; } = From(MaxValue); + private MidiVelocity(int value) : base(value) + { + } + protected override int GetMinValue() => MinValue; protected override int GetMaxValue() => MaxValue; + + public static MidiVelocity From(int value) + { + var midiVelocity = new MidiVelocity(value); + + midiVelocity.Validate(); + + return midiVelocity; + } + + public static bool TryFrom(int value, out MidiVelocity midiVelocity) + { + var x = new MidiVelocity(value); + + midiVelocity = x.TryValidate() + ? x + : null!; + + return midiVelocity is not null; + } } \ No newline at end of file diff --git a/src/DarkMusicConcepts.Units/Time.cs b/src/DarkMusicConcepts.Units/Time.cs index 074498a..c794e01 100644 --- a/src/DarkMusicConcepts.Units/Time.cs +++ b/src/DarkMusicConcepts.Units/Time.cs @@ -10,9 +10,33 @@ public class Time : Unit public static Time Min { get; } = From(MinValue); public static Time Max { get; } = From(MaxValue); + private Time(long value) : base(value) + { + } + protected override long GetMinValue() => MinValue; protected override long GetMaxValue() => MaxValue; + public static Time From(long ticks) + { + var time = new Time(ticks); + + time.Validate(); + + return time; + } + + public static bool TryFrom(long ticks, out Time time) + { + var x = new Time(ticks); + + time = x.TryValidate() + ? x + : null!; + + return time is not null; + } + private const long TicksPerQuarterNote = 960; private const long TicksPerSixteenthNote = TicksPerQuarterNote / 4; private const long TicksPerSixteenthNoteTriplet = TicksPerQuarterNote / 6; @@ -74,8 +98,6 @@ public class Time : Unit public static Time FromThirtySecondNoteDotteds(long amount) => From(amount * TicksPerSixteenthNoteDotted / 2); public static Time FromSixtyForthNoteDotteds(long amount) => From(amount * TicksPerSixteenthNoteDotted / 4); - public static Time FromTicks(long amount) => From(amount); - public static readonly Time Zero = From(0); public static readonly Time Bar = FromBars(1); diff --git a/src/DarkMusicConcepts.Units/Unit.cs b/src/DarkMusicConcepts.Units/Unit.cs index 6fcc0aa..75ba4e2 100644 --- a/src/DarkMusicConcepts.Units/Unit.cs +++ b/src/DarkMusicConcepts.Units/Unit.cs @@ -6,13 +6,18 @@ /// The actual value /// Self public abstract class Unit : IComparable, IComparable>, IEquatable> - where TThis : Unit, new() + where TThis : Unit where TValue : IComparable, IComparable, IEquatable { + protected Unit(TValue value) + { + Value = value; + } + protected abstract TValue GetMinValue(); protected abstract TValue GetMaxValue(); - private void Validate() + protected void Validate() { if (!IsValidValue(Value)) { @@ -20,7 +25,7 @@ private void Validate() } } - private bool TryValidate() + protected bool TryValidate() { return IsValidValue(Value); } @@ -45,33 +50,7 @@ private bool IsValidValue(TValue? value) return true; } - public TValue Value { get; protected set; } - - public static TThis From(TValue item) - { - var x = new TThis - { - Value = item - }; - - x.Validate(); - - return x; - } - - public static bool TryFrom(TValue item, out TThis thisValue) - { - var x = new TThis - { - Value = item - }; - - thisValue = x.TryValidate() - ? x - : null!; - - return thisValue is not null; - } + public TValue Value { get; } #region Equality diff --git a/tests/DarkMusicConcepts.Tests/UnitsTests/TimeTests.cs b/tests/DarkMusicConcepts.Tests/UnitsTests/TimeTests.cs index 25bb745..dec3d90 100644 --- a/tests/DarkMusicConcepts.Tests/UnitsTests/TimeTests.cs +++ b/tests/DarkMusicConcepts.Tests/UnitsTests/TimeTests.cs @@ -13,7 +13,7 @@ public void TryFrom_ShouldFail_WhenLessThanMin() { var success = Time.TryFrom(Time.MinValue - 1, out var invalidTime); success.Should().Be(false); - invalidTime.Should().Be(null); + invalidTime.Should().Be(null!); } [Fact] diff --git a/tests/DarkMusicConcepts.Tests/UnitsTests/UnitTests.cs b/tests/DarkMusicConcepts.Tests/UnitsTests/UnitTests.cs index 0443a50..96dbf0c 100644 --- a/tests/DarkMusicConcepts.Tests/UnitsTests/UnitTests.cs +++ b/tests/DarkMusicConcepts.Tests/UnitsTests/UnitTests.cs @@ -10,8 +10,32 @@ private class DemoUnit : Unit public static DemoUnit Min { get; } = From(MinValue); public static DemoUnit Max { get; } = From(MaxValue); + private DemoUnit(int value) : base(value) + { + } + protected override int GetMinValue() => MinValue; protected override int GetMaxValue() => MaxValue; + + public static DemoUnit From(int value) + { + var demoUnit = new DemoUnit(value); + + demoUnit.Validate(); + + return demoUnit; + } + + public static bool TryFrom(int value, out DemoUnit demoUnit) + { + var x = new DemoUnit(value); + + demoUnit = x.TryValidate() + ? x + : null!; + + return demoUnit is not null; + } } [Fact]