org.divxdede.text.formatter.SimpleFormatter Maven / Gradle / Ivy
/*
* Copyright (c) 2010 INFASS Syst?mes (http://www.infass.com) All rights reserved.
* SimpleFormatter.java is a part of this Commons library
* ====================================================================
*
* Commons 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 3 of the License,
* or any later version.
*
* This 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, see .
*/
package org.divxdede.text.formatter;
/**
* A simple string formatter giving an easy, readable and efficient way to concatenates strings.
*
* This formatter provide a substituion of any %% by the specified argument.
* Each argument's are formatted accordingly their {@link Object#toString()} methods.
*
* Exemple:
*
* String result = SimpleFormatter.format("%% documents sent by %%" , 125 , "Mr. Anderson");
* System.out.println(result);
*
* giving:
*
* 125 documents sent by Mr. Anderson
*
*
* @author Andr? S?bastien (divxdede)
* @since 0.2
*/
public final class SimpleFormatter {
/** private constructor, no instanciation
*/
private SimpleFormatter() {
}
/** Format a {@link String} by replacing each %% occurrences by the specified arguments.
* Each arguments are formatted accordingly to their {@link Object.toString()} methods.
*
* Exemple:
*
* String result = SimpleFormatter.format("Hello Mr. %% !! You take the %% pill" , "Anderson" , "pill");
*
*
* For some performance optimization, this method don't perform all pre-checks call.
* If you give less arguments than %% occurences then this method don't fails and let extra %% occurences as is.
* But in the counter parts, if you provide more argument's than %% occurences, this method will throw an IllegalArgumentException
*
* @param format String used to build the result, each %% are replaced by the specified arguments.
* @param args Subtitue arguments used for replace each %% occurences. The first argument replace the first %%, etc..
*
* @return String result
* @throws IllegalArgumentException If args's length is not equals to the %% occurences.
* @throws NullPointerException if format is null and args is no-empty
*/
public static String format(String format , Object... args ) {
if( args == null || args.length == 0 ) return format;
/** On tranforme les arguments en chaine de caracteres et on determine la taille
*/
String[] strArgs = new String[ args.length ];
int strArgsLength = 0;
for(int index = 0 ; index < args.length ; index++ ) {
strArgs[index] = ( args[index] == null ? "null" : args[index].toString() );
strArgsLength += strArgs[index].length();
}
/** Construction du r?sultat
*/
char[] result = new char[ format.length() + strArgsLength - (2 * args.length) ];
int resultPosition = 0;
int strArgsIndex = 0;
int formatPositionDebut = 0;
int formatPositionFin = 0;
String currentArgs = null;
while( strArgsIndex < args.length ) {
formatPositionFin = format.indexOf("%%",formatPositionDebut);
if( formatPositionFin < 0 ) throw new IllegalArgumentException("Incorrect argument's count");
format.getChars( formatPositionDebut , formatPositionFin , result , resultPosition);
resultPosition += (formatPositionFin - formatPositionDebut);
currentArgs = strArgs[strArgsIndex++];
currentArgs.getChars( 0 , currentArgs.length() , result , resultPosition );
resultPosition += currentArgs.length();
formatPositionDebut = formatPositionFin + 2;
}
if( formatPositionDebut < format.length() ) {
format.getChars( formatPositionDebut , format.length() , result , resultPosition);
}
return new String( result );
}
}