-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 changed file
with
241 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
using System; | ||
|
||
namespace Platform.Numbers | ||
{ | ||
public class CalcResult<T> | ||
{ | ||
public CalcResult() : this(default) { } | ||
|
||
public CalcResult(T value) | ||
{ | ||
Value = value; | ||
IsValid = true; | ||
} | ||
|
||
#region Arithmetic operations | ||
public CalcResult<T> Add(T a) | ||
{ | ||
if (IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Add(_value, a); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Subtract(T a) | ||
{ | ||
if (IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Subtract(_value, a); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Multiply(T a) | ||
{ | ||
if (IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Multiply(_value, a); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Divide(T a) | ||
{ | ||
if (IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Divide(_value, a); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Add(CalcResult<T> a) | ||
{ | ||
if (IsValid &= a.IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Add(_value, a._value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Subtract(CalcResult<T> a) | ||
{ | ||
if (IsValid &= a.IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Subtract(_value, a._value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Multiply(CalcResult<T> a) | ||
{ | ||
if (IsValid &= a.IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Multiply(_value, a._value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Divide(CalcResult<T> a) | ||
{ | ||
if (IsValid &= a.IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Divide(_value, a._value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Increment() | ||
{ | ||
if (IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Increment(_value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public CalcResult<T> Decrement() | ||
{ | ||
if (IsValid) | ||
{ | ||
try | ||
{ | ||
_value = Arithmetic<T>.Decrement(_value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Message = e.Message; | ||
IsValid = false; | ||
} | ||
} | ||
return this; | ||
} | ||
#endregion | ||
|
||
public T Value | ||
{ | ||
get => _value; | ||
set | ||
{ | ||
_value = value; | ||
IsValid = true; | ||
} | ||
} | ||
|
||
public bool IsValid; | ||
|
||
public string Message { get; private set; } | ||
|
||
private T _value; | ||
|
||
#region Operator overloading | ||
public static CalcResult<T> operator ++(CalcResult<T> x) => x.Increment(); | ||
|
||
public static CalcResult<T> operator --(CalcResult<T> x) => x.Decrement(); | ||
|
||
public static CalcResult<T> operator +(CalcResult<T> x, T y) => x.Add(y); | ||
|
||
public static CalcResult<T> operator -(CalcResult<T> x, T y) => x.Subtract(y); | ||
|
||
public static CalcResult<T> operator *(CalcResult<T> x, T y) => x.Multiply(y); | ||
|
||
public static CalcResult<T> operator /(CalcResult<T> x, T y) => x.Divide(y); | ||
|
||
public static CalcResult<T> operator +(CalcResult<T> x, CalcResult<T> y) => x.Add(y); | ||
|
||
public static CalcResult<T> operator -(CalcResult<T> x, CalcResult<T> y) => x.Subtract(y); | ||
|
||
public static CalcResult<T> operator *(CalcResult<T> x, CalcResult<T> y) => x.Multiply(y); | ||
|
||
public static CalcResult<T> operator /(CalcResult<T> x, CalcResult<T> y) => x.Divide(y); | ||
#endregion | ||
|
||
//public static explicit operator CalcResult<T>(T value) | ||
//{ | ||
// return new CalcResult<T>(value); | ||
//} | ||
|
||
|
||
public static implicit operator T(CalcResult<T> v) | ||
{ | ||
return v.Value; | ||
} | ||
|
||
public static implicit operator CalcResult<T>(T v) | ||
{ | ||
return new CalcResult<T>(v); | ||
} | ||
} | ||
} |