Home | History | Annotate | Download | only in util
      1 /*
      2  *******************************************************************************
      3  * Copyright (C) 1996-2011, International Business Machines Corporation and    *
      4  * others. All Rights Reserved.                                                *
      5  *******************************************************************************
      6  */
      7 
      8 package com.ibm.icu.util;
      9 
     10 import java.util.Date;
     11 import java.util.Locale;
     12 import java.util.MissingResourceException;
     13 import java.util.ResourceBundle;
     14 
     15 import com.ibm.icu.util.ULocale.Category;
     16 
     17 /**
     18  * <b>Note:</b> The Holiday framework is a technology preview.
     19  * Despite its age, is still draft API, and clients should treat it as such.
     20  *
     21  * An abstract class representing a holiday.
     22  * @draft ICU 2.8 (retainAll)
     23  * @provisional This API might change or be removed in a future release.
     24  */
     25 public abstract class Holiday implements DateRule
     26 {
     27     /**
     28      * @draft ICU 2.8
     29      * @provisional This API might change or be removed in a future release.
     30      */
     31     public static Holiday[] getHolidays()
     32     {
     33         return getHolidays(ULocale.getDefault(Category.FORMAT));
     34     }
     35 
     36     /**
     37      * @draft ICU 2.8
     38      * @provisional This API might change or be removed in a future release.
     39      */
     40     public static Holiday[] getHolidays(Locale locale)
     41     {
     42         return getHolidays(ULocale.forLocale(locale));
     43     }
     44 
     45     /**
     46      * @draft ICU 3.2
     47      * @provisional This API might change or be removed in a future release.
     48      */
     49     public static Holiday[] getHolidays(ULocale locale)
     50     {
     51         Holiday[] result = noHolidays;
     52 
     53         try {
     54             ResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", locale);
     55 
     56             result = (Holiday[]) bundle.getObject("holidays");
     57         }
     58         catch (MissingResourceException e) {
     59         }
     60         return result;
     61     }
     62 
     63     /**
     64      * Return the first occurrence of this holiday on or after the given date
     65      *
     66      * @param start Only holidays on or after this date are returned.
     67      *
     68      * @return      The date on which this holiday occurs, or null if it
     69      *              does not occur on or after the start date.
     70      *
     71      * @see #firstBetween
     72      * @draft ICU 2.8
     73      * @provisional This API might change or be removed in a future release.
     74      */
     75     public Date firstAfter(Date start) {
     76         return rule.firstAfter(start);
     77     }
     78 
     79     /**
     80      * Return the first occurrence of this holiday that is on or after
     81      * the given start date and before the given end date.
     82      *
     83      * @param start Only occurrences on or after this date are returned.
     84      * @param end   Only occurrences before this date are returned.
     85      *
     86      * @return      The date on which this event occurs, or null if it
     87      *              does not occur between the start and end dates.
     88      *
     89      * @see #firstAfter
     90      * @draft ICU 2.8
     91      * @provisional This API might change or be removed in a future release.
     92      */
     93     public Date firstBetween(Date start, Date end) {
     94         return rule.firstBetween(start, end);
     95     }
     96 
     97     /**
     98      * Checks whether this holiday falls on the given date.  This does
     99      * <em>not</em> take time of day into account; instead it checks
    100      * whether the holiday and the given date are on the same day.
    101      *
    102      * @param date  The date to check.
    103      * @return      true if this holiday occurs on the given date.
    104      * @draft ICU 2.8
    105      * @provisional This API might change or be removed in a future release.
    106      */
    107     public boolean isOn(Date date) {
    108         //System.out.println(name + ".isOn(" + date.toString() + "):");
    109         return rule.isOn(date);
    110     }
    111 
    112     /**
    113      * Check whether this holiday occurs at least once between the two
    114      * dates given.
    115      * @draft ICU 2.8
    116      * @provisional This API might change or be removed in a future release.
    117      */
    118     public boolean isBetween(Date start, Date end) {
    119         return rule.isBetween(start, end);
    120     }
    121 
    122     /**
    123      * Construct a new Holiday object.  This is for use by subclasses only.
    124      * This constructs a new holiday with the given name and date rules.
    125      *
    126      * @param name  The name of this holiday.  The getDisplayName method
    127      *              uses this string as a key to look up the holiday's name a
    128      *              resource bundle object named HolidayBundle.
    129      *
    130      * @param rule  The date rules used for determining when this holiday
    131      *              falls.  Holiday's implementation of the DateRule interface
    132      *              simply delegates to this DateRule object.
    133      * @draft ICU 2.8
    134      * @provisional This API might change or be removed in a future release.
    135      */
    136     protected Holiday(String name, DateRule rule)
    137     {
    138         this.name = name;
    139         this.rule = rule;
    140     }
    141 
    142     /**
    143      * Return the name of this holiday in the language of the default <code>DISPLAY</code> locale.
    144      * @see Category#DISPLAY
    145      * @draft ICU 2.8
    146      * @provisional This API might change or be removed in a future release.
    147      */
    148     public String getDisplayName() {
    149         return getDisplayName(ULocale.getDefault(Category.DISPLAY));
    150     }
    151 
    152     /**
    153      * Return the name of this holiday in the language of the specified locale.
    154      * The <code>name</code> parameter passed to this object's constructor is used
    155      * as a key to look up the holiday's localized name in a ResourceBundle object
    156      * named HolidayBundle.
    157      *
    158      * @param locale   A locale specifying the language in which the name is desired.
    159      *
    160      * @see ResourceBundle
    161      * @draft ICU 2.8
    162      * @provisional This API might change or be removed in a future release.
    163      */
    164     public String getDisplayName(Locale locale)
    165     {
    166         return getDisplayName(ULocale.forLocale(locale));
    167     }
    168 
    169     /**
    170      * Return the name of this holiday in the language of the specified locale
    171      * The <code>name</code> parameter passed to this object's constructor is used
    172      * as a key to look up the holiday's localized name in a ResourceBundle object
    173      * named HolidayBundle.
    174      *
    175      * @param locale   A locale specifying the language in which the name is desired.
    176      *
    177      * @see ResourceBundle
    178      * @draft ICU 3.2
    179      * @provisional This API might change or be removed in a future release.
    180      */
    181     public String getDisplayName(ULocale locale)
    182     {
    183         String dispName = name;
    184 
    185         try {
    186             ResourceBundle bundle = UResourceBundle.getBundleInstance("com.ibm.icu.impl.data.HolidayBundle", locale);
    187             dispName = bundle.getString(name);
    188         }
    189         catch (MissingResourceException e) {
    190         }
    191         return dispName;
    192     }
    193 
    194     /**
    195      * @draft ICU 2.8
    196      * @provisional This API might change or be removed in a future release.
    197      */
    198     public DateRule getRule() {
    199         return rule;
    200     }
    201 
    202     /**
    203      * @draft ICU 2.8
    204      * @provisional This API might change or be removed in a future release.
    205      */
    206     public void setRule(DateRule rule) {
    207         this.rule = rule;
    208     }
    209 
    210     private String      name;
    211     private DateRule    rule;
    212 
    213     private static Holiday[] noHolidays = {};
    214 }
    215