001 /**
002 * Copyright (c) 2011, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.base.io;
029
030
031 import java.text.DecimalFormat;
032 import java.text.DecimalFormatSymbols;
033 import java.util.Locale;
034
035
036 /**
037 * Create an instance of Locale.US {@code DecimalFormat}s through static factory
038 * methods.
039 *
040 * @author nikitin
041 *
042 * @version 1.0
043 *
044 */
045 public final class DecimalFormatFactory {
046
047 /** exponent value */
048 public static final int DEFAULT_PRECISION = 2;
049
050 private static final DecimalFormatSymbols US_SYMBOLS =
051 new DecimalFormatSymbols(Locale.US);
052
053 private static final DecimalFormat DEFAULT_DECIMAL_FORMAT =
054 valueOf(DEFAULT_PRECISION);
055
056 public static final DecimalFormat getInstance() {
057 return DEFAULT_DECIMAL_FORMAT;
058 }
059
060 public static final DecimalFormat valueOf(String pattern) {
061 return new DecimalFormat(pattern, US_SYMBOLS);
062 }
063
064 public static final DecimalFormat valueOf(int precision) {
065 return valueOf(createDecimalPattern(1, precision));
066 }
067
068 public static final DecimalFormat valueOf(int integerPart,
069 int fractionalPart) {
070 return valueOf(createDecimalPattern(integerPart, fractionalPart));
071 }
072
073 /**
074 * @return the defaultPrecision
075 */
076 public static int getDefaultPrecision() {
077 return DEFAULT_PRECISION;
078 }
079
080 /**
081 * Make a string format for decimal printing.
082 *
083 * @param iPart the integer part digits
084 * @param fPart fractional part digits
085 * @return a string format.
086 */
087 private static String createDecimalPattern(int iPart, int fPart) {
088 StringBuilder sb = new StringBuilder();
089
090 for (int i = 0; i < iPart; i++) {
091 sb.append("0");
092 }
093
094 if (fPart > 0) {
095 sb.append(".");
096 }
097
098 for (int i = 0; i < fPart; i++) {
099 sb.append("0");
100 }
101
102 return sb.toString();
103 }
104
105 }