KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > util > TranDeck


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 //
35
//
36
// History:
37
// 10/08/2001 Shiping Initial coding based on the existing code
38
//
39
//
40
//
41

42 package stockonline.util;
43
44 import java.util.*;
45
46 public class TranDeck
47 {
48     public static final int TRAN_SIZE = 43; // Total number of transactions
49

50     public static final int TRAN_BUY = 1;
51     public static final int TRAN_SELL = 2;
52     public static final int TRAN_QUERY_ID = 3;
53     public static final int TRAN_QUERY_CODE = 4;
54     public static final int TRAN_GETHOLDING = 5;
55     public static final int TRAN_CREATE = 6;
56     public static final int TRAN_UPDATE = 7;
57
58     int maxAccountNo; // Maximum account number that will be randomly created
59
int deckOrder[];
60     int deckCards[][];
61     Stack buyStack;
62     Stack sellStack;
63     int index;
64
65     Random rand; // Random generator
66

67     public TranDeck()
68     {
69         // The initial database has at least 3000 subscribers
70
this(3000);
71     }
72
73     public TranDeck(int maxAccountNo)
74     {
75         this.maxAccountNo = maxAccountNo;
76         deckOrder = new int[TRAN_SIZE];
77         deckCards = new int[TRAN_SIZE][2];
78         buyStack = new Stack();
79         sellStack = new Stack();
80
81         // Initialize the deck
82
for (int i = 0; i < TRAN_SIZE - 2; i++) {
83             if (0 <= i && i <= 2) {
84                 deckOrder[i] = TRAN_BUY;
85                 continue;
86             }
87
88             if (3 <= i && i <= 5) {
89                 deckOrder[i] = TRAN_SELL;
90                 continue;
91             }
92
93             if (6 <= i && i <= 20) {
94                 deckOrder[i] = TRAN_QUERY_ID;
95                 continue;
96             }
97
98             if (21 <= i && i <= 35) {
99                 deckOrder[i] = TRAN_QUERY_CODE;
100                 continue;
101             }
102
103             if (36 <= i && i <= 40) {
104                 deckOrder[i] = TRAN_GETHOLDING;
105                 continue;
106             }
107         }
108
109         deckOrder[41] = TRAN_CREATE;
110         deckOrder[42] = TRAN_UPDATE;
111
112         rand = new Random(getCurrentThreadNum());
113     }
114
115     public void setRandomGen(Random rand)
116     {
117         this.rand = rand;
118     }
119
120     public TranInfo getNext()
121     {
122         TranInfo tranInfo = new TranInfo();
123
124         tranInfo.tranType = deckOrder[index];
125         tranInfo.accountNo = deckCards[index][0];
126         tranInfo.stockID = deckCards[index][1];
127         index++;
128
129         return tranInfo;
130     }
131
132     public void shuffle()
133     {
134         // Shuffle deck
135
random_shuffle(deckOrder);
136
137         // Populate the deck
138
for (int i = 0; i < TRAN_SIZE; i++) {
139             deckCards[i][0] = Math.abs(rand.nextInt() % maxAccountNo) + 1; // subaccno
140
deckCards[i][1] = Math.abs(rand.nextInt() % 3000) + 1; // stockid
141
}
142
143         // Scan the deck and match buys and sells
144
for (int i = 0; i < TRAN_SIZE; i++) {
145             if (deckOrder[i] == TRAN_BUY) {
146                 if (!sellStack.empty()) {
147                     int pos = ((Integer JavaDoc)sellStack.pop()).intValue();
148                     deckOrder[i] = TRAN_SELL;
149                     deckOrder[pos] = TRAN_BUY;
150
151                     deckCards[pos][0] = deckCards[i][0];
152                     deckCards[pos][1] = deckCards[i][1];
153                 } else {
154                     buyStack.push(new Integer JavaDoc(i));
155                 }
156             } else if (deckOrder[i] == TRAN_SELL) {
157                 if (!buyStack.empty()) {
158                     int pos = ((Integer JavaDoc)buyStack.pop()).intValue();
159                     deckCards[i][0] = deckCards[pos][0];
160                     deckCards[i][1] = deckCards[pos][1];
161                 } else {
162                     sellStack.push(new Integer JavaDoc(i));
163                 }
164             }
165         }
166
167         index = 0;
168     }
169
170     // Get the current thread number.
171
// Note:
172
// (1) this method can only be used in a thread
173
// with an automatically generated name, that is,
174
// no thread name is specified in the contructors
175
// when it is created.
176
// (2) if the thread name is not auto-generated,
177
// this method returns zero, hopefully.
178
protected int getCurrentThreadNum()
179     {
180         // Automatically generated names are of the form "Thread-"+n,
181
// where n is an integer.
182
try {
183             return Integer.parseInt(Thread.currentThread().getName().substring(7));
184         }
185         catch (Exception JavaDoc e) {
186             return 0;
187         }
188     }
189
190     /**
191         This method evaluates
192             swap(*(first + N), *(first + M))
193         once for each N in the range [1, last - first),
194         where M is a value from some uniform random distribution
195         over the range [0, N).
196     */

197     protected void random_shuffle(int deck[])
198     {
199         for (int n = 1; n < deck.length; n++) {
200             int m = Math.abs(rand.nextInt() % n);
201             // Swap
202
int temp = deck[n];
203             deck[n] = deck[m];
204             deck[m] = temp;
205         }
206     }
207 }
208
Popular Tags