View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.util;
22  
23  import java.util.StringTokenizer;
24  import java.text.DecimalFormat;
25  
26  /***
27   * TODO Add docs
28   * Created on 04.12.2004
29   *
30   * Contributing Author(s):
31   *
32   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
34   *
35   * @author vlads
36   * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/15 05:09:26 $
37   */
38  public class StringUtil {
39  	/***
40  	 * jre 1.3 compatible replaceAll.
41  	 * @param str text to search and replace in
42  	 * @param replace the String to search for
43  	 * @param replacement the String to replace with
44  	 * @return the text with any replacements processed
45  	 */
46  	public static String replaceAll(String str, String replace, String replacement)
47  	{
48  		StringBuffer sb = new StringBuffer(str);
49  		int firstOccurrence = str.indexOf(replace);
50  
51  		while (firstOccurrence != -1)
52  		{
53  			sb.replace(firstOccurrence, firstOccurrence + replace.length(), replacement);
54  			firstOccurrence = sb.toString().indexOf(replace);
55  		}
56  
57  		return sb.toString();
58  	}
59  
60      /***
61       * DOCUMENT ME!
62       *
63       * @param str DOCUMENT ME!
64       * @param delim DOCUMENT ME!
65       *
66       * @return DOCUMENT ME!
67       */
68      private String[] stringToArray(String str, String delim) {
69          StringTokenizer paramStrokenizer = new StringTokenizer(str, delim);
70      
71          String[] retVal = new String[paramStrokenizer.countTokens()];
72          int i = 0;
73      
74          while (paramStrokenizer.hasMoreTokens()) {
75              retVal[i++] = paramStrokenizer.nextToken();
76          }
77      
78          return retVal;
79      }
80      
81      public static String percent(double duration, double durationTotal) {
82  		if (durationTotal == 0.0) {
83  			return "n/a";
84  		}
85  		final DecimalFormat format = new DecimalFormat("##0");
86  		format.setMinimumIntegerDigits(1);
87  		format.setMaximumFractionDigits(0);
88  				
89  		int prc = (new Double(Math.floor(100 * duration/durationTotal))).intValue();
90  		return format.format(prc);
91  	}
92  }