Package psiprobe.tools
Class SizeExpression
- java.lang.Object
-
- psiprobe.tools.SizeExpression
-
public final class SizeExpression extends Object
Tool for parsing and formatting SI-prefixed numbers in base-2 and base-10.
-
-
Field Summary
Fields Modifier and Type Field Description static long
MULTIPLIER_10
The Constant MULTIPLIER_10.static long
MULTIPLIER_2
The Constant MULTIPLIER_2.static char
PREFIX_GIGA
The Constant PREFIX_GIGA.static char
PREFIX_KILO
The Constant PREFIX_KILO.static char
PREFIX_MEGA
The Constant PREFIX_MEGA.static char
PREFIX_PETA
The Constant PREFIX_PETA.static char
PREFIX_TERA
The Constant PREFIX_TERA.static String
UNIT_BASE
The Constant UNIT_BASE.
-
Constructor Summary
Constructors Modifier Constructor Description private
SizeExpression()
Prevent Instantiation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
format(long value, int decimalPlaces, boolean base2)
Formats the value as an expression.private static double
multiplier(char unitPrefix, boolean base2)
Returns the base-2 or base-10 multiplier for a given prefix.static long
parse(String expression)
Parses the given expression into a numerical value.private static double
round(double value, int decimalPlaces)
Rounds a decimal value to the given decimal place.
-
-
-
Field Detail
-
MULTIPLIER_2
public static final long MULTIPLIER_2
The Constant MULTIPLIER_2.- See Also:
- Constant Field Values
-
MULTIPLIER_10
public static final long MULTIPLIER_10
The Constant MULTIPLIER_10.- See Also:
- Constant Field Values
-
UNIT_BASE
public static final String UNIT_BASE
The Constant UNIT_BASE.- See Also:
- Constant Field Values
-
PREFIX_KILO
public static final char PREFIX_KILO
The Constant PREFIX_KILO.- See Also:
- Constant Field Values
-
PREFIX_MEGA
public static final char PREFIX_MEGA
The Constant PREFIX_MEGA.- See Also:
- Constant Field Values
-
PREFIX_GIGA
public static final char PREFIX_GIGA
The Constant PREFIX_GIGA.- See Also:
- Constant Field Values
-
PREFIX_TERA
public static final char PREFIX_TERA
The Constant PREFIX_TERA.- See Also:
- Constant Field Values
-
PREFIX_PETA
public static final char PREFIX_PETA
The Constant PREFIX_PETA.- See Also:
- Constant Field Values
-
-
Method Detail
-
parse
public static long parse(String expression)
Parses the given expression into a numerical value.An expression has three parts:
Size Summary Table Name Description Base Number (Required) The mantissa or significand of the expression. This can include decimal values. Prefix (Optional) The SI prefix. These span from K for kilo- to P for peta-. Unit (Optional) If the unit "B" (for bytes) is provided, the prefix is treated as base-2 (1024). Otherwise, it uses base-10 (1000). Note: Whitespace may or may not exist between the Base Number and Prefix. Examples:
- "2k" returns
2000
- "3.5m" returns
3500000
- "2kb" returns
2048
- "3.5mb" returns
3670016
- Parameters:
expression
- the expression to parse- Returns:
- the parsed value
- Throws:
NumberFormatException
- if the given expression cannot be parsed
- "2k" returns
-
format
public static String format(long value, int decimalPlaces, boolean base2)
Formats the value as an expression.- Parameters:
value
- the numerical value to be formatteddecimalPlaces
- the number of decimal places in the mantissabase2
- whether to use the base-2 (1024) multiplier and format with "B" units. If false, uses the base-10 (1000) multiplier and no units.- Returns:
- a formatted string expression of the value
-
round
private static double round(double value, int decimalPlaces)
Rounds a decimal value to the given decimal place.- Parameters:
value
- the value to rounddecimalPlaces
- the number of decimal places to preserve.- Returns:
- the rounded value
-
multiplier
private static double multiplier(char unitPrefix, boolean base2)
Returns the base-2 or base-10 multiplier for a given prefix.- Parameters:
unitPrefix
- the character representing the prefix. Can be K, M, G, T, or P.base2
- whether to use the base-2 (1024) multiplier. If false, uses the base-10 (1000) multiplier.- Returns:
- the multiplier for the given prefix
-
-