Innovenergy_trunk/csharp/Lib/Units/State.cs

37 lines
1.6 KiB
C#
Raw Normal View History

2023-03-02 14:26:27 +00:00
using System.Collections;
namespace InnovEnergy.Lib.Units;
2023-03-02 14:26:27 +00:00
public readonly struct State : IReadOnlyList<String>
{
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;
}
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()){}
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);
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());
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];
}