using mbts.utils; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace BCG; public class PubSubRoute : IEquatable, IComparable { [JsonPropertyName("server_address")] public string? ServerAddress { get; set; } [JsonPropertyName("tag")] public string? TagRegExp { get; set; } //route names are used in intermediary code (i.e. while substations are being deployed) //so that MC Hammer can advertise them. [JsonPropertyName("name")] public string? Name { get; set; } static readonly IComparer comparer = new ComparerBuilder() { p => p.ServerAddress, p => p.TagRegExp, }; Regex? myRegex; public PubSubRoute() { } public PubSubRoute(string theServerAddress, string theTagRegExp) { ServerAddress = theServerAddress; TagRegExp = theTagRegExp; } [MemberNotNull(nameof(TagRegExp), nameof(myRegex))] public bool IsMatch(string tag) { if (TagRegExp is null) { throw new InvalidOperationException(); } myRegex ??= new Regex(TagRegExp); return myRegex.IsMatch(tag); } public int CompareTo(PubSubRoute? other) => other is null ? -1 : comparer.Compare(this, other); public bool Equals(PubSubRoute? other) => other is not null && ServerAddress == other.ServerAddress && TagRegExp == other.TagRegExp; public override bool Equals(object? obj) => Equals(obj as PubSubRoute); public override int GetHashCode() => HashCode.Combine(ServerAddress, TagRegExp); }