| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StringUtil |
|
| 2.5;2.5 |
| 1 | /* | |
| 2 | * Cobertura - http://cobertura.sourceforge.net/ | |
| 3 | * | |
| 4 | * Copyright (C) 2005 Jeremy Thomerson | |
| 5 | * | |
| 6 | * Note: This file is dual licensed under the GPL and the Apache | |
| 7 | * Source License (so that it can be used from both the main | |
| 8 | * Cobertura classes and the ant tasks). | |
| 9 | * | |
| 10 | * Cobertura is free software; you can redistribute it and/or modify | |
| 11 | * it under the terms of the GNU General Public License as published | |
| 12 | * by the Free Software Foundation; either version 2 of the License, | |
| 13 | * or (at your option) any later version. | |
| 14 | * | |
| 15 | * Cobertura is distributed in the hope that it will be useful, but | |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 18 | * General Public License for more details. | |
| 19 | * | |
| 20 | * You should have received a copy of the GNU General Public License | |
| 21 | * along with Cobertura; if not, write to the Free Software | |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
| 23 | * USA | |
| 24 | */ | |
| 25 | ||
| 26 | package net.sourceforge.cobertura.util; | |
| 27 | ||
| 28 | import java.text.NumberFormat; | |
| 29 | ||
| 30 | /** | |
| 31 | * Abstract, not to be instantiated utility class for String functions. | |
| 32 | * | |
| 33 | * @author Jeremy Thomerson | |
| 34 | */ | |
| 35 | 0 | public abstract class StringUtil |
| 36 | { | |
| 37 | ||
| 38 | /** | |
| 39 | * <p> | |
| 40 | * Replaces all instances of "replace" with "with" from the "original" | |
| 41 | * string. | |
| 42 | * </p> | |
| 43 | * | |
| 44 | * <p> | |
| 45 | * NOTE: it is known that a similar function is included in jdk 1.4 as replaceAll(), | |
| 46 | * but is written here so as to allow backward compatibility to users using SDK's | |
| 47 | * prior to 1.4 | |
| 48 | * </p> | |
| 49 | * | |
| 50 | * @param original The original string to do replacement on. | |
| 51 | * @param replace The string to replace. | |
| 52 | * @param with The string to replace "replace" with. | |
| 53 | * @return The replaced string. | |
| 54 | */ | |
| 55 | public static String replaceAll(String original, String replace, String with) | |
| 56 | { | |
| 57 | 482 | if (original == null) |
| 58 | { | |
| 59 | 1 | return original; |
| 60 | } | |
| 61 | ||
| 62 | 481 | final int len = replace.length(); |
| 63 | 481 | StringBuffer sb = new StringBuffer(original.length()); |
| 64 | 481 | int start = 0; |
| 65 | 481 | int found = -1; |
| 66 | ||
| 67 | 663 | while ((found = original.indexOf(replace, start)) != -1) |
| 68 | { | |
| 69 | 182 | sb.append(original.substring(start, found)); |
| 70 | 182 | sb.append(with); |
| 71 | 182 | start = found + len; |
| 72 | 182 | } |
| 73 | ||
| 74 | 481 | sb.append(original.substring(start)); |
| 75 | 481 | return sb.toString(); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Takes a double and turns it into a percent string. | |
| 80 | * Ex. 0.5 turns into 50% | |
| 81 | * | |
| 82 | * @param value | |
| 83 | * @return corresponding percent string | |
| 84 | */ | |
| 85 | public static String getPercentValue(double value) | |
| 86 | { | |
| 87 | //moved from HTMLReport.getPercentValue() | |
| 88 | NumberFormat formatter; | |
| 89 | 312 | formatter = NumberFormat.getPercentInstance(); |
| 90 | 312 | return formatter.format(value); |
| 91 | } | |
| 92 | ||
| 93 | } |