/// <summary> /// Gets a dynamic view of the underlying cache based on the parameters given. /// </summary> /// <param name="mode">View mode.</param> /// <param name="startKey">Start key of view.</param> /// <param name="endKey">End key of view.</param> /// <param name="tailCount">Number of items to include in view.</param> /// <param name="tailRange">Tail duration function. Takes last item's key and returns a new startKey.</param> /// <returns>An instance of <see cref="ObservableKeyedView"/>.</returns> /// <exception cref="ArgumentException"><paramref name="startKey"/> must be less than or equal to <paramref name="endKey"/>.</exception> public ObservableKeyedView GetView(ObservableKeyedView.ViewMode mode, TKey startKey, TKey endKey, uint tailCount, Func <TKey, TKey> tailRange) { if (this.keyComparer.Compare(startKey, endKey) > 0) { throw new ArgumentException($"startKey ({startKey}) must be less than or equal to endKey ({endKey})."); } var viewKey = Tuple.Create(startKey, endKey, tailCount, tailRange); WeakReference <ObservableKeyedView> weakView = null; ObservableKeyedView view = null; if (this.views.TryGetValue(viewKey, out weakView)) { if (weakView.TryGetTarget(out view)) { return(view); } else { view = new ObservableKeyedView(this, mode, startKey, endKey, tailCount, tailRange); weakView.SetTarget(view); } } else { view = new ObservableKeyedView(this, mode, startKey, endKey, tailCount, tailRange); weakView = new WeakReference <ObservableKeyedView>(view); this.views.Add(viewKey, weakView); } return(view); }
/// <summary> /// Gets a dynamic view of the underlying cache based on the parameters given. /// </summary> /// <param name="mode">View mode.</param> /// <param name="startKey">Start key of view.</param> /// <param name="endKey">End key of view.</param> /// <param name="tailCount">Number of items to include in view.</param> /// <param name="tailRange">Tail duration function. Takes last item's key and returns a new startKey.</param> /// <returns>An instance of <see cref="ObservableKeyedView"/>.</returns> /// <exception cref="ArgumentException"><paramref name="startKey"/> must be less than or equal to <paramref name="endKey"/>.</exception> public ObservableKeyedView GetView(ObservableKeyedView.ViewMode mode, TKey startKey, TKey endKey, uint tailCount, Func <TKey, TKey> tailRange) { if (this.keyComparer.Compare(startKey, endKey) > 0) { throw new ArgumentException($"startKey ({startKey}) must be less than or equal to endKey ({endKey})."); } var viewKey = Tuple.Create(startKey, endKey, tailCount, tailRange); WeakReference <ObservableKeyedView> weakView = null; ObservableKeyedView view = null; if (this.views.TryGetValue(viewKey, out weakView)) { if (weakView.TryGetTarget(out view)) { return(view); } else { view = new ObservableKeyedView(this, mode, startKey, endKey, tailCount, tailRange); weakView.SetTarget(view); // Sometimes the weak view gets deleted between when we grab it to check // if it has a hard reference and when we actually set the new hard reference // that we just created. If that happens, then the following code makes sure // the weak view gets put back into the collection. this.views[viewKey] = weakView; } } else { view = new ObservableKeyedView(this, mode, startKey, endKey, tailCount, tailRange); weakView = new WeakReference <ObservableKeyedView>(view); this.views.Add(viewKey, weakView); } return(view); }