1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.proton.engine.impl.CollectorImpl;
13 
14 import hunt.proton.engine.Collector;
15 import hunt.proton.engine.Event;
16 import hunt.proton.engine.EventType;
17 import hunt.proton.engine.impl.EventImpl;
18 import hunt.Exceptions;
19 
20 /**
21  * CollectorImpl
22  *
23  */
24 
25 class CollectorImpl : Collector
26 {
27 
28     private EventImpl head;
29     private EventImpl tail;
30     private EventImpl free;
31 
32     this()
33     {}
34 
35     
36     public Event peek()
37     {
38         return head;
39     }
40 
41     
42     public void pop()
43     {
44         if (head !is null) {
45             EventImpl next = head.next;
46             head.next = free;
47             free = head;
48             head.clear();
49             head = next;
50         }
51     }
52 
53     public EventImpl put(EventType type, Object context)
54     {
55         if (type is null) {
56             throw new IllegalArgumentException("Type cannot be null");
57         }
58         if (!type.isValid()) {
59             throw new IllegalArgumentException("Cannot put events of type ");
60         }
61         if (tail !is null && tail.getEventType() == type &&
62             tail.getContext() == context) {
63             return null;
64         }
65 
66         EventImpl event;
67         if (free is null) {
68             event = new EventImpl();
69         } else {
70             event = free;
71             free = free.next;
72             event.next = null;
73         }
74 
75         event.init(type, context);
76 
77         if (head is null) {
78             head = event;
79             tail = event;
80         } else {
81             tail.next = event;
82             tail = event;
83         }
84 
85         return event;
86     }
87 
88     
89     public bool more() {
90         return head !is null && head.next !is null;
91     }
92 
93 }