01: /*
02: * MCS Media Computer Software
03: * Copyright (c) 2006 by MCS
04: * --------------------------------------
05: * Created on 20.03.2006 by w.klaas
06: *
07: * Licensed under the Apache License, Version 2.0 (the "License");
08: * you may not use this file except in compliance with the License.
09: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: package de.mcs.jmeasurement;
20:
21: import java.text.DecimalFormat;
22: import java.text.NumberFormat;
23:
24: /**
25: * This class is for converting memory values to GUI representable strings.
26: *
27: * @author w.klaas
28: */
29: public final class MemoryHelper {
30:
31: /** constant for conversing B -> kb.. */
32: private static final int CONVERSION_CONSTANT = 1024;
33:
34: /** formatting nnumbers. */
35: private static NumberFormat numberFormat;
36:
37: /** prevent instancing. */
38: private MemoryHelper() {
39: }
40:
41: /**
42: * converting a memory value into a GUI representable string.
43: *
44: * @param freeMemory
45: * long value for the meory
46: * @return the gui representation like 1,23 KB
47: */
48: public static String toGUIString(final long freeMemory) {
49: if (numberFormat == null) {
50: numberFormat = DecimalFormat.getInstance();
51: numberFormat.setMaximumFractionDigits(2);
52: numberFormat.setMinimumFractionDigits(2);
53: }
54: long bytes = freeMemory;
55: double mem = (double) freeMemory;
56: String result = "";
57: if (bytes > CONVERSION_CONSTANT) {
58: mem = bytes / (double) CONVERSION_CONSTANT;
59: result = numberFormat.format(mem) + " kB";
60: bytes = bytes / CONVERSION_CONSTANT;
61: }
62: if (bytes > CONVERSION_CONSTANT) {
63: mem = bytes / (double) CONVERSION_CONSTANT;
64: result = numberFormat.format(mem) + " MB";
65: bytes = bytes / CONVERSION_CONSTANT;
66: }
67: if (bytes > CONVERSION_CONSTANT) {
68: mem = bytes / (double) CONVERSION_CONSTANT;
69: result = numberFormat.format(mem) + " GB";
70: bytes = bytes / CONVERSION_CONSTANT;
71: }
72: return result;
73: }
74: }
|