/*
 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.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * Since Java 1.5 one should rather use Formatter.format instead of
 * this class. However, this one is a bit more tolerant against wrongly
 * specified input.
 * 

* Formatting argument class that originally served as an adapter to * PrintfFormat. whith a more convenient and shorter syntax. *

* Usage note:
* One should use the static method sprintf and construct a new * FArg instance to hold parameters. Further arguments can be * appended very C-like with a varargs argument, for example: *

* FArg.sprintf("%-13s %- 8.4f", "statistic:", 2.345) * * * @author Markus Kraetzig * @see PrintfFormat */ public final class FArg { // incompatible with String.format: %-s, %i private static final Pattern pattern1 = Pattern .compile(".*\\s*%[-]?\\s*[0-9]*[is].*\\s*"); // incompatible with String.format: %-2.0d private static final Pattern pattern2 = Pattern .compile(".*\\s*%[-]?\\s*[0-9]*\\.[0-9]*d.*\\s*"); private final List list = new ArrayList(); /** * FArg double constructor. * * @param arg * double to be formatted */ public FArg(double arg) { super(); add(arg); } /** * FArg int constructor. * * @param arg * int to be formatted */ public FArg(int arg) { super(); add(arg); } /** * FArg Object constructor. * * @param arg * object to be formatted */ public FArg(Object arg) { super(); add(arg); } /** * Adds a new double argument to be formatted. * * @param arg * the element to be formatted * @return this after adding arg */ public FArg add(double arg) { return add(new Double(arg)); } /** * Adds a new int argument to be formatted. * * @param arg * the element to be formatted * @return this after adding arg */ public FArg add(int arg) { return add(new Integer(arg)); } /** * Adds a new Object argument to be formatted. * * @param arg * the element to be formatted * @return this after adding arg */ public FArg add(Object arg) { list.add(arg); return this; } /** * Gets the stored arguments as an object array that can be used with * PrintfFormat.sprintf. * * @return the object array */ public Object[] getArgs() { return list.toArray(); } /** * The String.format method should be used instead. * * A wrapper method for the PrintfFormat.sprintf method. It is * slightly shorter and that resembles the syntax used by the * com.braju toolset (by exchanging Format and * Parameters with FArg). * * @param formatString * the string for formatting args, see the * PrinfFormat documentation * @param args * the object containing the arguments in the correct order * according to the formatting string * @return the formatted string * * @throws IllegalArgumentException * if (formatString == null || args == null) * @see com.jstatcom.util.PrintfFormat */ public static String sprintf(String formatString, FArg args) { return sprintf(formatString, args.getArgs()); } /** * The String.format method should be used instead. * * A wrapper method for the PrintfFormat.sprintf method with a * convenient vararg argument. * * @param formatString * the string for formatting args, see the * PrinfFormat documentation * @param args * strings or numbers to be formated * @return the formatted string * * @throws IllegalArgumentException * if (formatString == null) * @see com.jstatcom.util.PrintfFormat */ public static String sprintf(String formatString, Object... args) { if (formatString == null) throw new IllegalArgumentException("Argument was null."); // for backwards compatibility if (pattern1.matcher(formatString).matches() || pattern2.matcher(formatString).matches()) return new PrintfFormat(Locale.ENGLISH, formatString).sprintf(args); else return String.format(Locale.ENGLISH, formatString, args); } }

Ads help maintain this website.