| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- namespace Quadarax.Foundation.Core.QConsole.Value
- {
- public abstract class AbstractValue
- {
- private object? _value;
- /// <summary>
- /// Defines if value is set. Not null.
- /// </summary>
- public bool HasValue => _value != null;
- /// <summary>
- /// Returns underlying system value type.
- /// </summary>
- public Type ValueType { get; }
- public AbstractValue()
- {
- ValueType = typeof(object);
- Reset();
- }
- public AbstractValue(Type valueType, object value) : this(valueType)
- {
- _value = value;
- }
- protected AbstractValue(Type valueType)
- {
- ValueType = valueType ?? throw new ArgumentNullException(nameof(valueType));
- }
- protected abstract object ConvertStringToValue(string valueAsString);
- public abstract string AsString();
- public void Set(string valueAsString)
- {
- if (string.IsNullOrEmpty(valueAsString))
- {
- Reset();
- return;
- }
- Set(ConvertStringToValue(valueAsString));
- }
- public virtual void Set(object? value)
- {
- _value = value;
- }
- public object? Get()
- {
- return _value;
- }
- public void Reset()
- {
- _value = null;
- }
- public static AbstractValue Create(TypeValuesEnum typeValue)
- {
- switch (typeValue)
- {
- case TypeValuesEnum.Bool:
- return new BoolValue();
- case TypeValuesEnum.Date:
- return new DateValue();
- case TypeValuesEnum.DateTime:
- return new DateTimeValue();
- case TypeValuesEnum.Decimal:
- return new DecimalValue();
- case TypeValuesEnum.Guid:
- return new GuidValue();
- case TypeValuesEnum.Integer:
- return new IntegerValue();
- case TypeValuesEnum.String:
- return new StringValue();
- case TypeValuesEnum.Time:
- return new TimeValue();
- case TypeValuesEnum.TimeSpan:
- return new TimeSpanValue();
- default:
- throw new NotSupportedException($"Unsupported argument value '{typeValue}'");
- }
- }
- public override string ToString()
- {
- return AsString();
- }
- }
- }
|