Mercurial > hg > blitz_condensed
comparison src/org/dancres/util/NumUtil.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.util; | |
2 | |
3 import java.util.regex.Matcher; | |
4 import java.util.regex.Pattern; | |
5 | |
6 /** | |
7 */ | |
8 public class NumUtil { | |
9 private static final String GB = "g"; | |
10 private static final String MB = "m"; | |
11 private static final String KB = "k"; | |
12 | |
13 public static long convertToBytes(String theDbCacheSizeValue) | |
14 throws IllegalArgumentException { | |
15 | |
16 String format = "\\d\\s*[" + GB + MB + KB + "]?"; | |
17 | |
18 // check format | |
19 // | |
20 Pattern pattern = Pattern.compile(format, Pattern.CASE_INSENSITIVE); | |
21 Matcher matcher = pattern.matcher(theDbCacheSizeValue); | |
22 if (!matcher.find()) | |
23 throw new IllegalArgumentException("Could not parse " + | |
24 theDbCacheSizeValue + | |
25 ". Should be of the form '0-9[k|m|g]'"); | |
26 | |
27 // grab the number piece | |
28 // | |
29 String splitOnUOM = "[kKmMgG]"; | |
30 String[] splitOnUOMArr = theDbCacheSizeValue.split(splitOnUOM); | |
31 long size = Long.parseLong(splitOnUOMArr[0].trim()); | |
32 | |
33 // grab the unit of measure piece | |
34 // | |
35 String uom = theDbCacheSizeValue.substring(splitOnUOMArr[0].length()); | |
36 if (uom.length() > 0) | |
37 uom = uom.substring(0, 1); | |
38 | |
39 // convert the size to bytes | |
40 // | |
41 long sizeInBytes = size; | |
42 if (KB.equalsIgnoreCase(uom)) | |
43 sizeInBytes = size * 1024; | |
44 else if (MB.equalsIgnoreCase(uom)) | |
45 sizeInBytes = size * 1024 * 1024; | |
46 else if (GB.equalsIgnoreCase(uom)) | |
47 sizeInBytes = size * 1024 * 1024 * 1024; | |
48 else | |
49 sizeInBytes = size; | |
50 | |
51 return sizeInBytes; | |
52 } | |
53 | |
54 public static void main(String anArgs[]) throws Exception { | |
55 System.out.println(convertToBytes(anArgs[0])); | |
56 } | |
57 } |