diff --git a/Src/Controls/Objects/Optional.cs b/Src/Controls/Objects/Optional.cs
index 6491503..10cb31b 100644
--- a/Src/Controls/Objects/Optional.cs
+++ b/Src/Controls/Objects/Optional.cs
@@ -6,8 +6,14 @@
// https://stackoverflow.com/questions/65787544/nullable-enum-type-cannot-be-assigned-to-null-when-used-as-a-generic
// ***************************************************************************************
+using System;
+
namespace PPlus.Controls.Objects
{
+ ///
+ /// Represents a optional value
+ ///
+ /// Type of value
internal readonly struct Optional
{
private Optional(T value, bool hasValue = true)
@@ -16,21 +22,98 @@ private Optional(T value, bool hasValue = true)
Value = value;
}
+ ///
+ /// Get if the existing value
+ ///
public bool HasValue { get; }
+ ///
+ /// Get Value
+ ///
public T Value { get; }
+ ///
+ /// Sets the value to non-optional
+ ///
+ ///
+ ///
public static Optional Set(T value)
{
return new Optional(value, true);
}
+ ///
+ /// Sets the empty value to optional
+ ///
+ ///
public static Optional Empty()
{
return new(default, false);
}
+ ///
+ /// Implicit cast to
+ ///
+ /// The value to cast
+
public static implicit operator T(Optional optional) => optional.Value;
+ ///
+ /// Compare if it is equal to
+ ///
+ /// Left operand
+ /// Rith operand
+ ///
+ public static bool operator ==(T left, Optional right)
+ {
+ return left!.Equals(right.Value);
+ }
+
+ ///
+ /// Compare if it is not equal to
+ ///
+ /// Left operand
+ /// Rith operand
+ ///
+ public static bool operator !=(T left, Optional right)
+ {
+ return !(left == right);
+ }
+
+ ///
+ /// Compare with a object
+ ///
+ /// The object
+ ///
+ public override bool Equals(object? obj)
+ {
+ if (obj is Optional item)
+ {
+ if (Value == null || !item.HasValue)
+ {
+ return obj == null;
+ }
+ return Value.Equals(item.Value);
+ }
+ else if (obj is T itemT)
+ {
+ if (Value == null)
+ {
+ return obj == null;
+ }
+ return Value.Equals(itemT);
+ }
+ return false;
+ }
+
+ ///
+ /// Get the HashCode
+ ///
+ ///
+ public override int GetHashCode()
+ {
+ return HashCode.Combine(Value, HasValue);
+ }
}
+
}