/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.util;

import java.text.NumberFormat;

import com.jstatcom.model.JSCConstants;

/**
 * A collection of static methods that are related to strings and formatting.
 * 
 * @author Markus Kraetzig 
 */
public final class UString {
    public static final String NAN_STRING = Double.NaN + "";

    public static final String NAN_STRING_ALT = "NA";

    public static final String POSINF_STRING = Double.POSITIVE_INFINITY + "";

    public static final String SIGPOSINF_STRING = "+"
            + Double.POSITIVE_INFINITY + "";

    public static final String NEGINF_STRING = Double.NEGATIVE_INFINITY + "";

    /**
     * Formats number with format if
     * format != null. Otherwise return number as
     * a string. The formatter respects the given precision.
     * 
     * @param number
     *            the double to format
     * @param format
     *            the formatter, can be null
     * @param precision
     *            number of digits right to the decimal point
     * @return a formatted string with number
     */
    public static String formatNumber(double number, NumberFormat format,
            int precision) {
        if (format != null) {
            format.setMaximumFractionDigits(precision);
            format.setMinimumFractionDigits(precision);
            return format.format(number);
        }
        return number + "";
    }

    /**
     * Parses a given string to a Double if possible and returns
     * it. NaN, (+)Infinity, and -Infinity, are recognized. If
     * the given string is empty, NaN is returned.
     * 
     * @param text
     *            a string that should represent a number, scientific notation
     *            is recognized as well
     * 
     * @return the assembled Double or null if
     *         something went wrong
     * @throws IllegalArgumentException
     *             if (text == null)
     */
    public static Double parseToNumber(String text) {

        if (text == null)
            throw new IllegalArgumentException("Argument was null.");

        text = text.trim();

        if (text.length() == 0) // Fast fail.
            return null;

        if (text.equalsIgnoreCase(NAN_STRING)
                || text.equalsIgnoreCase(NAN_STRING_ALT))
            return JSCConstants.NaN;
        if (text.equalsIgnoreCase(POSINF_STRING)
                || text.equalsIgnoreCase(SIGPOSINF_STRING))
            return JSCConstants.POS_INF;
        if (text.equalsIgnoreCase(NEGINF_STRING))
            return JSCConstants.NEG_INF;

        try {
            double val = Double.parseDouble(text);
            return new Double(val);
        } catch (NumberFormatException ex) {
            return null;
        }
    }

    /**
     * This method replaces all occurences of a substring in an
     * original string with a replacement string. It does not work with regular
     * expressions.
     * 
     * @param origString
     *            the string to work on
     * @param substring
     *            must occur completely in origString for the
     *            replacement to work
     * @param replacementString
     *            the string with which substring is replaced
     * 
     * @return a new string with the replacement(s) or 
* origString if any of the parameters is * null, if substring is empty or if * substring does not occur in * origString */ public static String replaceAllSubStrings(String origString, String substring, String replacementString) { if (origString == null || substring == null || replacementString == null) return origString; if (origString.length() == 0 || substring.length() == 0) return origString; int checkIndex = 0; int lastIndexFound = 0; while ((lastIndexFound = origString.indexOf(substring, checkIndex)) >= 0) { origString = replaceSubString(origString, substring, replacementString, checkIndex); checkIndex = lastIndexFound + replacementString.length(); } return origString; } /** * This method replaces the first occurence of a substring in an original * string with a replacement string. It does not work with regular * expressions. * * @param origString * the string to work on * @param substring * must occur completely in origString for the * replacement to work * @param replacementString * the string with which substring is replaced * * @return a new string with the replacement or
* origString if any of the parameters is * null, if substring is empty or if * substring does not occur in * origString */ public static String replaceSubString(String origString, String substring, String replacementString) { return replaceSubString(origString, substring, replacementString, 0); } /** * This method replaces the first occurence of a substring in an original * string with a replacement string, starting from a given position. It does * not work with regular expressions. * * @param origString * the string to work on * @param substring * must occur completely in origString for the * replacement to work * @param replacementString * the string with which substring is replaced * @param pos * the index in origString to start searching for * substring, if < 0 pos is set * to 0 * * @return a new string with the replacement or
* origString if any of the parameters is * null, if substring is empty or if * substring does not occur in * origString or if * pos >= origString.length() */ public static String replaceSubString(String origString, String substring, String replacementString, int pos) { if (origString == null || substring == null || replacementString == null) return origString; if (pos >= origString.length()) return origString; if (pos < 0) pos = 0; if (origString.length() == 0 || substring.length() == 0) return origString; StringBuffer buffer = new StringBuffer(origString); int index = origString.indexOf(substring, pos); if (index < 0) return origString; buffer.delete(index, index + substring.length()); buffer.insert(index, replacementString); return buffer.toString(); } /** * Resturns string with first characted capitalized, rest lowercase. Useful * for names. * * @param s * @return string with first char upper case, rest lowercase or * null if s == null */ public static String capitalizeFirst(String s) { if (s == null) return s; s = s.trim(); if (s.length() < 2) return s.toUpperCase(); s = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); return s; } }

Ads help maintain this website.