KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > StringUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: StringUtil.java,v 1.4 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton;
21
22 import org.apache.log4j.*;
23
24 import java.util.*;
25
26 /**
27  * Simple utility functions that work on Strings
28  * TODO - can't this be relaced by java.lang.String.replaceAll() [jdk 1.4] - A: probably not; that funtion uses regexps; this one doesn't
29  */

30 public class StringUtil {
31
32     //public constants
33
protected static final Logger logger = Logger.getLogger(StringUtil.class.getName());
34
35     /**
36      * Replace all occurences of a pattern in a String with a new pattern
37      *
38      * @param sourceStr the source string
39      * @param oldPattern the old pattern
40      * @param newPattern the new pattern
41      * @return an adjusted String
42      */

43     public static String JavaDoc replace(String JavaDoc sourceStr, String JavaDoc oldPattern, String JavaDoc newPattern) {
44         if (sourceStr!=null && sourceStr.length()<1) return sourceStr; //no way we can match on this, so just return the source str
45
String JavaDoc s = _replace(sourceStr, oldPattern, newPattern);
46         if (s!=null && s.length()<1) {
47             return (newPattern==null ? null : s);
48         } else {
49             return s;
50         }
51     }
52     private static String JavaDoc _replace(String JavaDoc sourceStr, String JavaDoc oldPattern, String JavaDoc newPattern) {
53         //eliminate the obvious
54
if (sourceStr==null) {
55             if (oldPattern==null) return newPattern;
56             else return sourceStr;
57         }
58         else if (oldPattern==null) return sourceStr;
59         if (newPattern==null) newPattern = "";
60
61         //see if the pattern exists
62
int i = sourceStr.indexOf(oldPattern);
63         if (i<0) return sourceStr;
64         else return sourceStr.substring(0,i)+newPattern+_replace(sourceStr.substring(i+oldPattern.length()),oldPattern,newPattern);
65     }
66
67     /**
68      * get a Calendar representing an elapsed amt of time
69      *
70      * @since csc_010404_1
71      */

72     public static Calendar getElapsed(long elapsed) {
73         Calendar cal = Calendar.getInstance();
74         cal.set(Calendar.HOUR_OF_DAY, 0);
75         cal.set(Calendar.MINUTE, 0);
76         cal.set(Calendar.SECOND, 0);
77         cal.set(Calendar.MILLISECOND, 0);
78         cal.setTimeInMillis(cal.getTimeInMillis()+elapsed);
79         return cal;
80     }
81     
82     /**
83      * get a String describing an elapsed amt of time (format: "XX hrs, YY mins, ZZ secs (MMMM millis)")
84      *
85      * @since csc_010404_1
86      */

87     public static String JavaDoc getElapsedStr(long elapsed) {
88         return getElapsedStr(elapsed, Calendar.MILLISECOND);
89     }
90
91     /**
92      * get a String describing an elapsed amt of time (format: "XX hrs, YY mins, ZZ secs").
93      * Granularity are calendar constants MILLISECOND, SECOND, MINUTE, or HOUR.
94      *
95      * @since csc_010404_1
96      */

97     public static String JavaDoc getElapsedStr(long elapsed, int granularity) {
98         if (elapsed==0) return "0 secs";
99         Calendar cal = getElapsed(elapsed);
100         int hrs = cal.get(Calendar.HOUR_OF_DAY);
101         int mins = cal.get(Calendar.MINUTE);
102         int secs = cal.get(Calendar.SECOND);
103         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
104         String JavaDoc sep = "";
105         if (hrs>0) {
106             sb.append(hrs+" hr"+(hrs>1 ? "s" : ""));
107             sep = ", ";
108         }
109         if ((mins>0) && (granularity==Calendar.MINUTE || granularity==Calendar.SECOND || granularity==Calendar.MILLISECOND)) {
110             sb.append(sep+mins+" min"+(mins>1 ? "s" : ""));
111             sep = ", ";
112         }
113         if ((secs>0) && (granularity==Calendar.SECOND || granularity==Calendar.MILLISECOND)) {
114             sb.append(sep+secs+" sec"+(secs>1 ? "s" : ""));
115             sep = ", ";
116         }
117         if (granularity==Calendar.MILLISECOND) {
118             sb.append(" ("+elapsed+" millis)");
119         }
120         return sb.toString();
121     }
122 }
Popular Tags