home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / TimeSeries.java < prev    next >
Encoding:
Java Source  |  1997-01-24  |  2.4 KB  |  104 lines

  1. package COM.odi.demo.pport;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * A TimeSeries contains a series of values, indexed by time.  For
  7.  * space efficiency it is represented as a three-level TRIE, with
  8.  * one level each for year, month, and day.
  9.  */
  10.  
  11. import COM.odi.*;
  12.  
  13. final
  14. public class TimeSeries
  15. {
  16.   /* The first year that this table represents. */
  17.  
  18.   private int startYear;
  19.  
  20.   /* First level table, indexed by years, relative to startYear. */
  21.  
  22.   private int[][][] years;
  23.  
  24.  
  25.   /**
  26.    * Make a new TimeSeries
  27.    * @param startYear years since 1900
  28.    * @param nYears initial number of years, just a hint
  29.    */
  30.   TimeSeries(int startYear, int nYears)
  31.   {
  32.     this.startYear = startYear;
  33.     years = new int[nYears][][];
  34.   }
  35.  
  36.   /**
  37.    * Return the value at the specified date.
  38.    * @param year years since 1900
  39.    * @param month months since January - [0, 11] 
  40.    * @param day day of the month - [1, 31]
  41.    * @return the value at the specified date
  42.    */
  43.   int lookup(int year, int month, int day)
  44.   {
  45.     int[][] months = years[year - startYear];
  46.     if (months != null) {
  47.       int[] days = months[month];
  48.       if (days != null)
  49.     return days[day - 1];
  50.     }
  51.     return 0;
  52.   }
  53.  
  54.   /**
  55.    * Set the value at the specified date.
  56.    * @param date the date
  57.    * @param value the value at this date
  58.    */
  59.   void insert(TradeDate date, int value)
  60.   {
  61.     int relativeYear = date.year - startYear;
  62.  
  63.     /* If the year exceeds the size of the years array, grow the years
  64.        array to be large enough, plus 10 for future expansion. */
  65.  
  66.     int nYears = years.length;
  67.     if (relativeYear > nYears) {
  68.       int[][][] newYears = new int[relativeYear + 10][][];
  69.       for (int i = 0; i < nYears; i++)
  70.     newYears[i] = years[i];
  71.       years = newYears;
  72.     }
  73.  
  74.     /* Get the months array, creating it if necessary. */
  75.  
  76.     int[][] months = years[relativeYear];
  77.     if (months == null) {
  78.       months = new int[12][];
  79.       years[relativeYear] = months;
  80.     }
  81.  
  82.     /* Get the days array, creating it if necessary. */
  83.  
  84.     int[] days = months[date.month];
  85.     if (days == null) {
  86.       days = new int[TradeDate.daysPerMonth(date.month, date.year)];
  87.       months[date.month] = days;
  88.     }
  89.  
  90.     days[date.day - 1] = value;
  91.   }
  92.  
  93.   public int[][][] getYears() {
  94.     return years;
  95.   }
  96.  
  97.   /* This class is never used as a persistent hash key. */
  98.   public int hashCode() {
  99.     return super.hashCode();
  100.   }
  101.  
  102. }
  103.  
  104.