1 20 package org.enhydra.barracuda.plankton; 21 22 import org.apache.log4j.*; 23 24 import java.util.*; 25 26 30 public class StringUtil { 31 32 protected static final Logger logger = Logger.getLogger(StringUtil.class.getName()); 34 35 43 public static String replace(String sourceStr, String oldPattern, String newPattern) { 44 if (sourceStr!=null && sourceStr.length()<1) return sourceStr; String 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 _replace(String sourceStr, String oldPattern, String newPattern) { 53 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 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 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 87 public static String getElapsedStr(long elapsed) { 88 return getElapsedStr(elapsed, Calendar.MILLISECOND); 89 } 90 91 97 public static String 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 sb = new StringBuffer (100); 104 String 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 |