1 module hunt.proton.engine.impl.LinkNode;
2 /*
3  * hunt-proton: AMQP Protocol library for D programming language.
4  *
5  * Copyright (C) 2018-2019 HuntLabs
6  *
7  * Website: https://www.huntlabs.net/
8  *
9  * Licensed under the Apache-2.0 License.
10  *
11  */
12 
13 
14 interface Query(T)
15 {
16     public bool matches(LinkNode!T node);
17 }
18 
19 class LinkNode(E)
20 {
21     private E _value;
22     private LinkNode!E _prev;
23     private LinkNode!E _next;
24 
25     this(E value)
26     {
27         _value = value;
28     }
29 
30     public E getValue()
31     {
32         return _value;
33     }
34 
35     public LinkNode!E getPrev()
36     {
37         return _prev;
38     }
39 
40     public LinkNode!E getNext()
41     {
42         return _next;
43     }
44 
45     public LinkNode!E next(Query!E query)
46     {
47         LinkNode!E next = _next;
48         while(next !is null && !query.matches(next))
49         {
50             next = next.getNext();
51         }
52         return next;
53     }
54 
55     public LinkNode!E remove()
56     {
57         LinkNode!E prev = _prev;
58         LinkNode!E next = _next;
59         if(prev !is null)
60         {
61             prev._next = next;
62         }
63         if(next !is null)
64         {
65             next._prev = prev;
66         }
67         _next = _prev = null;
68         return next;
69     }
70 
71     public LinkNode!E addAtTail(E value)
72     {
73         if(_next is null)
74         {
75             _next = new LinkNode!E(value);
76             _next._prev = this;
77             return _next;
78         }
79         else
80         {
81             return _next.addAtTail(value);
82         }
83     }
84 
85     public static LinkNode!T newList(T)(T value)
86     {
87         return new LinkNode!T (value);
88     }
89 
90 }