/Users/lyon/j4p/src/net/stocks/Quote.java

1    package net.stocks; 
2     
3    import utils.StringUtils; 
4     
5    import java.beans.XMLEncoder; 
6    import java.io.BufferedReader; 
7    import java.io.ByteArrayOutputStream; 
8    import java.io.IOException; 
9    import java.io.InputStream; 
10   import java.io.InputStreamReader; 
11   import java.net.URL; 
12   import java.util.Calendar; 
13   import java.util.Date; 
14   import java.util.StringTokenizer; 
15   import java.util.Vector; 
16    
17   public class Quote { 
18       private String symbol; 
19       private float price; 
20       private Date date; 
21       private float change; 
22       private float open; 
23       private float bid; 
24       private float ask; 
25       private long volume; 
26    
27       public Quote(String s) { 
28           // assume that the quote is of the form 
29           // symbol   price   date    time  (20 min. Delay)     change      open    bid     ask     volume 
30           // "AAPL",55.31,"11/3/2004","4:00pm",+1.81,54.44,56.11,53.99,21504764 
31           StringTokenizer st = new StringTokenizer(s, ","); 
32           symbol = getSymbol(st); 
33           price = getPrice(st); 
34           date = getDate(getSymbol(st), getSymbol(st)); 
35           change = getPrice(st); 
36           open = getPrice(st); 
37           bid = getPrice(st); 
38           ask = getPrice(st); 
39           volume = getLong(st); 
40       } 
41    
42       private static String getSymbol(StringTokenizer st) { 
43           return trimQuotes(st.nextToken()); 
44       } 
45    
46       private float getPrice(StringTokenizer st) { 
47           final String s = getSymbol(st); 
48           if (s.indexOf("N/A") == -1) 
49               return Float.valueOf(s).floatValue(); 
50           return Float.NaN; 
51       } 
52    
53       private static long getLong(StringTokenizer st) { 
54           final String s = getSymbol(st); 
55           return Long.valueOf(s).longValue(); 
56       } 
57    
58       private static int getInt(StringTokenizer st) { 
59           final String s = getSymbol(st); 
60           return Integer.valueOf(s).intValue(); 
61       } 
62    
63       private static int getInt(String s) { 
64           return Integer.valueOf(s).intValue(); 
65       } 
66    
67       /* 
68       return a string with no quotes 
69       */ 
70       private static String trimQuotes(String s) { 
71           return StringUtils.replaceAll(s, "\"", ""); 
72       } 
73    
74       /* 
75       Parse two strings of the form: 
76       "11/3/2004","4:00pm" 
77       */ 
78       private static Date getDate(String dateString, String time) { 
79           StringTokenizer st = new StringTokenizer(dateString, "/"); 
80           int month = getInt(getSymbol(st).substring(1)); 
81           int date = getInt(st); 
82           int year = getInt(getSymbol(st).substring(0, 4)); 
83           st = new StringTokenizer(time, ":"); 
84           int hour = getInt(st); 
85           String minuteString = st.nextToken(""); 
86           boolean isPM = minuteString.indexOf("AM") == -1; 
87           if (isPM) hour = hour + 12; //use a 24 hour clock 
88           int minute = getMin(minuteString); 
89           Calendar c = Calendar.getInstance(); 
90           // the month is zero based 
91           // i.e., Jan = 0. 
92           c.set(year, month - 1, date, hour, minute); 
93           return c.getTime(); 
94       } 
95    
96       private static int getMin(String minuteString) { 
97           String ms = StringUtils.replaceAll(minuteString.toUpperCase(), ":", ""); 
98           ms = StringUtils.replaceAll(ms, "PM", ""); 
99           ms = StringUtils.replaceAll(ms, "AM", ""); 
100          ms = StringUtils.replaceAll(ms, "\"", ""); 
101          return Integer.valueOf("00").intValue(); 
102      } 
103   
104      public static void main(String args[]) { 
105          testQuoteSystem(); 
106      } 
107   
108      public static void print(Object o[]) { 
109          for (int i = 0; i < o.length; i++) 
110              System.out.println(o[i]); 
111      } 
112   
113      public String toXml() { 
114          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
115          XMLEncoder e = new XMLEncoder(baos); 
116          e.writeObject(this); 
117          e.flush(); 
118          return baos.toString(); 
119      } 
120   
121      public String toString() { 
122          return symbol + " " + date + " " + price + " " + volume; 
123      } 
124   
125      private static void testQuoteSystem() { 
126          Vector s = new Vector(); 
127          s.addElement("aapl"); 
128          s.addElement("xlnx"); 
129          s.addElement("altr"); 
130          s.addElement("mot"); 
131          s.addElement("cy"); 
132          s.addElement("crus"); 
133          s.addElement("sfa"); 
134          s.addElement("adbe"); 
135          s.addElement("msft"); 
136          s.addElement("sunw"); 
137          String urlString = makeQuoteURLString(s); 
138          //System.out.println(urlString); 
139          Vector url = getUrl(urlString); 
140          //printVector(url); 
141          print(getQuotes(url)); 
142      } 
143   
144      public static Quote makeQuote(String symbol) { 
145          Vector s = new Vector(); 
146          s.addElement(symbol); 
147          String urlString = makeQuoteURLString(s); 
148          Vector url = getUrl(urlString); 
149          Quote[] quotes = getQuotes(url); 
150          for (int i = 0; i < quotes.length; i++) 
151              if (quotes[i] != null) return quotes[i]; 
152          return null; 
153      } 
154   
155      public static Quote[] getQuotes(Vector v) { 
156          Quote qa [] = new Quote[v.size()]; 
157          for (int i = 0; i < qa.length; i++) { 
158              String s = v.elementAt(i).toString(); 
159              if (s.indexOf(",") == -1) continue; 
160              qa[i] = new Quote(s); 
161          } 
162          return qa; 
163      } 
164   
165      public static void printVector(Vector v) { 
166          for (int i = 0; i < v.size(); i++) { 
167              System.out.println(v.elementAt(i)); 
168          } 
169      } 
170   
171      public static Vector getUrl(String _URLString) { 
172          Vector contents = new Vector(); 
173          BufferedReader in = null; 
174          try { 
175              URL URLString = new URL(_URLString); 
176              InputStream is = URLString.openStream(); 
177              in = new BufferedReader(new 
178                      InputStreamReader(is)); 
179              String line; 
180              contents.addElement("Copyright 1999 by Doug Lyon http://www.docjava.com"); 
181              contents.addElement("symbol\tprice\tdate\ttime" + 
182                      "  (20 min. Delay)\t  change\t  open\t  bid\t  ask\t  volume"); 
183              while (null != (line = in.readLine())) 
184                  contents.addElement(line); 
185          } catch (Exception e) { 
186              e.printStackTrace(); 
187          } finally { 
188              try { 
189                  if (in != null) in.close(); 
190              } catch (IOException e) { 
191                  e.printStackTrace(); 
192              } 
193          } 
194          return contents; 
195      } 
196   
197      public static String makeQuoteURLString(Vector symbols) { 
198          String symbolsString = ""; 
199          for (int i = 0; i < symbols.size(); i++) { 
200              String symbol = (String) symbols.elementAt(i); 
201              symbolsString += ((i != 0) ? "," : "") + symbol.toUpperCase(); 
202          } 
203          return 
204                  "http://216.109.124.128/download/javasoft.beans?SYMBOLS=" + 
205                  symbolsString + 
206                  "&format=sl"; 
207      } 
208   
209      public String getSymbol() { 
210          return symbol; 
211      } 
212   
213      public float getPrice() { 
214          return price; 
215      } 
216   
217      public Date getDate() { 
218          return date; 
219      } 
220   
221      public float getChange() { 
222          return change; 
223      } 
224   
225      public float getOpen() { 
226          return open; 
227      } 
228   
229      public float getBid() { 
230          return bid; 
231      } 
232   
233      public float getAsk() { 
234          return ask; 
235      } 
236   
237      public long getVolume() { 
238          return volume; 
239      } 
240  } 
241