Files
spark/src/DataStreamSuite/PubSub/Client/Routing/PubSubRoute.cs
T

62 lines
1.7 KiB
C#

using mbts.utils;
using Spark.Core.Json;
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> {
[JsonConverter(typeof(JsonValueOrArrayConverter<string>))]
[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 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);
}