|
| 1 | +using StackExchange.Redis; |
| 2 | + |
| 3 | +namespace RESPite.StackExchange.Redis; |
| 4 | + |
| 5 | +public readonly struct BoundedRedisValue : IEquatable<BoundedRedisValue> |
| 6 | +{ |
| 7 | + internal readonly RedisValue ValueRaw; |
| 8 | + public RedisValue Value => ValueRaw; |
| 9 | + private readonly BoundType _type; |
| 10 | + internal BoundType Type => _type; |
| 11 | + |
| 12 | + public BoundedRedisValue(RedisValue value, bool exclusive = false) |
| 13 | + { |
| 14 | + ValueRaw = value; |
| 15 | + _type = exclusive ? BoundType.Exclusive : BoundType.Inclusive; |
| 16 | + } |
| 17 | + |
| 18 | + private BoundedRedisValue(BoundType type) |
| 19 | + { |
| 20 | + _type = type; |
| 21 | + ValueRaw = RedisValue.Null; |
| 22 | + } |
| 23 | + |
| 24 | + internal enum BoundType : byte |
| 25 | + { |
| 26 | + Inclusive, |
| 27 | + Exclusive, |
| 28 | + MinValue, |
| 29 | + MaxValue, |
| 30 | + } |
| 31 | + public bool Inclusive => _type == BoundType.Inclusive; |
| 32 | + |
| 33 | + public override string ToString() => _type switch |
| 34 | + { |
| 35 | + BoundType.Inclusive => $"[{ValueRaw}", |
| 36 | + BoundType.Exclusive => $"({ValueRaw}", |
| 37 | + BoundType.MinValue => "-", |
| 38 | + BoundType.MaxValue => "+", |
| 39 | + _ => _type.ToString(), |
| 40 | + }; |
| 41 | + |
| 42 | + public override int GetHashCode() => unchecked((Value.GetHashCode() * 397) ^ _type.GetHashCode()); |
| 43 | + |
| 44 | + public override bool Equals(object? obj) => obj is BoundedRedisValue other && Equals(other); |
| 45 | + bool IEquatable<BoundedRedisValue>.Equals(BoundedRedisValue other) => Equals(other); |
| 46 | + public bool Equals(in BoundedRedisValue other) => Value.Equals(other.Value) && Inclusive == other.Inclusive; |
| 47 | + public static bool operator ==(BoundedRedisValue left, BoundedRedisValue right) => left.Equals(right); |
| 48 | + public static bool operator !=(BoundedRedisValue left, BoundedRedisValue right) => !left.Equals(right); |
| 49 | + public static implicit operator BoundedRedisValue(RedisValue value) => new(value); |
| 50 | + |
| 51 | + public static BoundedRedisValue MinValue => new(BoundType.MinValue); |
| 52 | + public static BoundedRedisValue MaxValue => new(BoundType.MaxValue); |
| 53 | +} |
0 commit comments