CollectionNavigator.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #nullable disable
  2. using System.Collections;
  3. namespace Terminal.Gui.Views;
  4. /// <inheritdoc cref="CollectionNavigatorBase"/>
  5. /// <remarks>This implementation is based on a static <see cref="Collection"/> of objects.</remarks>
  6. internal class CollectionNavigator : CollectionNavigatorBase, IListCollectionNavigator
  7. {
  8. private readonly object _collectionLock = new ();
  9. private IList _collection;
  10. /// <summary>Constructs a new CollectionNavigator.</summary>
  11. public CollectionNavigator () { }
  12. /// <summary>Constructs a new CollectionNavigator for the given collection.</summary>
  13. /// <param name="collection"></param>
  14. public CollectionNavigator (IList collection) { Collection = collection; }
  15. /// <inheritdoc/>
  16. public IList Collection
  17. {
  18. get
  19. {
  20. lock (_collectionLock)
  21. {
  22. return _collection;
  23. }
  24. }
  25. set
  26. {
  27. lock (_collectionLock)
  28. {
  29. _collection = value;
  30. }
  31. }
  32. }
  33. /// <inheritdoc/>
  34. protected override object ElementAt (int idx)
  35. {
  36. lock (_collectionLock)
  37. {
  38. return Collection [idx];
  39. }
  40. }
  41. /// <inheritdoc/>
  42. protected override int GetCollectionLength ()
  43. {
  44. lock (_collectionLock)
  45. {
  46. return Collection.Count;
  47. }
  48. }
  49. }