|
| 1 | +#region Copyright © 2019 Couchcoding |
| 2 | + |
| 3 | +// File: FixedSizedQueue.cs |
| 4 | +// Package: Logbert |
| 5 | +// Project: Logbert |
| 6 | +// |
| 7 | +// The MIT License (MIT) |
| 8 | +// |
| 9 | +// Copyright (c) 2019 Couchcoding |
| 10 | +// |
| 11 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +// of this software and associated documentation files (the "Software"), to deal |
| 13 | +// in the Software without restriction, including without limitation the rights |
| 14 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +// copies of the Software, and to permit persons to whom the Software is |
| 16 | +// furnished to do so, subject to the following conditions: |
| 17 | +// |
| 18 | +// The above copyright notice and this permission notice shall be included in |
| 19 | +// all copies or substantial portions of the Software. |
| 20 | +// |
| 21 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | +// THE SOFTWARE. |
| 28 | + |
| 29 | +#endregion |
| 30 | + |
| 31 | +using System.Collections.Concurrent; |
| 32 | + |
| 33 | +namespace Couchcoding.Logbert.Helper |
| 34 | +{ |
| 35 | + /// <summary> |
| 36 | + /// Implements a <see cref="ConcurrentQueue{T}"/> of a fixed size. |
| 37 | + /// </summary> |
| 38 | + internal class FixedSizedQueue<T> : ConcurrentQueue<T> |
| 39 | + { |
| 40 | + #region Private Fields |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Simple object for thread synchronization. |
| 44 | + /// </summary> |
| 45 | + private readonly object mSyncObject = new object(); |
| 46 | + |
| 47 | + #endregion |
| 48 | + |
| 49 | + #region Public Properties |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the fixed size of the <see cref="FixedSizedQueue{T}"/>. |
| 53 | + /// </summary> |
| 54 | + public int Size |
| 55 | + { |
| 56 | + get; |
| 57 | + } |
| 58 | + |
| 59 | + #endregion |
| 60 | + |
| 61 | + #region Public Methods |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Adds an object at the end of the <see cref="FixedSizedQueue{T}"/>. |
| 65 | + /// </summary> |
| 66 | + /// <param name="obj">The object to add at the end of the <see cref="FixedSizedQueue{T}"/></param> |
| 67 | + public new void Enqueue(T obj) |
| 68 | + { |
| 69 | + // Add the object to the queue. |
| 70 | + base.Enqueue(obj); |
| 71 | + |
| 72 | + lock (mSyncObject) |
| 73 | + { |
| 74 | + while (base.Count > Size) |
| 75 | + { |
| 76 | + // Ensure we don't exceed the maximum size. |
| 77 | + TryDequeue(out T outObj); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + #endregion |
| 83 | + |
| 84 | + #region Constructor |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Creates a new instance of the <see cref="FixedSizedQueue{T}"/> with the specified <paramref name="size"/>. |
| 88 | + /// </summary> |
| 89 | + /// <param name="size">The maximum size of the <see cref="FixedSizedQueue{T}"/>.</param> |
| 90 | + public FixedSizedQueue(int size) |
| 91 | + { |
| 92 | + Size = size; |
| 93 | + } |
| 94 | + |
| 95 | + #endregion |
| 96 | + } |
| 97 | +} |
0 commit comments