001/* 002 * Cobertura - http://cobertura.sourceforge.net/ 003 * 004 * Copyright (C) 2005 Jeremy Thomerson 005 * 006 * Note: This file is dual licensed under the GPL and the Apache 007 * Source License (so that it can be used from both the main 008 * Cobertura classes and the ant tasks). 009 * 010 * Cobertura is free software; you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License as published 012 * by the Free Software Foundation; either version 2 of the License, 013 * or (at your option) any later version. 014 * 015 * Cobertura is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 018 * General Public License for more details. 019 * 020 * You should have received a copy of the GNU General Public License 021 * along with Cobertura; if not, write to the Free Software 022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 023 * USA 024 */ 025 026package net.sourceforge.cobertura.util; 027 028import java.text.NumberFormat; 029 030/** 031 * Abstract, not to be instantiated utility class for String functions. 032 * 033 * @author Jeremy Thomerson 034 */ 035public abstract class StringUtil 036{ 037 038 /** 039 * <p> 040 * Replaces all instances of "replace" with "with" from the "original" 041 * string. 042 * </p> 043 * 044 * <p> 045 * NOTE: it is known that a similar function is included in jdk 1.4 as replaceAll(), 046 * but is written here so as to allow backward compatibility to users using SDK's 047 * prior to 1.4 048 * </p> 049 * 050 * @param original The original string to do replacement on. 051 * @param replace The string to replace. 052 * @param with The string to replace "replace" with. 053 * @return The replaced string. 054 */ 055 public static String replaceAll(String original, String replace, String with) 056 { 057 if (original == null) 058 { 059 return original; 060 } 061 062 final int len = replace.length(); 063 StringBuffer sb = new StringBuffer(original.length()); 064 int start = 0; 065 int found = -1; 066 067 while ((found = original.indexOf(replace, start)) != -1) 068 { 069 sb.append(original.substring(start, found)); 070 sb.append(with); 071 start = found + len; 072 } 073 074 sb.append(original.substring(start)); 075 return sb.toString(); 076 } 077 078 /** 079 * Takes a double and turns it into a percent string. 080 * Ex. 0.5 turns into 50% 081 * 082 * @param value 083 * @return corresponding percent string 084 */ 085 public static String getPercentValue(double value) 086 { 087 //moved from HTMLReport.getPercentValue() 088 value = Math.floor(value * 100) / 100; //to represent 199 covered lines from 200 as 99% covered, not 100 % 089 return NumberFormat.getPercentInstance().format(value); 090 } 091 092}