home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-24 | 2.4 KB | 104 lines |
- package COM.odi.demo.pport;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * A TimeSeries contains a series of values, indexed by time. For
- * space efficiency it is represented as a three-level TRIE, with
- * one level each for year, month, and day.
- */
-
- import COM.odi.*;
-
- final
- public class TimeSeries
- {
- /* The first year that this table represents. */
-
- private int startYear;
-
- /* First level table, indexed by years, relative to startYear. */
-
- private int[][][] years;
-
-
- /**
- * Make a new TimeSeries
- * @param startYear years since 1900
- * @param nYears initial number of years, just a hint
- */
- TimeSeries(int startYear, int nYears)
- {
- this.startYear = startYear;
- years = new int[nYears][][];
- }
-
- /**
- * Return the value at the specified date.
- * @param year years since 1900
- * @param month months since January - [0, 11]
- * @param day day of the month - [1, 31]
- * @return the value at the specified date
- */
- int lookup(int year, int month, int day)
- {
- int[][] months = years[year - startYear];
- if (months != null) {
- int[] days = months[month];
- if (days != null)
- return days[day - 1];
- }
- return 0;
- }
-
- /**
- * Set the value at the specified date.
- * @param date the date
- * @param value the value at this date
- */
- void insert(TradeDate date, int value)
- {
- int relativeYear = date.year - startYear;
-
- /* If the year exceeds the size of the years array, grow the years
- array to be large enough, plus 10 for future expansion. */
-
- int nYears = years.length;
- if (relativeYear > nYears) {
- int[][][] newYears = new int[relativeYear + 10][][];
- for (int i = 0; i < nYears; i++)
- newYears[i] = years[i];
- years = newYears;
- }
-
- /* Get the months array, creating it if necessary. */
-
- int[][] months = years[relativeYear];
- if (months == null) {
- months = new int[12][];
- years[relativeYear] = months;
- }
-
- /* Get the days array, creating it if necessary. */
-
- int[] days = months[date.month];
- if (days == null) {
- days = new int[TradeDate.daysPerMonth(date.month, date.year)];
- months[date.month] = days;
- }
-
- days[date.day - 1] = value;
- }
-
- public int[][][] getYears() {
- return years;
- }
-
- /* This class is never used as a persistent hash key. */
- public int hashCode() {
- return super.hashCode();
- }
-
- }
-
-