| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 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 | |
|
| 57 | |
|
| 58 | |
public class PoemLocale { |
| 59 | |
|
| 60 | 1 | private static final HashMap<Locale, PoemLocale> localeCache = new HashMap<Locale, PoemLocale>(); |
| 61 | |
|
| 62 | |
|
| 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 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public static PoemLocale fromLanguageTag(String tag) { |
| 79 | 9 | String subtags[] = StringUtils.split(tag, '-'); |
| 80 | |
|
| 81 | |
|
| 82 | 9 | if (subtags.length > 0 && subtags[0].length() == 2) { |
| 83 | 8 | Locale locale = null; |
| 84 | |
|
| 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 | |
|
| 105 | |
|
| 106 | |
|
| 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]; |
| 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]; |
| 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 | |
|
| 144 | |
|
| 145 | |
public final Locale locale() { |
| 146 | 6 | return locale; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public String monthName(int monthNum) { |
| 154 | 1 | return months[monthNum - 1]; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public String shortMonthName(int monthNum) { |
| 162 | 1 | return shortMonths[monthNum - 1]; |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public DateFormat dateFormat(int style) { |
| 170 | 3 | return dateFormats[style]; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public DateFormat timestampFormat(int style) { |
| 178 | 4 | return timestampFormats[style]; |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public int hashCode() { |
| 189 | 2 | return locale.hashCode(); |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public boolean equals(Object o) { |
| 197 | 6 | if (this == o) |
| 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 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public String toString() { |
| 212 | 7 | return locale.toString(); |
| 213 | |
} |
| 214 | |
} |