-
-
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
25 changed files
with
741 additions
and
15 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
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,152 @@ | ||
// Copyright 2012-2021 Dmytro Kyshchenko | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using xFunc.Maths.Analyzers; | ||
using xFunc.Maths.Analyzers.Formatters; | ||
using xFunc.Maths.Resources; | ||
|
||
namespace xFunc.Maths.Expressions.Units | ||
{ | ||
/// <summary> | ||
/// Represents the convert function. | ||
/// </summary> | ||
public class Convert : IExpression | ||
{ | ||
private readonly IConverter converter; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Convert"/> class. | ||
/// </summary> | ||
/// <param name="converter">The converter.</param> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="unit">The target unit.</param> | ||
public Convert(IConverter converter, IExpression value, StringExpression unit) | ||
: this(converter, ImmutableArray.Create(value, unit)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Convert"/> class. | ||
/// </summary> | ||
/// <param name="converter">The converter.</param> | ||
/// <param name="arguments">The list of arguments.</param> | ||
internal Convert(IConverter converter, ImmutableArray<IExpression> arguments) | ||
{ | ||
this.converter = converter ?? throw new ArgumentNullException(nameof(converter)); | ||
|
||
if (arguments == null) | ||
throw new ArgumentNullException(nameof(arguments)); | ||
|
||
if (arguments.Length < 2) | ||
throw new ParseException(Resource.LessParams); | ||
|
||
if (arguments.Length > 2) | ||
throw new ParseException(Resource.MoreParams); | ||
|
||
Value = arguments[0]; | ||
Unit = (StringExpression)arguments[1]; | ||
} | ||
|
||
/// <summary> | ||
/// Deconstructs <see cref="Convert"/>. | ||
/// </summary> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="unit">The target unit.</param> | ||
public void Deconstruct(out IExpression value, out StringExpression unit) | ||
{ | ||
value = Value; | ||
unit = Unit; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
{ | ||
if (ReferenceEquals(this, obj)) | ||
return true; | ||
|
||
if (obj is null || GetType() != obj.GetType()) | ||
return false; | ||
|
||
var (value, unit) = (Convert)obj; | ||
|
||
return Value.Equals(value) && Unit.Equals(unit); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string ToString(IFormatter formatter) => Analyze(formatter); | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() => ToString(new CommonFormatter()); | ||
|
||
/// <inheritdoc /> | ||
public object Execute() => Execute(null); | ||
|
||
/// <inheritdoc /> | ||
public object Execute(ExpressionParameters? parameters) | ||
{ | ||
var valueResult = Value.Execute(parameters); | ||
var unitResult = Unit.Execute(parameters); | ||
|
||
return unitResult switch | ||
{ | ||
string unit => converter.Convert(valueResult, unit), | ||
_ => throw new ResultIsNotSupportedException(this, valueResult, unitResult), | ||
}; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer) | ||
{ | ||
if (analyzer is null) | ||
throw new ArgumentNullException(nameof(analyzer)); | ||
|
||
return analyzer.Analyze(this); // TODO: | ||
} | ||
|
||
/// <inheritdoc /> | ||
public TResult Analyze<TResult, TContext>( | ||
IAnalyzer<TResult, TContext> analyzer, | ||
TContext context) | ||
{ | ||
if (analyzer is null) | ||
throw new ArgumentNullException(nameof(analyzer)); | ||
|
||
return analyzer.Analyze(this, context); // TODO: | ||
} | ||
|
||
/// <summary> | ||
/// Clones this instance of the <see cref="IExpression" />. | ||
/// </summary> | ||
/// <param name="left">The left argument of new expression.</param> | ||
/// <param name="right">The right argument of new expression.</param> | ||
/// <returns> | ||
/// Returns the new instance of <see cref="IExpression" /> that is a clone of this instance. | ||
/// </returns> | ||
public IExpression Clone(IExpression? left = null, StringExpression? right = null) | ||
=> new Convert(converter, left ?? Value, right ?? Unit); | ||
|
||
/// <summary> | ||
/// Gets the value. | ||
/// </summary> | ||
public IExpression Value { get; } | ||
|
||
/// <summary> | ||
/// Gets the target unit. | ||
/// </summary> | ||
public StringExpression Unit { get; } | ||
} | ||
} |
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,80 @@ | ||
// Copyright 2012-2021 Dmytro Kyshchenko | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using xFunc.Maths.Expressions.Units.AngleUnits; | ||
using xFunc.Maths.Expressions.Units.PowerUnits; | ||
|
||
namespace xFunc.Maths.Expressions.Units | ||
{ | ||
/// <summary> | ||
/// The unit converter. | ||
/// </summary> | ||
public class Converter : IConverter | ||
{ | ||
/// <inheritdoc /> | ||
public object Convert(object value, string unit) | ||
{ | ||
if (value is null) | ||
throw new ArgumentNullException(nameof(value)); | ||
if (string.IsNullOrWhiteSpace(unit)) | ||
throw new ArgumentNullException(nameof(unit)); | ||
|
||
unit = unit.ToLower(); | ||
|
||
var result = value switch | ||
{ | ||
NumberValue numberValue => Convert(numberValue, unit), | ||
AngleValue angleValue => Convert(angleValue, unit), | ||
PowerValue powerValue => Convert(powerValue, unit), | ||
_ => throw new ValueIsNotSupportedException(value), // TODO: include in UI | ||
}; | ||
|
||
return result; | ||
} | ||
|
||
private object Convert(NumberValue value, string unit) | ||
=> unit switch | ||
{ | ||
"rad" or "radian" or "radians" => AngleValue.Radian(value), | ||
"deg" or "degree" or "degrees" => AngleValue.Degree(value), | ||
"grad" or "gradian" or "gradians" => AngleValue.Gradian(value), | ||
|
||
"w" => PowerValue.Watt(value), | ||
"kw" => PowerValue.Kilowatt(value), | ||
"hp" => PowerValue.Horsepower(value), | ||
|
||
_ => throw new UnitIsNotSupportedException(unit), // TODO: include in UI | ||
}; | ||
|
||
private AngleValue Convert(AngleValue value, string unit) | ||
=> unit switch | ||
{ | ||
"rad" or "radian" or "radians" => value.ToRadian(), | ||
"deg" or "degree" or "degrees" => value.ToDegree(), | ||
"grad" or "gradian" or "gradians" => value.ToGradian(), | ||
_ => throw new UnitIsNotSupportedException(unit), | ||
}; | ||
|
||
private PowerValue Convert(PowerValue value, string unit) | ||
=> unit switch | ||
{ | ||
"w" => value.ToWatt(), | ||
"kw" => value.ToKilowatt(), | ||
"hp" => value.ToHorsepower(), | ||
_ => throw new UnitIsNotSupportedException(unit), | ||
}; | ||
} | ||
} |
Oops, something went wrong.