home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 2.3 KB | 107 lines |
- package COM.odi.demo.pport;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * A Security has a history of its price over time.
- */
-
- import java.io.*;
- import java.util.*;
- import COM.odi.*;
- import COM.odi.util.*;
-
- final
- public class Security
- {
- /* The ticker symbol. */
-
- String symbol;
-
- /* The price history, i.e. what price did it have at a given date. */
-
- private TimeSeries priceHistory;
-
- /* Hash table of all Security objects, keyed by symbol. */
-
- static OSHashtable all = new OSHashtable();
-
-
- Security(String symbol)
- {
- this.symbol = symbol;
- priceHistory = new TimeSeries(50, 50);
- all.put(symbol, this);
- }
-
- static Security get(String symbol)
- {
- Security result = (Security)all.get(symbol);
- if (result == null) {
- result = new Security(symbol);
-
- /* Temporary, for debugging */
- System.out.println("New security " + symbol);
-
- }
- return result;
- }
-
- int getPrice(TradeDate date)
- {
- return priceHistory.lookup(date.year, date.month, date.day);
- }
-
- /**
- * Load up a new database from the textual securities file.
- * @param pathname The pathname of the securities file.
- * @param db The database to store the objects into.
- */
- static void load(String pathname, Database db) throws IOException
- {
- BufferedReader stream = null;
- try {
- stream = new BufferedReader(new FileReader(pathname));
- all.clear();
- String line = stream.readLine();
- while (line != null) {
- StringTokenizer tkn = new StringTokenizer(line, "\t");
- Security sec = get(tkn.nextToken());
- TradeDate date = new TradeDate(tkn.nextToken());
- int price = (int) (1000.0 * (new Float(tkn.nextToken())).floatValue());
- sec.priceHistory.insert(date, price);
- line = stream.readLine();
- }
- } finally {
- if (stream != null)
- stream.close();
- }
- db.createRoot("securities", all);
- }
-
- static void initialize(Database db)
- {
- all = (OSHashtable)db.getRoot("securities");
- }
-
- public String getSymbol() {
- return symbol;
- }
-
- public TimeSeries getPriceHistory() {
- return priceHistory;
- }
-
- static public Enumeration getAll() {
- return all.elements();
- }
-
-
- /* This class is never used as a persistent hash key. */
- public int hashCode() {
- return super.hashCode();
- }
-
- }
-
-