1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.emarket.trade.domain;
21
22 import java.util.List;
23 import java.util.PriorityQueue;
24
25 public class PriceEntry {
26
27 private float price;
28 private PriorityQueue<ReceivedOrder> buyList;
29 private PriorityQueue<ReceivedOrder> sellList;
30
31 public PriceEntry( float p ){
32 price = p;
33 buyList = new PriorityQueue<ReceivedOrder>(100, OrderBook.timeComparator);
34 sellList = new PriorityQueue<ReceivedOrder>(100, OrderBook.timeComparator);
35 }
36
37 void add( ReceivedOrder order) {
38
39 if( order.isBuy() ){
40 buyList.offer( order);
41 }else if( order.isSell() ){
42 sellList.offer( order);
43 }
44 }
45
46 public float getPrice(){
47 return price;
48 }
49
50 public boolean hasBuyOrders(){
51 boolean result = false;
52
53 if( buyList.size() > 0 ){
54 result = true;
55 }
56 return result;
57 }
58
59 public int getTotalBuyQuantity(){
60 int totalQuantity =0;
61 for( ReceivedOrder order : buyList ){
62 totalQuantity += order.getQuantity();
63 }
64 return totalQuantity;
65 }
66
67 public int getTotalBuyOrders(){
68 return buyList.size();
69 }
70
71 public int getBuyOrdersForQuantity( int quantity, List<ReceivedOrder> matchingOrders){
72
73 int remainingQuantity = quantity;
74 int matchedQuantity =0;
75
76 for(ReceivedOrder order : buyList ){
77
78 if( matchedQuantity < quantity){
79 if( remainingQuantity > order.getQuantity() ){
80 matchingOrders.add(order);
81 }
82 if( remainingQuantity == order.getQuantity() ){
83 matchingOrders.add(order);
84 }if( remainingQuantity < order.getQuantity() ){
85 matchingOrders.add(order);
86 }
87
88 matchedQuantity = matchedQuantity + order.getQuantity();
89 remainingQuantity = remainingQuantity - order.getQuantity();
90
91 if( matchedQuantity >= quantity ){
92 break;
93 }
94 }
95 }
96
97 return matchedQuantity;
98 }
99
100
101 public boolean hasSellOrders(){
102 boolean result = false;
103
104 if( sellList.size() > 0){
105 result = true;
106 }
107 return result;
108 }
109
110 public int getTotalSellQuantity(){
111 int totalQuantity =0;
112 for( ReceivedOrder order : sellList ){
113 totalQuantity += order.getQuantity();
114 }
115 return totalQuantity;
116 }
117
118 public int getTotalSellOrders(){
119 return sellList.size();
120 }
121
122 public int getSellOrdersForQuantity( int quantity, List<ReceivedOrder> matchingOrders){
123
124 int remainingQuantity = quantity;
125 int matchedQuantity =0;
126
127 for(ReceivedOrder order : sellList ){
128
129 if( matchedQuantity < quantity){
130 if( remainingQuantity > order.getQuantity() ){
131 matchingOrders.add(order);
132 }
133 if( remainingQuantity == order.getQuantity() ){
134 matchingOrders.add(order);
135 }if( remainingQuantity < order.getQuantity() ){
136 matchingOrders.add(order);
137 }
138
139 matchedQuantity = matchedQuantity + order.getQuantity();
140 remainingQuantity = remainingQuantity - order.getQuantity();
141
142 if( matchedQuantity >= quantity ){
143 break;
144 }
145 }
146 }
147
148 return matchedQuantity;
149 }
150
151 public int getBuyQuantity(){
152 int quantity =0;
153 ReceivedOrder buy = buyList.peek();
154 if( buy != null ){
155 quantity = buy.getQuantity();
156 }
157 return quantity;
158 }
159
160 public int getSellQuantity(){
161 int quantity =0;
162 ReceivedOrder sell = sellList.peek();
163 if( sell != null ){
164 quantity = sell.getQuantity();
165 }
166 return quantity;
167 }
168 }