2023-03-02 14:26:27 +00:00
|
|
|
using System.Collections;
|
|
|
|
|
2023-02-26 09:38:28 +00:00
|
|
|
namespace InnovEnergy.Lib.Units;
|
2023-02-25 14:53:58 +00:00
|
|
|
|
2023-03-02 14:26:27 +00:00
|
|
|
public readonly struct State : IReadOnlyList<String>
|
2023-02-25 14:53:58 +00:00
|
|
|
{
|
|
|
|
public IReadOnlyList<String> Values { get; }
|
|
|
|
|
2023-03-01 07:25:23 +00:00
|
|
|
public State(IReadOnlyList<String> values)
|
|
|
|
{
|
|
|
|
if (values.Any(v => v.Contains(";")))
|
|
|
|
throw new ArgumentException("State values cannot contain the character ;", nameof(values));
|
|
|
|
|
|
|
|
Values = values;
|
|
|
|
}
|
2023-02-25 14:53:58 +00:00
|
|
|
|
|
|
|
public State(params String[] values) : this((IReadOnlyList<String>)values){}
|
2023-03-01 09:38:46 +00:00
|
|
|
public State(params State[] states) : this((IReadOnlyList<String>)states.SelectMany(s => s.Values).ToList()){}
|
2023-02-25 14:53:58 +00:00
|
|
|
|
2023-03-02 14:14:22 +00:00
|
|
|
public static implicit operator State(String s) => new(s);
|
|
|
|
public static implicit operator State(Enum e) => new(e.ToString());
|
|
|
|
public static implicit operator State(Boolean s) => new(s.ToString());
|
|
|
|
public static implicit operator State(List<String> s) => new((IReadOnlyList<String>)s);
|
|
|
|
public static implicit operator State(String[] s) => new((IReadOnlyList<String>)s);
|
2023-03-02 16:15:10 +00:00
|
|
|
public static implicit operator State(List<Enum> es) => new(es.Select(e => e.ToString()).ToList());
|
|
|
|
public static implicit operator State(Enum[] es) => new(es.Select(e => e.ToString()).ToList());
|
2023-02-25 14:53:58 +00:00
|
|
|
|
2023-03-02 14:14:22 +00:00
|
|
|
public static State operator |(State left, State right) => new(left, right);
|
2023-03-01 07:25:23 +00:00
|
|
|
|
2023-03-02 14:26:27 +00:00
|
|
|
public IEnumerator<String> GetEnumerator() => Values.GetEnumerator();
|
|
|
|
|
2023-03-01 07:25:23 +00:00
|
|
|
public override String ToString() => String.Join("; ", Values);
|
2023-03-02 14:26:27 +00:00
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
|
|
|
|
public Int32 Count => Values.Count;
|
|
|
|
public String this[Int32 index] => Values[index];
|
2023-02-25 14:53:58 +00:00
|
|
|
}
|