Last active
October 31, 2020 20:38
-
-
Save joao-r-reis/49d9f1794c5723b928d52c19996bdfd3 to your computer and use it in GitHub Desktop.
NetworkTopologyStrategy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class NetworkTopologyStrategy : IReplicationStrategy, IEquatable<NetworkTopologyStrategy> | |
{ | |
private readonly SortedSet<DatacenterReplicationFactor> _replicationFactorsSet; | |
private readonly IReadOnlyDictionary<string, int> _replicationFactorsMap; | |
private readonly int _hashCode; | |
public NetworkTopologyStrategy(IReadOnlyDictionary<string, int> replicationFactors) | |
{ | |
_replicationFactorsSet = new SortedSet<DatacenterReplicationFactor>( | |
replicationFactors.Select(rf => new DatacenterReplicationFactor(rf.Key, rf.Value)), | |
DatacenterReplicationFactorComparer.Instance); | |
_replicationFactorsMap = replicationFactors; | |
_hashCode = NetworkTopologyStrategy.ComputeHashCode(_replicationFactorsSet); | |
} | |
public override bool Equals(object obj) | |
{ | |
return Equals(obj as NetworkTopologyStrategy); | |
} | |
public bool Equals(NetworkTopologyStrategy other) | |
{ | |
return other != null && _replicationFactorsSet.SetEquals(other._replicationFactorsSet); | |
} | |
private static int ComputeHashCode(IEnumerable<DatacenterReplicationFactor> replicationFactorsSet) | |
{ | |
return Utils.CombineHashCode(replicationFactorsSet); | |
} | |
public override int GetHashCode() | |
{ | |
return _hashCode; | |
} | |
} | |
internal struct DatacenterReplicationFactor : IEquatable<DatacenterReplicationFactor> | |
{ | |
private readonly int _hashCode; | |
public DatacenterReplicationFactor(string datacenter, int replicationFactor) | |
{ | |
Datacenter = datacenter ?? throw new ArgumentNullException(nameof(datacenter)); | |
ReplicationFactor = replicationFactor; | |
_hashCode = DatacenterReplicationFactor.ComputeHashCode(Datacenter, ReplicationFactor); | |
} | |
public string Datacenter { get; } | |
public int ReplicationFactor { get; } | |
public override bool Equals(object obj) | |
{ | |
return obj.GetType() == GetType() && Equals((DatacenterReplicationFactor)obj); | |
} | |
public bool Equals(DatacenterReplicationFactor other) | |
{ | |
return Datacenter == other.Datacenter && | |
ReplicationFactor == other.ReplicationFactor; | |
} | |
public override int GetHashCode() | |
{ | |
return _hashCode; | |
} | |
private static int ComputeHashCode(string datacenter, int replicationFactor) | |
{ | |
return Utils.CombineHashCode(new object[] { datacenter, replicationFactor }); | |
} | |
} | |
internal class DatacenterReplicationFactorComparer : IComparer<DatacenterReplicationFactor> | |
{ | |
private DatacenterReplicationFactorComparer() { } | |
public static DatacenterReplicationFactorComparer Instance { get; } = new DatacenterReplicationFactorComparer(); | |
public int Compare(DatacenterReplicationFactor x, DatacenterReplicationFactor y) | |
{ | |
return string.Compare(x.Datacenter, y.Datacenter, StringComparison.Ordinal); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment