001: // yFormatter.java
002: // -----------------------
003: // part of YACY
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2004
007: //
008: // (C) 2007 Bjoern 'Fuchs' Krombholz; fox.box@gmail.com
009: //
010: // This program is free software; you can redistribute it and/or modify
011: // it under the terms of the GNU General Public License as published by
012: // the Free Software Foundation; either version 2 of the License, or
013: // (at your option) any later version.
014: //
015: // This program is distributed in the hope that it will be useful,
016: // but WITHOUT ANY WARRANTY; without even the implied warranty of
017: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: // GNU General Public License for more details.
019: //
020: // You should have received a copy of the GNU General Public License
021: // along with this program; if not, write to the Free Software
022: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: //
024: // Using this software in any meaning (reading, learning, copying, compiling,
025: // running) means that you agree that the Author(s) is (are) not responsible
026: // for cost, loss of data or any harm that may be caused directly or indirectly
027: // by usage of this softare or this documentation. The usage of this software
028: // is on your own risk. The installation and usage (starting/running) of this
029: // software may allow other people or application to access your computer and
030: // any attached devices and is highly dependent on the configuration of the
031: // software which must be done by the user of the software; the author(s) is
032: // (are) also not responsible for proper configuration and usage of the
033: // software, even if provoked by documentation provided together with
034: // the software.
035: //
036: // Any changes to this file according to the GPL as documented in the file
037: // gpl.txt aside this file in the shipment you received can be done to the
038: // lines that follows this copyright notice here, but changes must not be
039: // done inside the copyright notive above. A re-distribution must contain
040: // the intact and unchanged copyright notice.
041: // Contributions and changes to the program code must be marked as such.
042: package de.anomic.tools;
043:
044: import java.text.DecimalFormat;
045: import java.text.DecimalFormatSymbols;
046: import java.text.NumberFormat;
047: import java.util.Locale;
048:
049: /**
050: * This class provides simple Formatters to unify YaCy's output
051: * formattings.
052: *
053: * At the moment yFormatter can be used to format numbers according
054: * to the locale set for YaCy.
055: */
056: public final class yFormatter {
057: // default formatter
058: private static NumberFormat numForm = NumberFormat
059: .getInstance(new Locale("en"));
060:
061: // generic formatter that can be used when no localized formatting is allowed
062: private static final NumberFormat cleanNumForm = new DecimalFormat(
063: "####.##", new DecimalFormatSymbols(Locale.ENGLISH));
064:
065: static {
066: // just initialize defaults on class load
067: initDefaults();
068: }
069:
070: /**
071: * @param locale the {@link Locale} to set or <code>null</code> to set the special
072: * empty locale to create unformatted numbers
073: */
074: public static void setLocale(Locale locale) {
075: numForm = (locale == null ? cleanNumForm : NumberFormat
076: .getInstance(locale));
077: initDefaults();
078: }
079:
080: /**
081: * @param lang an ISO 639 language code which is used to generate a {@link Locale}
082: */
083: public static void setLocale(String lang) {
084: String l = (lang.equalsIgnoreCase("default") ? "en" : lang
085: .toLowerCase());
086:
087: setLocale(l.equals("none") ? null : new Locale(l));
088: }
089:
090: private static void initDefaults() {
091: numForm.setGroupingUsed(true); // always group int digits
092: numForm.setParseIntegerOnly(false); // allow int/double/float
093: numForm.setMaximumFractionDigits(2); // 2 decimal digits for float/double
094: }
095:
096: public static String number(double d, boolean localized) {
097: return (localized ? number(d) : cleanNumForm.format(d));
098: }
099:
100: public static String number(double d) {
101: return numForm.format(d);
102: }
103:
104: public static String number(long l, boolean localized) {
105: return (localized ? number(l) : cleanNumForm.format(l));
106: }
107:
108: public static String number(long l) {
109: return numForm.format(l);
110: }
111:
112: /**
113: * Method formats String representation of numbers according to the formatting
114: * rules for numbers defined by this class. This method is probably only useful
115: * for "numbers" read from property files.
116: * @param s string to parse into a number and reformat
117: * @return the formatted number as a String or "-" in case of a parsing error
118: */
119: public static String number(String s) {
120: String ret = null;
121: try {
122: if (s.indexOf('.') == -1) {
123: ret = number(Long.parseLong(s));
124: } else {
125: ret = number(Double.parseDouble(s));
126: }
127: } catch (NumberFormatException e) { /* empty */
128: }
129:
130: return (ret == null ? "-" : ret);
131: }
132: }
|