001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package javax.microedition.global;
027:
028: import com.sun.j2me.global.FormatAbstractionLayer;
029: import java.util.Calendar;
030:
031: import com.sun.j2me.global.CommonFormatter;
032: import com.sun.j2me.global.MessageFormat;
033: import com.sun.j2me.global.LocaleHelpers;
034:
035: // JAVADOC COMMENT ELIDED
036: public final class Formatter {
037:
038: // JAVADOC COMMENT ELIDED
039: public static final int TIME_LONG = 3;
040:
041: // JAVADOC COMMENT ELIDED
042: public static final int TIME_SHORT = 2;
043:
044: // JAVADOC COMMENT ELIDED
045: public static final int DATE_LONG = 1;
046:
047: // JAVADOC COMMENT ELIDED
048: public static final int DATE_SHORT = 0;
049:
050: // JAVADOC COMMENT ELIDED
051: public static final int DATETIME_LONG = 5;
052:
053: // JAVADOC COMMENT ELIDED
054: public static final int DATETIME_SHORT = 4;
055:
056: /**
057: * Current Formatter locale.
058: */
059: private String locale;
060:
061: /**
062: * FormatAbstractionLayer subclass instance for obtaining of Formatter
063: * realization.
064: */
065: private static FormatAbstractionLayer formatAbstractionLayer = FormatAbstractionLayer
066: .getInstance();
067:
068: /**
069: * Current Formatter realization instance.
070: */
071: private CommonFormatter formatterImpl;
072:
073: // JAVADOC COMMENT ELIDED
074: public Formatter() throws UnsupportedLocaleException {
075: this (System.getProperty("microedition.locale"));
076: }
077:
078: // JAVADOC COMMENT ELIDED
079: public Formatter(String locale) throws UnsupportedLocaleException,
080: IllegalArgumentException {
081: if (!LocaleHelpers.isValidLocale(locale)
082: && !("".equals(locale))) {
083: throw new IllegalArgumentException("Invalid locale format");
084: }
085:
086: locale = LocaleHelpers.normalizeLocale(locale);
087:
088: if ("".equals(locale)) {
089: this .locale = null;
090: } else {
091: this .locale = locale;
092: }
093:
094: if (this .locale == null) {
095: formatterImpl = FormatAbstractionLayer
096: .getNeutralFormatter();
097: } else {
098: formatterImpl = formatAbstractionLayer.getFormatter(locale);
099: }
100: }
101:
102: // JAVADOC COMMENT ELIDED
103: public static String formatMessage(String template, String[] params) {
104: if (template == null || params == null) {
105: throw new NullPointerException(
106: "Template or parameter array is null.");
107: }
108: return MessageFormat.format(template, params);
109: }
110:
111: // JAVADOC COMMENT ELIDED
112: public String formatDateTime(Calendar dateTime, int style) {
113: if (dateTime == null) {
114: throw new NullPointerException("Calendar is null.");
115: }
116: if (style < DATE_SHORT || style > DATETIME_LONG) {
117: throw new IllegalArgumentException("Illegal style value");
118: }
119:
120: return formatterImpl.formatDateTime(dateTime, style);
121: }
122:
123: // JAVADOC COMMENT ELIDED
124: public String formatCurrency(double number) {
125: return formatterImpl.formatCurrency(number);
126: }
127:
128: // JAVADOC COMMENT ELIDED
129: public String formatCurrency(double number, String currencyCode)
130: throws IllegalArgumentException {
131:
132: if (currencyCode.length() != 3 || currencyCode.charAt(0) < 'A'
133: || currencyCode.charAt(0) > 'Z'
134: || currencyCode.charAt(1) < 'A'
135: || currencyCode.charAt(1) > 'Z'
136: || currencyCode.charAt(2) < 'A'
137: || currencyCode.charAt(2) > 'Z') {
138: throw new IllegalArgumentException("Illegal currency code");
139: }
140:
141: return formatterImpl.formatCurrency(number, currencyCode);
142: }
143:
144: // JAVADOC COMMENT ELIDED
145: public String formatNumber(double number) {
146: return formatterImpl.formatNumber(number);
147: }
148:
149: // JAVADOC COMMENT ELIDED
150: public String formatNumber(double number, int decimals)
151: throws IllegalArgumentException {
152:
153: if (decimals < 1 || decimals > 15) {
154: throw new IllegalArgumentException(
155: "Illegal number of decimals");
156: }
157:
158: return formatterImpl.formatNumber(number, decimals);
159: }
160:
161: // JAVADOC COMMENT ELIDED
162: public String formatNumber(long number) {
163: return formatterImpl.formatNumber(number);
164: }
165:
166: // JAVADOC COMMENT ELIDED
167: public String formatPercentage(long number) {
168: return formatterImpl.formatPercentage(number);
169: }
170:
171: // JAVADOC COMMENT ELIDED
172: public String formatPercentage(float number, int decimals)
173: throws IllegalArgumentException {
174:
175: if (decimals < 1 || decimals > 15) {
176: throw new IllegalArgumentException(
177: "Illegal number of decimals");
178: }
179:
180: return formatterImpl.formatPercentage(number, decimals);
181: }
182:
183: // JAVADOC COMMENT ELIDED
184: public static String[] getSupportedLocales() {
185: return formatAbstractionLayer.getSupportedLocales();
186: }
187:
188: // JAVADOC COMMENT ELIDED
189: public String getLocale() {
190: return locale;
191: }
192: }
|