001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.util;
009:
010: import java.util.Date;
011: import java.util.Locale;
012: import java.util.MissingResourceException;
013: import java.util.ResourceBundle;
014:
015: /**
016: * An abstract class representing a holiday.
017: * @draft ICU 2.8 (retainAll)
018: * @provisional This API might change or be removed in a future release.
019: */
020: public abstract class Holiday implements DateRule {
021: /**
022: * @draft ICU 2.8
023: * @provisional This API might change or be removed in a future release.
024: */
025: public static Holiday[] getHolidays() {
026: return getHolidays(ULocale.getDefault());
027: }
028:
029: /**
030: * @draft ICU 2.8
031: * @provisional This API might change or be removed in a future release.
032: */
033: public static Holiday[] getHolidays(Locale locale) {
034: return getHolidays(ULocale.forLocale(locale));
035: }
036:
037: /**
038: * @draft ICU 3.2
039: * @provisional This API might change or be removed in a future release.
040: */
041: public static Holiday[] getHolidays(ULocale locale) {
042: Holiday[] result = noHolidays;
043:
044: try {
045: ResourceBundle bundle = UResourceBundle.getBundleInstance(
046: "HolidayBundle", locale);
047:
048: result = (Holiday[]) bundle.getObject("holidays");
049: } catch (MissingResourceException e) {
050: }
051: return result;
052: }
053:
054: /**
055: * Return the first occurrance of this holiday on or after the given date
056: *
057: * @param start Only holidays on or after this date are returned.
058: *
059: * @return The date on which this holiday occurs, or null if it
060: * does not occur on or after the start date.
061: *
062: * @see #firstBetween
063: * @draft ICU 2.8
064: * @provisional This API might change or be removed in a future release.
065: */
066: public Date firstAfter(Date start) {
067: return rule.firstAfter(start);
068: }
069:
070: /**
071: * Return the first occurrance of this holiday that is on or after
072: * the given start date and before the given end date.
073: *
074: * @param start Only occurrances on or after this date are returned.
075: * @param end Only occurrances before this date are returned.
076: *
077: * @return The date on which this event occurs, or null if it
078: * does not occur between the start and end dates.
079: *
080: * @see #firstAfter
081: * @draft ICU 2.8
082: * @provisional This API might change or be removed in a future release.
083: */
084: public Date firstBetween(Date start, Date end) {
085: return rule.firstBetween(start, end);
086: }
087:
088: /**
089: * Checks whether this holiday falls on the given date. This does
090: * <em>not</em> take time of day into account; instead it checks
091: * whether the holiday and the given date are on the same day.
092: *
093: * @param date The date to check.
094: * @return true if this holiday occurs on the given date.
095: * @draft ICU 2.8
096: * @provisional This API might change or be removed in a future release.
097: */
098: public boolean isOn(Date date) {
099: //System.out.println(name + ".isOn(" + date.toString() + "):");
100: return rule.isOn(date);
101: }
102:
103: /**
104: * Check whether this holiday occurs at least once between the two
105: * dates given.
106: * @draft ICU 2.8
107: * @provisional This API might change or be removed in a future release.
108: */
109: public boolean isBetween(Date start, Date end) {
110: return rule.isBetween(start, end);
111: }
112:
113: /**
114: * Construct a new Holiday object. This is for use by subclasses only.
115: * This constructs a new holiday with the given name and date rules.
116: *
117: * @param name The name of this holiday. The getDisplayName method
118: * uses this string as a key to look up the holiday's name a
119: * resource bundle object named HolidayBundle.
120: *
121: * @param rule The date rules used for determining when this holiday
122: * falls. Holiday's implementation of the DateRule inteface
123: * simply delegates to this DateRule object.
124: * @draft ICU 2.8
125: * @provisional This API might change or be removed in a future release.
126: */
127: protected Holiday(String name, DateRule rule) {
128: this .name = name;
129: this .rule = rule;
130: }
131:
132: /**
133: * Return the name of this holiday in the language of the default locale
134: * @draft ICU 2.8
135: * @provisional This API might change or be removed in a future release.
136: */
137: public String getDisplayName() {
138: return getDisplayName(ULocale.getDefault());
139: }
140:
141: /**
142: * Return the name of this holiday in the language of the specified locale
143: * The <code>name</code> parameter passed to this object's constructor is used
144: * as a key to look up the holiday's localized name in a ResourceBundle object
145: * named HolidayBundle.
146: *
147: * @param locale A locale specifying the language in which the name is desired.
148: *
149: * @see ResourceBundle
150: * @draft ICU 2.8
151: * @provisional This API might change or be removed in a future release.
152: */
153: public String getDisplayName(Locale locale) {
154: return getDisplayName(ULocale.forLocale(locale));
155: }
156:
157: /**
158: * Return the name of this holiday in the language of the specified locale
159: * The <code>name</code> parameter passed to this object's constructor is used
160: * as a key to look up the holiday's localized name in a ResourceBundle object
161: * named HolidayBundle.
162: *
163: * @param locale A locale specifying the language in which the name is desired.
164: *
165: * @see ResourceBundle
166: * @draft ICU 3.2
167: * @provisional This API might change or be removed in a future release.
168: */
169: public String getDisplayName(ULocale locale) {
170: String name = this .name;
171:
172: try {
173: ResourceBundle bundle = UResourceBundle.getBundleInstance(
174: "HolidayBundle", locale);
175: name = bundle.getString(name);
176: } catch (MissingResourceException e) {
177: //System.out.println("Using default display name for " + name);
178: }
179: return name;
180: }
181:
182: /**
183: * @draft ICU 2.8
184: * @provisional This API might change or be removed in a future release.
185: */
186: public DateRule getRule() {
187: return rule;
188: }
189:
190: /**
191: * @draft ICU 2.8
192: * @provisional This API might change or be removed in a future release.
193: */
194: public void setRule(DateRule rule) {
195: this .rule = rule;
196: }
197:
198: private String name;
199: private DateRule rule;
200:
201: private static Holiday[] noHolidays = {};
202: }
|