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. |