|
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.
|
|
From: <fd...@us...> - 2007-08-29 21:11:22
|
Revision: 3438
http://jnode.svn.sourceforge.net/jnode/?rev=3438&view=rev
Author: fduminy
Date: 2007-08-29 14:11:15 -0700 (Wed, 29 Aug 2007)
Log Message:
-----------
fixed bug with NumberUtils.size (http://www.jnode.org/node/969)
Modified Paths:
--------------
trunk/core/src/core/org/jnode/util/NumberUtils.java
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-08-29 20:40:14 UTC (rev 3437)
+++ trunk/core/src/core/org/jnode/util/NumberUtils.java 2007-08-29 21:11:15 UTC (rev 3438)
@@ -213,17 +213,16 @@
* @return the text for of the size
*/
public static String size(long v) {
- SizeUnit prevUnit = SizeUnit.MIN;
for(SizeUnit unit : SizeUnit.values())
{
- if (v < unit.getMultiplier())
+ if ((v < unit.getMultiplier()) || SizeUnit.MAX.equals(unit))
{
- return String.valueOf(v) + prevUnit.getUnit();
+ return String.valueOf(v) + unit.getUnit();
}
+
v = v >>> 10;
- prevUnit = unit;
}
- return String.valueOf(v) + prevUnit.getUnit();
+ return String.valueOf(v) + SizeUnit.MAX.getUnit();
}
/**
Modified: trunk/core/src/core/org/jnode/util/SizeUnit.java
===================================================================
--- trunk/core/src/core/org/jnode/util/SizeUnit.java 2007-08-29 20:40:14 UTC (rev 3437)
+++ trunk/core/src/core/org/jnode/util/SizeUnit.java 2007-08-29 21:11:15 UTC (rev 3438)
@@ -37,5 +37,8 @@
return unit;
}
-
+ public String toString()
+ {
+ return multiplier + ", " + unit;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ls...@us...> - 2007-08-31 20:11:06
|
Revision: 3440
http://jnode.svn.sourceforge.net/jnode/?rev=3440&view=rev
Author: lsantha
Date: 2007-08-31 13:11:03 -0700 (Fri, 31 Aug 2007)
Log Message:
-----------
Added support for standard decimal (metric or SI) and binary prefixes.
Added Paths:
-----------
trunk/core/src/core/org/jnode/util/BinaryPrefix.java
trunk/core/src/core/org/jnode/util/DecimalPrefix.java
Added: trunk/core/src/core/org/jnode/util/BinaryPrefix.java
===================================================================
--- trunk/core/src/core/org/jnode/util/BinaryPrefix.java (rev 0)
+++ trunk/core/src/core/org/jnode/util/BinaryPrefix.java 2007-08-31 20:11:03 UTC (rev 3440)
@@ -0,0 +1,62 @@
+/*
+ * $Id$
+ */
+package org.jnode.util;
+
+import org.jnode.vm.annotation.SharedStatics;
+
+@SharedStatics
+public enum BinaryPrefix {
+ B(1l, ""),
+ 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 BinaryPrefix MIN = B;
+ public static final BinaryPrefix MAX = E;
+
+ final private long multiplier;
+ final private String unit;
+
+ private BinaryPrefix(long multiplier, String unit)
+ {
+ this.multiplier = multiplier;
+ this.unit = unit;
+ }
+
+ public long getMultiplier() {
+ return multiplier;
+ }
+
+ public String getUnit() {
+ return unit;
+ }
+
+ public String toString()
+ {
+ return multiplier + ", " + unit;
+ }
+
+ /**
+ * Convert the given value to a size string like 64K
+ * @param v the size to convert
+ * @return the text for of the size
+ */
+ public static String apply(long v) {
+ for (BinaryPrefix unit : values()) {
+ if ((v < 1024) && (v > 0)) {
+ return String.valueOf(v) + unit.getUnit();
+ }
+
+ v = v >>> 10;
+ }
+ return String.valueOf(v >>> 10) + MAX.getUnit();
+ }
+}
Added: trunk/core/src/core/org/jnode/util/DecimalPrefix.java
===================================================================
--- trunk/core/src/core/org/jnode/util/DecimalPrefix.java (rev 0)
+++ trunk/core/src/core/org/jnode/util/DecimalPrefix.java 2007-08-31 20:11:03 UTC (rev 3440)
@@ -0,0 +1,62 @@
+/*
+ * $Id$
+ */
+package org.jnode.util;
+
+import org.jnode.vm.annotation.SharedStatics;
+
+@SharedStatics
+public enum DecimalPrefix {
+ B(1l, ""),
+ K(1000l, "k"),
+ M(1000l*1000l, "M"),
+ G(1000l*1000l*1000l, "G"),
+ T(1000l*1000l*1000l*1000l, "T"),
+ P(1000l*1000l*1000l*1000l*1000l, "P"),
+ E(1000l*1000l*1000l*1000l*1000l*1000l, "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 DecimalPrefix MIN = B;
+ public static final DecimalPrefix MAX = E;
+
+ final private long multiplier;
+ final private String unit;
+
+ private DecimalPrefix(long multiplier, String unit)
+ {
+ this.multiplier = multiplier;
+ this.unit = unit;
+ }
+
+ public long getMultiplier() {
+ return multiplier;
+ }
+
+ public String getUnit() {
+ return unit;
+ }
+
+ public String toString()
+ {
+ return multiplier + ", " + unit;
+ }
+
+ /**
+ * Convert the given value to a size string like 64K
+ * @param v the size to convert
+ * @return the text for of the size
+ */
+ public static String apply(long v) {
+ for (DecimalPrefix unit : values()) {
+ if ((v < 1000l) && (v > 0l)) {
+ return String.valueOf(v) + unit.getUnit();
+ }
+
+ v = v / 1000l;
+ }
+ return String.valueOf(v / 1000l) + MAX.getUnit();
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ls...@us...> - 2007-08-31 20:25:59
|
Revision: 3441
http://jnode.svn.sourceforge.net/jnode/?rev=3441&view=rev
Author: lsantha
Date: 2007-08-31 13:25:54 -0700 (Fri, 31 Aug 2007)
Log Message:
-----------
Added support for standard decimal (metric or SI) and binary prefixes.
Modified Paths:
--------------
trunk/core/src/core/org/jnode/util/BinaryPrefix.java
trunk/core/src/core/org/jnode/util/DecimalPrefix.java
trunk/core/src/core/org/jnode/util/NumberUtils.java
trunk/core/src/core/org/jnode/util/SizeUnit.java
Modified: trunk/core/src/core/org/jnode/util/BinaryPrefix.java
===================================================================
--- trunk/core/src/core/org/jnode/util/BinaryPrefix.java 2007-08-31 20:11:03 UTC (rev 3440)
+++ trunk/core/src/core/org/jnode/util/BinaryPrefix.java 2007-08-31 20:25:54 UTC (rev 3441)
@@ -51,7 +51,7 @@
*/
public static String apply(long v) {
for (BinaryPrefix unit : values()) {
- if ((v < 1024) && (v > 0)) {
+ if ((v < 1024) && (v >= 0)) {
return String.valueOf(v) + unit.getUnit();
}
Modified: trunk/core/src/core/org/jnode/util/DecimalPrefix.java
===================================================================
--- trunk/core/src/core/org/jnode/util/DecimalPrefix.java 2007-08-31 20:11:03 UTC (rev 3440)
+++ trunk/core/src/core/org/jnode/util/DecimalPrefix.java 2007-08-31 20:25:54 UTC (rev 3441)
@@ -51,7 +51,7 @@
*/
public static String apply(long v) {
for (DecimalPrefix unit : values()) {
- if ((v < 1000l) && (v > 0l)) {
+ if ((v < 1000l) && (v >= 0l)) {
return String.valueOf(v) + unit.getUnit();
}
Modified: trunk/core/src/core/org/jnode/util/NumberUtils.java
===================================================================
--- trunk/core/src/core/org/jnode/util/NumberUtils.java 2007-08-31 20:11:03 UTC (rev 3440)
+++ trunk/core/src/core/org/jnode/util/NumberUtils.java 2007-08-31 20:25:54 UTC (rev 3441)
@@ -211,10 +211,11 @@
* Convert the given value to a size string like 64K
* @param v the size to convert
* @return the text for of the size
+ * @deprecated use toDeciamByte() or toBinaryByte() instead
*/
public static String size(long v) {
for (SizeUnit unit : SizeUnit.values()) {
- if ((v < 1024) && (v > 0)) {
+ if ((v < 1024) && (v >= 0)) {
return String.valueOf(v) + unit.getUnit();
}
@@ -222,6 +223,25 @@
}
return String.valueOf(v >>> 10) + SizeUnit.MAX.getUnit();
}
+
+ /**
+ * Convert the given value to a size string like 64K
+ * @param v the size to convert
+ * @return the text for of the size
+ */
+ public static String toDeciamByte(long v) {
+ return DecimalPrefix.apply(v) + "B";
+ }
+
+ /**
+ * Convert the given value to a size string like 64K
+ * @param v the size to convert
+ * @return the text for of the size
+ */
+ public static String toBinaryByte(long v) {
+ return BinaryPrefix.apply(v) + "B";
+ }
+
/**
*
Modified: trunk/core/src/core/org/jnode/util/SizeUnit.java
===================================================================
--- trunk/core/src/core/org/jnode/util/SizeUnit.java 2007-08-31 20:11:03 UTC (rev 3440)
+++ trunk/core/src/core/org/jnode/util/SizeUnit.java 2007-08-31 20:25:54 UTC (rev 3441)
@@ -3,6 +3,9 @@
import java.math.BigInteger;
import org.jnode.vm.annotation.SharedStatics;
+/**
+ * @deprecated use DecimalPrefix or BinaryPrefix instead.
+ */
@SharedStatics
public enum SizeUnit {
B(1l, "B"),
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|