/
Consistent Hashing - class HashRingLocation<T>

Consistent Hashing - class HashRingLocation<T>

Storage node for Items of type T

public class HashRingLocation<T>: HashRingNode<T>, INotifyPropertyChanged { //Fields private SortedDictionary<UInt32, HashRingNode<T>> _nodeDictionary; //Properties public int DuplicateCount { get; set; } public SortedDictionary<UInt32, HashRingNode<T>> NodeDictionary { get{ _nodeDictionary = _nodeDictionary ?? new SortedDictionary<uint, HashRingNode<T>>(); return _nodeDictionary; } } public List<HashRingNode<T>> Nodes { get{return NodeDictionary.Values.ToList();} } //Constructor public HashRingLocation(UInt32 key, T item) { Key = key; Item = item; } // Public Item Accessor and Iterator public int ItemCount { get { return NodeDictionary.Count + DuplicateCount; } } public void ShowIt() { RaisePropertyChanged(() => Nodes); } }
//Protected INotifyPropertyChanged Implementation // Property changed event public event PropertyChangedEventHandler PropertyChanged; //Set the property and notifies any listeners that it changed (if it did) protected void SetProperty<T>(ref T field, T value, Expression<Func<T>> memberExpression, params Expression<Func<object>>[] moreNotifications) { // Must have member expression to find property name if (memberExpression == null){throw new ArgumentNullException();} var bodyExpr = memberExpression.Body as MemberExpression; // Member expression must have a body (a property) if (bodyExpr == null){ throw new ArgumentNullException();} // don't do anything unless the value changes if (EqualityComparer<T>.Default.Equals(field, value)){ return; } field = value; RaisePropertyChanged(memberExpression, moreNotifications); } //Raise property changed by names only (value needs to be reread) protected void RaisePropertyChanged<T>(Expression<Func<T>> memberExpression, params Expression<Func<object>>[] moreNotifications) { // Must have member expression to find property name if (memberExpression == null){throw new ArgumentNullException();} var bodyExpr = memberExpression.Body as MemberExpression; // Member expression must have a body (a property) if (bodyExpr == null){throw new ArgumentNullException(); } var handler = PropertyChanged; if (handler != null){ handler(this, new PropertyChangedEventArgs(bodyExpr.Member.Name)); foreach (Expression<Func<object>> notifyAlso in moreNotifications) { if (notifyAlso != null){ var alsoExpr = notifyAlso.Body as MemberExpression; handler(this, new PropertyChangedEventArgs(alsoExpr.Member.Name)); } } } }