-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a bounded-size version of the in-memory message queue
- Loading branch information
Philip Whitehouse
committed
Jul 27, 2023
1 parent
dce91e6
commit 37b4d9c
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
quickfixj-core/src/main/java/quickfix/BoundInMemoryMessageQueue.java
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,45 @@ | ||
package quickfix; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A bounded-size version of InMemoryMessageQueue. | ||
* @see InMemoryMessageQueue | ||
*/ | ||
public class BoundInMemoryMessageQueue implements MessageQueue { | ||
// The map should be accessed from a single thread | ||
private final Map<Integer, Message> backingMap = new LinkedHashMap<>(); | ||
private final int maxSize; | ||
|
||
public BoundInMemoryMessageQueue(int maxSize) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
public void enqueue(int sequence, Message message) { | ||
if (backingMap.size() >= maxSize) { | ||
List<Integer> keys = backingMap.keySet().stream().sorted().collect(Collectors.toList()); | ||
if (sequence < keys.get(0)) { | ||
backingMap.remove(keys.get(keys.size()-1)); | ||
backingMap.put(sequence, message); | ||
} | ||
} else { | ||
this.backingMap.put(sequence, message); | ||
} | ||
} | ||
|
||
public Message dequeue(int sequence) { | ||
return (Message) this.backingMap.remove(sequence); | ||
} | ||
|
||
public void clear() { | ||
this.backingMap.clear(); | ||
} | ||
|
||
Map<Integer, Message> getBackingMap() { | ||
return this.backingMap; | ||
} | ||
} | ||
|