Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e40426a
commit 7a2e18d
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace ModuleManager.Collections | ||
{ | ||
public interface IMessageQueue<T> | ||
{ | ||
void Add(T value); | ||
} | ||
|
||
public class MessageQueue<T> : IMessageQueue<T>, IEnumerable<T> | ||
{ | ||
public class Enumerator : IEnumerator<T> | ||
{ | ||
private readonly MessageQueue<T> queue; | ||
private Node current; | ||
|
||
public Enumerator(MessageQueue<T> queue) | ||
{ | ||
this.queue = queue; | ||
} | ||
|
||
public T Current => current.value; | ||
object IEnumerator.Current => Current; | ||
|
||
public void Dispose() { } | ||
|
||
public bool MoveNext() | ||
{ | ||
if (current == null) | ||
current = queue.head; | ||
else | ||
current = current.next; | ||
|
||
return current != null; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
current = null; | ||
} | ||
} | ||
|
||
private class Node | ||
{ | ||
public Node next; | ||
public readonly T value; | ||
|
||
public Node(T value) | ||
{ | ||
this.value = value; | ||
} | ||
} | ||
|
||
private readonly object lockObject = new object(); | ||
private Node head; | ||
private Node tail; | ||
|
||
public void Add(T value) | ||
{ | ||
Node node = new Node(value); | ||
lock (lockObject) | ||
{ | ||
if (head == null) | ||
{ | ||
head = node; | ||
tail = node; | ||
} | ||
else | ||
{ | ||
tail.next = node; | ||
tail = node; | ||
} | ||
} | ||
} | ||
|
||
public MessageQueue<T> TakeAll() | ||
{ | ||
MessageQueue<T> queue = new MessageQueue<T>(); | ||
lock(lockObject) | ||
{ | ||
queue.head = head; | ||
queue.tail = tail; | ||
head = null; | ||
tail = null; | ||
} | ||
return queue; | ||
} | ||
|
||
public Enumerator GetEnumerator() => new Enumerator(this); | ||
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using Xunit; | ||
using ModuleManager.Collections; | ||
|
||
namespace ModuleManagerTests.Collections | ||
{ | ||
public class MessageQueueTest | ||
{ | ||
private class TestClass { } | ||
|
||
private MessageQueue<TestClass> queue = new MessageQueue<TestClass>(); | ||
|
||
[Fact] | ||
public void Test__Empty() | ||
{ | ||
Assert.Empty(queue); | ||
} | ||
|
||
[Fact] | ||
public void TestAdd() | ||
{ | ||
TestClass o1 = new TestClass(); | ||
TestClass o2 = new TestClass(); | ||
TestClass o3 = new TestClass(); | ||
|
||
queue.Add(o1); | ||
queue.Add(o2); | ||
queue.Add(o3); | ||
|
||
Assert.Equal(new[] { o1, o2, o3 }, queue); | ||
} | ||
|
||
[Fact] | ||
public void TestTakeAll() | ||
{ | ||
TestClass o1 = new TestClass(); | ||
TestClass o2 = new TestClass(); | ||
TestClass o3 = new TestClass(); | ||
TestClass o4 = new TestClass(); | ||
|
||
queue.Add(o1); | ||
queue.Add(o2); | ||
queue.Add(o3); | ||
|
||
MessageQueue<TestClass> queue2 = queue.TakeAll(); | ||
|
||
queue.Add(o4); | ||
|
||
Assert.Equal(new[] { o4 }, queue); | ||
Assert.Equal(new[] { o1, o2, o3 }, queue2); | ||
} | ||
} | ||
} |
This file contains 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