Skip to content

Commit

Permalink
fix: refactoring to resolve compiler warnings (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldivis committed Nov 3, 2023
1 parent c111eb9 commit cfdb07c
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 34 deletions.
26 changes: 25 additions & 1 deletion src/DarkMusicConcepts.Units/Bpm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ public class Bpm : Unit<double, Bpm>
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;
}
}
24 changes: 24 additions & 0 deletions src/DarkMusicConcepts.Units/Frequency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,35 @@ public class Frequency : Unit<double, Frequency>
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;

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;
}
}
24 changes: 24 additions & 0 deletions src/DarkMusicConcepts.Units/MidiNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ public class MidiNumber : Unit<int, MidiNumber>
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;
}
}
24 changes: 24 additions & 0 deletions src/DarkMusicConcepts.Units/MidiVelocity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ public class MidiVelocity : Unit<int, MidiVelocity>
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;
}
}
26 changes: 24 additions & 2 deletions src/DarkMusicConcepts.Units/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,33 @@ public class Time : Unit<long, Time>
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;
Expand Down Expand Up @@ -74,8 +98,6 @@ public class Time : Unit<long, Time>
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);
Expand Down
39 changes: 9 additions & 30 deletions src/DarkMusicConcepts.Units/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
/// <typeparam name="TValue">The actual value</typeparam>
/// <typeparam name="TThis">Self</typeparam>
public abstract class Unit<TValue, TThis> : IComparable, IComparable<Unit<TValue, TThis>>, IEquatable<Unit<TValue, TThis>>
where TThis : Unit<TValue, TThis>, new()
where TThis : Unit<TValue, TThis>
where TValue : IComparable, IComparable<TValue>, IEquatable<TValue>
{
protected Unit(TValue value)
{
Value = value;
}

protected abstract TValue GetMinValue();
protected abstract TValue GetMaxValue();

private void Validate()
protected void Validate()
{
if (!IsValidValue(Value))
{
throw new ArgumentOutOfRangeException(nameof(Value), Value, $"{nameof(Value)} has to be within range {GetMinValue()}-{GetMaxValue()}");
}
}

private bool TryValidate()
protected bool TryValidate()
{
return IsValidValue(Value);
}
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/DarkMusicConcepts.Tests/UnitsTests/TimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 24 additions & 0 deletions tests/DarkMusicConcepts.Tests/UnitsTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,32 @@ private class DemoUnit : Unit<int, DemoUnit>
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]
Expand Down

0 comments on commit cfdb07c

Please sign in to comment.