View Javadoc

1   /*
2    *  
3    *  author nambi sankaran
4    *  copyright (C) 2009 nambi sankaran.
5    *
6    *  This program is free software: you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation, either version 3 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 }