1 /*
   2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.locale.provider;
  27 
  28 import java.text.DateFormat;
  29 import java.text.SimpleDateFormat;
  30 import java.text.spi.DateFormatProvider;
  31 import java.util.Calendar;
  32 import java.util.Locale;
  33 import java.util.MissingResourceException;
  34 import java.util.Set;
  35 
  36 /**
  37  * Concrete implementation of the  {@link java.text.spi.DateFormatProvider
  38  * DateFormatProvider} class for the JRE LocaleProviderAdapter.
  39  *
  40  * @author Naoto Sato
  41  * @author Masayoshi Okutsu
  42  */
  43 public class DateFormatProviderImpl extends DateFormatProvider implements AvailableLanguageTags {
  44     private final LocaleProviderAdapter.Type type;
  45     private final Set<String> langtags;
  46 
  47     public DateFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  48         this.type = type;
  49         this.langtags = langtags;
  50     }
  51 
  52     /**
  53      * Returns an array of all locales for which this locale service provider
  54      * can provide localized objects or names.
  55      *
  56      * @return An array of all locales for which this locale service provider
  57      * can provide localized objects or names.
  58      */
  59     @Override
  60     public Locale[] getAvailableLocales() {
  61         return LocaleProviderAdapter.toLocaleArray(langtags);
  62     }
  63 
  64     @Override
  65     public boolean isSupportedLocale(Locale locale) {
  66         return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
  67     }
  68 
  69     /**
  70      * Returns a new <code>DateFormat</code> instance which formats time
  71      * with the given formatting style for the specified locale.
  72      * @param style the given formatting style.  Either one of
  73      *     {@link java.text.DateFormat#SHORT DateFormat.SHORT},
  74      *     {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM},
  75      *     {@link java.text.DateFormat#LONG DateFormat.LONG}, or
  76      *     {@link java.text.DateFormat#FULL DateFormat.FULL}.
  77      * @param locale the desired locale.
  78      * @exception IllegalArgumentException if <code>style</code> is invalid,
  79      *     or if <code>locale</code> isn't
  80      *     one of the locales returned from
  81      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  82      *     getAvailableLocales()}.
  83      * @exception NullPointerException if <code>locale</code> is null
  84      * @return a time formatter.
  85      * @see java.text.DateFormat#getTimeInstance(int, java.util.Locale)
  86      */
  87     @Override
  88     public DateFormat getTimeInstance(int style, Locale locale) {
  89         return getInstance(-1, style, locale);
  90     }
  91 
  92     /**
  93      * Returns a new <code>DateFormat</code> instance which formats date
  94      * with the given formatting style for the specified locale.
  95      * @param style the given formatting style.  Either one of
  96      *     {@link java.text.DateFormat#SHORT DateFormat.SHORT},
  97      *     {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM},
  98      *     {@link java.text.DateFormat#LONG DateFormat.LONG}, or
  99      *     {@link java.text.DateFormat#FULL DateFormat.FULL}.
 100      * @param locale the desired locale.
 101      * @exception IllegalArgumentException if <code>style</code> is invalid,
 102      *     or if <code>locale</code> isn't
 103      *     one of the locales returned from
 104      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 105      *     getAvailableLocales()}.
 106      * @exception NullPointerException if <code>locale</code> is null
 107      * @return a date formatter.
 108      * @see java.text.DateFormat#getDateInstance(int, java.util.Locale)
 109      */
 110     @Override
 111     public DateFormat getDateInstance(int style, Locale locale) {
 112         return getInstance(style, -1, locale);
 113     }
 114 
 115     /**
 116      * Returns a new <code>DateFormat</code> instance which formats date and time
 117      * with the given formatting style for the specified locale.
 118      * @param dateStyle the given date formatting style.  Either one of
 119      *     {@link java.text.DateFormat#SHORT DateFormat.SHORT},
 120      *     {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM},
 121      *     {@link java.text.DateFormat#LONG DateFormat.LONG}, or
 122      *     {@link java.text.DateFormat#FULL DateFormat.FULL}.
 123      * @param timeStyle the given time formatting style.  Either one of
 124      *     {@link java.text.DateFormat#SHORT DateFormat.SHORT},
 125      *     {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM},
 126      *     {@link java.text.DateFormat#LONG DateFormat.LONG}, or
 127      *     {@link java.text.DateFormat#FULL DateFormat.FULL}.
 128      * @param locale the desired locale.
 129      * @exception IllegalArgumentException if <code>dateStyle</code> or
 130      *     <code>timeStyle</code> is invalid,
 131      *     or if <code>locale</code> isn't
 132      *     one of the locales returned from
 133      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 134      *     getAvailableLocales()}.
 135      * @exception NullPointerException if <code>locale</code> is null
 136      * @return a date/time formatter.
 137      * @see java.text.DateFormat#getDateTimeInstance(int, int, java.util.Locale)
 138      */
 139     @Override
 140     public DateFormat getDateTimeInstance(int dateStyle, int timeStyle,
 141                                           Locale locale) {
 142         return getInstance(dateStyle, timeStyle, locale);
 143     }
 144 
 145     private DateFormat getInstance(int dateStyle, int timeStyle, Locale locale) {
 146         if (locale == null) {
 147             throw new NullPointerException();
 148         }
 149 
 150         SimpleDateFormat sdf = new SimpleDateFormat("", locale);
 151         Calendar cal = sdf.getCalendar();
 152         try {
 153             String pattern = LocaleProviderAdapter.forType(type)
 154                 .getLocaleResources(locale).getDateTimePattern(timeStyle, dateStyle,
 155                                                                cal);
 156             sdf.applyPattern(pattern);
 157         } catch (MissingResourceException mre) {
 158             // Specify the fallback pattern
 159             sdf.applyPattern("M/d/yy h:mm a");
 160         }
 161 
 162         return sdf;
 163     }
 164 
 165     @Override
 166     public Set<String> getAvailableLanguageTags() {
 167         return langtags;
 168     }
 169 }