-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,812 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
xFunc.Maths/Expressions/Units/Converters/LengthConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Dmytro Kyshchenko. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace xFunc.Maths.Expressions.Units.Converters; | ||
|
||
/// <summary> | ||
/// The length unit converter. | ||
/// </summary> | ||
public class LengthConverter : IConverter<LengthValue>, IConverter<object> | ||
{ | ||
/// <inheritdoc /> | ||
public LengthValue Convert(object value, string unit) | ||
{ | ||
if (value is null) | ||
throw new ArgumentNullException(nameof(value)); | ||
if (string.IsNullOrWhiteSpace(unit)) | ||
throw new ArgumentNullException(nameof(unit)); | ||
|
||
if (!LengthUnit.FromName(unit, out var lengthUnit)) | ||
throw new UnitIsNotSupportedException(unit); | ||
|
||
return value switch | ||
{ | ||
LengthValue lengthValue => lengthValue.To(lengthUnit), | ||
NumberValue numberValue => new LengthValue(numberValue, lengthUnit), | ||
_ => throw new ValueIsNotSupportedException(value), | ||
}; | ||
} | ||
|
||
/// <inheritdoc/> | ||
object IConverter<object>.Convert(object value, string unit) | ||
=> Convert(value, unit); | ||
|
||
/// <inheritdoc cref="IConverter{TValue}.CanConvertTo" /> | ||
public bool CanConvertTo(string unit) | ||
=> LengthUnit.Names.Contains(unit.ToLower()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Dmytro Kyshchenko. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace xFunc.Maths.Expressions.Units.LengthUnits; | ||
|
||
/// <summary> | ||
/// Represents a length number. | ||
/// </summary> | ||
public class Length : Unit<LengthValue> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Length"/> class. | ||
/// </summary> | ||
/// <param name="value">A length value.</param> | ||
public Length(LengthValue value) | ||
: base(value) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> analyzer) | ||
=> analyzer.Analyze(this); | ||
|
||
/// <inheritdoc /> | ||
protected override TResult AnalyzeInternal<TResult, TContext>( | ||
IAnalyzer<TResult, TContext> analyzer, | ||
TContext context) | ||
=> analyzer.Analyze(this, context); | ||
} |
Oops, something went wrong.