01: /*
02: * MCS Media Computer Software Copyright (c) 2007 by MCS
03: * -------------------------------------- Created on 23.01.2007 by w.klaas
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
06: * use this file except in compliance with the License. You may obtain a copy of
07: * the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14: * License for the specific language governing permissions and limitations under
15: * the License.
16: */
17: /**
18: *
19: */package de.mcs.utils;
20:
21: /**
22: * This class helps to convert numbers.
23: *
24: * @author w.klaas
25: *
26: */
27: public class NumberHelper {
28:
29: private static final String CONVERT_UNITS = "KMGkmg";
30:
31: private static final String CONVERT_TIMES = "YMDhms";
32:
33: public static int parseInt(final String valueStr) {
34: String newValue = valueStr;
35: int unit = 1;
36: if (CONVERT_UNITS.indexOf(valueStr
37: .charAt(valueStr.length() - 1)) >= 0) {
38: switch (valueStr.charAt(valueStr.length() - 1)) {
39: case 'K':
40: unit = 1024;
41: break;
42: case 'M':
43: unit = 1024 * 1024;
44: break;
45: case 'G':
46: unit = 1024 * 1024 * 1024;
47: break;
48: case 'k':
49: unit = 1000;
50: break;
51: case 'm':
52: unit = 1000 * 1000;
53: break;
54: case 'g':
55: unit = 1000 * 1000 * 1000;
56: break;
57: default:
58: break;
59: }
60: newValue = newValue.substring(0, newValue.length() - 1);
61: }
62: return Integer.parseInt(newValue) * unit;
63: }
64:
65: public static long parseTime(final String valueStr) {
66: String newValue = valueStr;
67: int unit = 1;
68: if (CONVERT_TIMES.indexOf(valueStr
69: .charAt(valueStr.length() - 1)) >= 0) {
70: switch (valueStr.charAt(valueStr.length() - 1)) {
71: case 'D':
72: unit = unit * 24;
73: case 'h':
74: unit = unit * 60;
75: case 'm':
76: unit = unit * 60;
77: case 's':
78: unit = unit * 1000;
79: break;
80: default:
81: break;
82: }
83: newValue = newValue.substring(0, newValue.length() - 1);
84: }
85: return Long.parseLong(newValue) * unit;
86: }
87:
88: }
|