1 28 29 package com.idaremedia.antx.helpers; 30 31 import java.io.PrintWriter ; 32 import java.text.FieldPosition ; 33 import java.text.Format ; 34 import java.text.ParsePosition ; 35 import java.util.Date ; 36 37 49 50 public class DurationFormat extends Format 51 { 52 static final long MS_PER_HOUR = 60L * 60L * 1000L; 53 static final long MS_PER_MIN = 60L * 1000L; 54 static final long MS_PER_SEC = 1000L; 55 56 59 public static final String NO_DURATION = "--"; 60 61 62 63 66 public DurationFormat() 67 { 68 super(); 69 SPC=""; 70 hash_= getClass().getName().hashCode()+SPC.hashCode(); 71 } 72 73 74 75 78 public DurationFormat(String spc) 79 { 80 super(); 81 if (spc==null) { throw new IllegalArgumentException ("non-null spacer"); } 82 SPC=spc; 83 hash_= getClass().getName().hashCode()+SPC.hashCode(); 84 } 85 86 87 88 96 public StringBuffer format(long duration, boolean inc_ms, StringBuffer sb) 97 { 98 if (duration>0) { 99 int nf=0; 100 long field = duration / MS_PER_HOUR; 101 long ms_gone = field * MS_PER_HOUR; 102 if (field>0) { 103 sb.append(field); 104 sb.append("hr"); 105 nf++; 106 } 107 field = (duration - ms_gone) / MS_PER_MIN; 108 ms_gone += field * MS_PER_MIN; 109 if (field>0) { 110 if (nf>0) {sb.append(SPC);} 111 sb.append(field); 112 sb.append("min"); 113 nf++; 114 } 115 field = (duration - ms_gone) / MS_PER_SEC; 116 ms_gone += field * MS_PER_SEC; 117 if (field>0) { 118 if (nf>0) {sb.append(SPC);} 119 sb.append(field); 120 sb.append("sec"); 121 nf++; 122 } 123 field = duration - ms_gone; 124 if (inc_ms && field>0) { 125 if (nf>0) {sb.append(SPC);} 126 sb.append(field); 127 sb.append("ms"); 128 nf++; 129 } 130 } else { 131 sb.append(NO_DURATION); 132 } 133 return sb; 134 } 135 136 137 138 145 public void format(long duration, boolean inc_ms, PrintWriter w) 146 { 147 if (duration>0) { 148 int nf=0; 149 long field = duration / MS_PER_HOUR; 150 long ms_gone = field * MS_PER_HOUR; 151 if (field>0) { 152 w.print(field); 153 w.print("hr"); 154 nf++; 155 } 156 field = (duration - ms_gone) / MS_PER_MIN; 157 ms_gone += field * MS_PER_MIN; 158 if (field>0) { 159 if (nf>0) {w.print(SPC);} 160 w.print(field); 161 w.print("min"); 162 nf++; 163 } 164 field = (duration - ms_gone) / MS_PER_SEC; 165 ms_gone += field * MS_PER_SEC; 166 if (field>0) { 167 if (nf>0) {w.print(SPC);} 168 w.print(field); 169 w.print("sec"); 170 nf++; 171 } 172 field = duration - ms_gone; 173 if (inc_ms && field>0) { 174 if (nf>0) {w.print(SPC);} 175 w.print(field); 176 w.print("ms"); 177 nf++; 178 } 179 } else { 180 w.print(NO_DURATION); 181 } 182 } 183 184 185 186 189 public final String format(long duration, boolean inc_ms) 190 { 191 return format(duration,inc_ms,new StringBuffer (50)).toString(); 192 } 193 194 195 196 201 public StringBuffer format(Object obj, StringBuffer sb, FieldPosition fp) 202 { 203 long duration= -1L; 204 if (obj instanceof Number ) { 205 duration = ((Number )obj).longValue(); 206 } 207 else if (obj instanceof Date ) { 208 duration = ((Date )obj).getTime(); 209 } 210 else { 211 throw new IllegalArgumentException ("Unable to convert non-Number"); 212 } 213 return format(duration,true,new StringBuffer (50)); 214 } 215 216 217 218 222 public Object parseObject(String str, ParsePosition pp) 223 { 224 throw new UnsupportedOperationException (); 225 } 226 227 228 229 233 public Object parseObject(String str) 234 { 235 throw new UnsupportedOperationException (); 236 } 237 238 239 240 243 public boolean equals(Object o) 244 { 245 if (o==this) { 246 return true; 247 } 248 if (o==null) { 249 return false; 250 } 251 if (o.getClass()==getClass()) { 252 DurationFormat df = (DurationFormat)o; 253 return SPC.equals(df.SPC); 254 } 255 return false; 256 } 257 258 259 260 263 public int hashCode() 264 { 265 return hash_; 266 } 267 268 269 270 273 public static final DurationFormat INSTANCE = new DurationFormat("."); 274 275 276 277 280 public static final DurationFormat getInstance() 281 { 282 return INSTANCE; 283 } 284 285 286 287 private final String SPC; 288 private final int hash_; 289 } 290 291 292 | Popular Tags |