Coverage Report - org.melati.poem.PoemLocale
 
Classes in this File Line Coverage Branch Coverage Complexity
PoemLocale
88%
48/54
66%
12/18
2.273
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati.poem;
 47  
 
 48  
 import java.util.HashMap;
 49  
 import java.util.Locale;
 50  
 import java.text.DateFormat;
 51  
 import java.text.DateFormatSymbols;
 52  
 
 53  
 import org.melati.poem.util.StringUtils;
 54  
 
 55  
 /**
 56  
  * A wrapper for a <code>Locale</code> for use within Melati.
 57  
  */
 58  
 public class PoemLocale {
 59  
 
 60  1
   private static final HashMap<Locale, PoemLocale> localeCache = new HashMap<Locale, PoemLocale>();
 61  
 
 62  
   /** Default Locale: GB. */
 63  1
   public static final PoemLocale HERE = new PoemLocale(Locale.UK);
 64  
 
 65  
   private final Locale locale;
 66  
   
 67  
   private final DateFormatSymbols dateFormatSymbols;
 68  
   private final String[] months, shortMonths;
 69  
   private final DateFormat[] dateFormats;
 70  
   private final DateFormat[] timestampFormats;
 71  
   
 72  
   /**
 73  
    * Creates a melati locale from a language tag as defined in RFC3066.
 74  
    * 
 75  
    * @param tag A language tag, for example, "en-gb"
 76  
    * @return A melati locale from the tag if we can parse it, otherwise null
 77  
    */
 78  
   public static PoemLocale fromLanguageTag(String tag) {
 79  9
     String subtags[] = StringUtils.split(tag, '-');
 80  
 
 81  
     // if 1st subtag is 2 letters, then it's a 2 letter language code
 82  9
     if (subtags.length > 0 && subtags[0].length() == 2) {
 83  8
       Locale locale = null;
 84  
       // if 2nd subtag exists and is 2 letters, then it's a 2 letter county code
 85  8
       if (subtags.length > 1 && subtags[1].length() == 2)
 86  4
         locale = new Locale(subtags[0], subtags[1]);
 87  
       else
 88  4
         locale = new Locale(subtags[0], "");
 89  8
       return new PoemLocale(locale);
 90  
     }
 91  1
     return null;
 92  
   } 
 93  
   
 94  
   public static PoemLocale from(Locale locale) {
 95  0
     if (locale == null)
 96  0
       throw new NullPointerException();
 97  0
     PoemLocale it = localeCache.get(locale);
 98  0
     if(it == null)
 99  0
       localeCache.put(locale, new PoemLocale(locale));
 100  0
     return localeCache.get(locale);
 101  
   }
 102  
 
 103  
   /**
 104  
    * Constructor given a non-null Locale. 
 105  
    * 
 106  
    * @param locale The Locale to base ours on.
 107  
    */
 108  12
   public PoemLocale(Locale locale) {
 109  12
     if (locale == null)
 110  1
       throw new NullPointerException();
 111  11
     this.locale = locale;
 112  
 
 113  11
     dateFormatSymbols = new DateFormatSymbols(locale);
 114  11
     months = dateFormatSymbols.getMonths();
 115  11
     shortMonths = dateFormatSymbols.getShortMonths();
 116  
 
 117  11
     dateFormats = new DateFormat[4]; // don't tell me this will break
 118  11
     dateFormats[DateFormat.FULL] =
 119  11
         DateFormat.getDateInstance(DateFormat.FULL, locale);
 120  11
     dateFormats[DateFormat.LONG] =
 121  11
         DateFormat.getDateInstance(DateFormat.LONG, locale);
 122  11
     dateFormats[DateFormat.MEDIUM] =
 123  11
         DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
 124  11
     dateFormats[DateFormat.SHORT] =
 125  11
         DateFormat.getDateInstance(DateFormat.SHORT, locale);
 126  
 
 127  11
     timestampFormats = new DateFormat[4]; // don't tell me this will break
 128  11
     timestampFormats[DateFormat.FULL] =
 129  11
         DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,
 130  
                                        locale);
 131  11
     timestampFormats[DateFormat.LONG] =
 132  11
         DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
 133  
                                        locale);
 134  11
     timestampFormats[DateFormat.MEDIUM] =
 135  11
         DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
 136  
                                        locale);
 137  11
     timestampFormats[DateFormat.SHORT] =
 138  11
         DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
 139  
                                        locale);
 140  11
   }
 141  
 
 142  
   /**
 143  
    * @return the Locale
 144  
    */
 145  
   public final Locale locale() {
 146  6
     return locale;
 147  
   }
 148  
 
 149  
   /**
 150  
    * @param monthNum numeric month
 151  
    * @return full name of month
 152  
    */
 153  
   public String monthName(int monthNum) {
 154  1
     return months[monthNum - 1];
 155  
   }
 156  
 
 157  
   /**
 158  
    * @param monthNum numeric month
 159  
    * @return short name of month
 160  
    */
 161  
   public String shortMonthName(int monthNum) {
 162  1
     return shortMonths[monthNum - 1];
 163  
   }
 164  
 
 165  
   /**
 166  
    * @param style as defined in DateFormat
 167  
    * @return a format of that style
 168  
    */
 169  
   public DateFormat dateFormat(int style) {
 170  3
     return dateFormats[style];
 171  
   }
 172  
 
 173  
   /**
 174  
    * @param style as defined in DateFormat
 175  
    * @return a format of that style
 176  
    */
 177  
   public DateFormat timestampFormat(int style) {
 178  4
     return timestampFormats[style];
 179  
   }
 180  
 
 181  
   /**
 182  
    * Delegated to Locale.
 183  
    * 
 184  
    * @see java.util.Locale#hashCode()
 185  
    * {@inheritDoc}
 186  
    * @see java.lang.Object#hashCode()
 187  
    */
 188  
   public int hashCode() {
 189  2
     return locale.hashCode();
 190  
   }
 191  
 
 192  
   /**
 193  
    * {@inheritDoc}
 194  
    * @see java.lang.Object#equals(java.lang.Object)
 195  
    */
 196  
   public boolean equals(Object o) {
 197  6
     if (this == o)     // quick check
 198  1
       return true;
 199  5
     if (o instanceof PoemLocale) 
 200  4
       return locale.equals(((PoemLocale)o).locale());
 201  
     else 
 202  1
       return false;
 203  
   }
 204  
   
 205  
   /**
 206  
    * Delegated to Locale.
 207  
    * 
 208  
    * {@inheritDoc}
 209  
    * @see java.lang.Object#toString()
 210  
    */
 211  
   public String toString() {
 212  7
     return locale.toString();
 213  
   }
 214  
 }