|
From: <fd...@us...> - 2007-02-11 22:12:21
|
Revision: 3116
http://jnode.svn.sourceforge.net/jnode/?rev=3116&view=rev
Author: fduminy
Date: 2007-02-11 14:12:18 -0800 (Sun, 11 Feb 2007)
Log Message:
-----------
for size computation :
- created SizeUnit enum
- added more units (that can fit in a long value)
Modified Paths:
--------------
trunk/core/src/core/org/jnode/util/NumberUtils.java
Added Paths:
-----------
trunk/core/src/core/org/jnode/util/SizeUnit.java
Modified: trunk/core/src/core/org/jnode/util/NumberUtils.java
===================================================================
--- trunk/core/src/core/org/jnode/util/NumberUtils.java 2007-02-10 21:22:09 UTC (rev 3115)
+++ trunk/core/src/core/org/jnode/util/NumberUtils.java 2007-02-11 22:12:18 UTC (rev 3116)
@@ -25,12 +25,6 @@
* @author epr
*/
public class NumberUtils {
-
- public static final int K = 1024;
- public static final int M = 1024*1024;
- public static final int G = 1024*1024*1024;
- public static final long T = 1024l*1024l*1024l*1024l;
-
/**
* Convert a float to a string with a given maximum number of fraction digits.
* @param value
@@ -219,61 +213,57 @@
* @return the text for of the size
*/
public static String size(long v) {
- // Is < 1Kb?
- if (v < K) {
- return String.valueOf(v) + "B";
- }
- // Is < 1Mb?
- v = v >>> 10;
- if (v < K) {
- return String.valueOf(v) + "KB";
- }
- // Is < 1Gb?
- v = v >>> 10;
- if (v < K) {
- return String.valueOf(v) + "MB";
- }
- // Is < 1Tb?
- v = v >>> 10;
- if (v < K) {
- return String.valueOf(v) + "GB";
- }
- // Large...
- v = v >>> 10;
- return String.valueOf(v) + "TB";
+ SizeUnit prevUnit = SizeUnit.MIN;
+ for(SizeUnit unit : SizeUnit.values())
+ {
+ if (v < unit.getMultiplier())
+ {
+ return String.valueOf(v) + prevUnit.getUnit();
+ }
+ v = v >>> 10;
+ prevUnit = unit;
+ }
+ return String.valueOf(v) + prevUnit.getUnit();
}
/**
*
- * @param size a number eventually followed by a multiplier (K: Kilobytes, M: Megabytes, G:Gigabytes)
+ * @param size a number eventually followed by a multiplier (K: Kilobytes, M: Megabytes, G:Gigabytes, ...)
* @return the numeric value of the size
*/
public static long getSize(String size) {
if((size == null) || size.trim().equals(""))
return 0;
-
- long multiplier = 1;
- if(size.endsWith("B"))
- size = size.substring(0, size.length() - 1);
-
- if(size.endsWith("T")) {
- multiplier = T;
- size = size.substring(0, size.length() - 1);
- } else if(size.endsWith("G")) {
- multiplier = G;
- size = size.substring(0, size.length() - 1);
- } else if(size.endsWith("M")) {
- multiplier = M;
- size = size.substring(0, size.length() - 1);
- } else if(size.endsWith("K")) {
- multiplier = K;
- size = size.substring(0, size.length() - 1);
+ size = size.trim();
+ long multiplier = SizeUnit.MIN.getMultiplier();
+ SizeUnit sizeUnit = getSizeUnit(size);
+ if(sizeUnit != null)
+ {
+ multiplier = sizeUnit.getMultiplier();
+ size = size.substring(0, size.length() - sizeUnit.getUnit().length());
}
return Long.parseLong(size) * multiplier;
}
-
+
+ public static SizeUnit getSizeUnit(String size) {
+ if((size == null) || size.trim().equals(""))
+ return null;
+
+ size = size.trim();
+ for(SizeUnit unit : SizeUnit.values())
+ {
+ String unitStr = unit.getUnit();
+ if(size.endsWith(unitStr))
+ {
+ return unit;
+ }
+ }
+
+ return null;
+ }
+
/**
* This method avoids the use on Integer.toHexString, since this class may be used
* during the boot-phase when the Integer class in not yet initialized.
Added: trunk/core/src/core/org/jnode/util/SizeUnit.java
===================================================================
--- trunk/core/src/core/org/jnode/util/SizeUnit.java (rev 0)
+++ trunk/core/src/core/org/jnode/util/SizeUnit.java 2007-02-11 22:12:18 UTC (rev 3116)
@@ -0,0 +1,39 @@
+package org.jnode.util;
+
+import java.math.BigInteger;
+
+public enum SizeUnit {
+ B(1l, "B"),
+ K(1024l, "K"),
+ M(1024l*1024l, "M"),
+ G(1024l*1024l*1024l, "G"),
+ T(1024l*1024l*1024l*1024l, "T"),
+ P(1024l*1024l*1024l*1024l*1024l, "P"),
+ E(1024l*1024l*1024l*1024l*1024l*1024l, "E");
+ //these units have too big multipliers to fit in a long
+ // (aka they are greater than 2^64) :
+ //Z(1024l*1024l*1024l*1024l*1024l*1024l*1024l, "Z"),
+ //Y(1024l*1024l*1024l*1024l*1024l*1024l*1024l*1024l, "Y");
+
+ public static final SizeUnit MIN = B;
+ public static final SizeUnit MAX = E;
+
+ final private long multiplier;
+ final private String unit;
+
+ private SizeUnit(long multiplier, String unit)
+ {
+ this.multiplier = multiplier;
+ this.unit = unit;
+ }
+
+ public long getMultiplier() {
+ return multiplier;
+ }
+
+ public String getUnit() {
+ return unit;
+ }
+
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|