Files
spark/src/DataStreamSuite/PubSub/Client/Routing/PubSubRoute.cs
T
John Colvin 5fa284cad3 refactor: Use DependencyInjection paradigm in DataStreamSuite
This allows us to create loosely coupled services with automatic
dependency resolution
2024-07-07 18:13:59 -04:00

68 lines
1.8 KiB
C#

using bts;
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<PubSubRoute>, IComparable<PubSubRoute>, ICloneable<PubSubRoute> {
[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<PubSubRoute> comparer = new ComparerBuilder<PubSubRoute>() {
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 PubSubRoute Clone() =>
new() {
ServerAddress = ServerAddress,
TagRegExp = TagRegExp,
Name = Name,
};
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);
}