public class FormatUtils extends Object
Modifier and Type | Field and Description |
---|---|
private static double |
BYTES_IN_GIGABYTE |
private static double |
BYTES_IN_KILOBYTE |
private static double |
BYTES_IN_MEGABYTE |
private static double |
BYTES_IN_TERABYTE |
private static String |
DAYS |
private static String |
HOURS |
private static String |
MILLIS |
private static String |
MINS |
private static String |
NANOS |
private static String |
SECS |
static Pattern |
TIME_DURATION_PATTERN |
static String |
TIME_DURATION_REGEX |
private static List<Long> |
TIME_UNIT_MULTIPLIERS |
private static String |
UNION |
private static String |
VALID_TIME_UNITS |
private static String |
WEEKS |
Constructor and Description |
---|
FormatUtils() |
Modifier and Type | Method and Description |
---|---|
protected static long |
calculateMultiplier(TimeUnit originalTimeUnit,
TimeUnit newTimeUnit)
Returns the numerical multiplier to convert a value from
originalTimeUnit to
newTimeUnit (i.e. |
protected static TimeUnit |
determineTimeUnit(String rawUnit)
Returns the
TimeUnit enum that maps to the provided raw String input. |
static String |
formatCount(long count)
Formats the specified count by adding commas.
|
static String |
formatDataSize(double dataSize)
Formats the specified data size in human readable format.
|
static String |
formatHoursMinutesSeconds(long sourceDuration,
TimeUnit sourceUnit)
Formats the specified duration in 'HH:mm:ss.SSS' format.
|
static String |
formatMinutesSeconds(long sourceDuration,
TimeUnit sourceUnit)
Formats the specified duration in 'mm:ss.SSS' format.
|
static String |
formatNanos(long nanos,
boolean includeTotalNanos)
Formats nanoseconds in the format:
3 seconds, 8 millis, 3 nanos - if includeTotalNanos = false,
3 seconds, 8 millis, 3 nanos (3008000003 nanos) - if includeTotalNanos = true
|
static String |
formatUtilization(double utilization) |
static double |
getPreciseTimeDuration(String value,
TimeUnit desiredUnit)
Returns the parsed and converted input in the requested units.
|
protected static TimeUnit |
getSmallerTimeUnit(TimeUnit originalUnit)
Returns the next smallest
TimeUnit (i.e. |
static long |
getTimeDuration(String value,
TimeUnit desiredUnit)
Deprecated.
As of Apache NiFi 1.9.0, because this method only returns whole numbers, use
getPreciseTimeDuration(String, TimeUnit) when possible. |
protected static boolean |
isWeek(String rawUnit)
Returns
true if this raw unit String is parsed as representing "weeks", which does not have a value in the TimeUnit enum. |
private static String |
join(String delimiter,
String... values) |
protected static List<Object> |
makeWholeNumberTime(double decimal,
TimeUnit timeUnit)
Converts the provided time duration value to one that can be represented as a whole number.
|
private static String |
pad2Places(long val) |
private static String |
pad3Places(long val) |
private static final String UNION
private static final double BYTES_IN_KILOBYTE
private static final double BYTES_IN_MEGABYTE
private static final double BYTES_IN_GIGABYTE
private static final double BYTES_IN_TERABYTE
private static final String NANOS
private static final String MILLIS
private static final String SECS
private static final String MINS
private static final String HOURS
private static final String DAYS
private static final String WEEKS
private static final String VALID_TIME_UNITS
public static final String TIME_DURATION_REGEX
public static final Pattern TIME_DURATION_PATTERN
public static String formatCount(long count)
count
- the value to add commas topublic static String formatMinutesSeconds(long sourceDuration, TimeUnit sourceUnit)
sourceDuration
- the duration to formatsourceUnit
- the unit to interpret the durationpublic static String formatHoursMinutesSeconds(long sourceDuration, TimeUnit sourceUnit)
sourceDuration
- the duration to formatsourceUnit
- the unit to interpret the durationprivate static String pad2Places(long val)
private static String pad3Places(long val)
public static String formatDataSize(double dataSize)
dataSize
- Data size in bytes@Deprecated public static long getTimeDuration(String value, TimeUnit desiredUnit)
getPreciseTimeDuration(String, TimeUnit)
when possible.TimeUnit
after parsing the String
input. If the resulting value is a decimal (i.e.
25 hours -> TimeUnit.DAYS = 1.04
), the value is rounded.value
- the raw String input (i.e. "28 minutes")desiredUnit
- the requested output TimeUnit
public static double getPreciseTimeDuration(String value, TimeUnit desiredUnit)
If the value is 0 <= x < 1
in the provided units, the units will first be converted to a smaller unit to get a value >= 1 (i.e. 0.5 seconds -> 500 milliseconds).
This is because the underlying unit conversion cannot handle decimal values.
If the value is x >= 1
but x is not a whole number, the units will first be converted to a smaller unit to attempt to get a whole number value (i.e. 1.5 seconds -> 1500 milliseconds).
If the value is x < 1000
and the units are TimeUnit.NANOSECONDS
, the result will be a whole number of nanoseconds, rounded (i.e. 123.4 ns -> 123 ns).
This method handles decimal values over 1 ns
, but < 1 ns
will return 0
in any other unit.
Examples:
"10 seconds", TimeUnit.MILLISECONDS
-> 10_000.0
"0.010 s", TimeUnit.MILLISECONDS
-> 10.0
"0.010 s", TimeUnit.SECONDS
-> 0.010
"0.010 ns", TimeUnit.NANOSECONDS
-> 1
"0.010 ns", TimeUnit.MICROSECONDS
-> 0
value
- the String
inputdesiredUnit
- the desired output TimeUnit
protected static List<Object> makeWholeNumberTime(double decimal, TimeUnit timeUnit)
List
containing the new value as a long
at index 0 and the
TimeUnit
at index 1. If the incoming value is already whole, it is returned as is.
If the incoming value cannot be made whole, a whole approximation is returned. For values
>= 1 TimeUnit.NANOSECONDS
, the value is rounded (i.e. 123.4 ns -> 123 ns).
For values < 1 TimeUnit.NANOSECONDS
, the constant [1L, TimeUnit.NANOSECONDS
] is returned as the smallest measurable unit of time.
Examples:
1, TimeUnit.SECONDS
-> [1, TimeUnit.SECONDS
]
1.1, TimeUnit.SECONDS
-> [1100, TimeUnit.MILLISECONDS
]
0.1, TimeUnit.SECONDS
-> [100, TimeUnit.MILLISECONDS
]
0.1, TimeUnit.NANOSECONDS
-> [1, TimeUnit.NANOSECONDS
]
decimal
- the time duration as a decimaltimeUnit
- the current time unitlong
) and the smaller time unit usedprotected static long calculateMultiplier(TimeUnit originalTimeUnit, TimeUnit newTimeUnit)
originalTimeUnit
to
newTimeUnit
(i.e. for TimeUnit.DAYS -> TimeUnit.MINUTES
would return
24 * 60 = 1440). If the original and new units are the same, returns 1. If the new unit
is larger than the original (i.e. the result would be less than 1), throws an
IllegalArgumentException
.originalTimeUnit
- the source time unitnewTimeUnit
- the destination time unitprotected static TimeUnit getSmallerTimeUnit(TimeUnit originalUnit)
TimeUnit
(i.e. TimeUnit.DAYS -> TimeUnit.HOURS
).
If the parameter is null
or TimeUnit.NANOSECONDS
, an
IllegalArgumentException
is thrown because there is no valid smaller TimeUnit.originalUnit
- the TimeUnitprotected static boolean isWeek(String rawUnit)
true
if this raw unit String
is parsed as representing "weeks", which does not have a value in the TimeUnit
enum.rawUnit
- the String containing the desired unitprotected static TimeUnit determineTimeUnit(String rawUnit)
TimeUnit
enum that maps to the provided raw String
input. The
highest time unit is TimeUnit.DAYS
. Any input that cannot be parsed will result in
an IllegalArgumentException
.rawUnit
- the String to parsepublic static String formatUtilization(double utilization)
public static String formatNanos(long nanos, boolean includeTotalNanos)
nanos
- the number of nanoseconds to formatincludeTotalNanos
- whether or not to include the total number of nanoseconds in parentheses in the returned valueCopyright © 2021 Apache NiFi Project. All rights reserved.