Package com.norconex.commons.lang.unit
Class DataUnitFormatter
- java.lang.Object
-
- com.norconex.commons.lang.unit.DataUnitFormatter
-
- All Implemented Interfaces:
Serializable
public class DataUnitFormatter extends Object implements Serializable
Formats a data unit to a human-readable string.
As of 2.0.0, a distinction is made between decimal (default) and binary units. Also, units are now supported up to yotta (10008) or yobi (10248).
- Since:
- 1.4.0
- Author:
- Pascal Essiembre
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DataUnitFormatter()Creates a new DataUnit formatter using decimal notation, with default system locale, and without decimals.DataUnitFormatter(int decimalPrecision)Deprecated.Since 2.0.0 use setter methods.DataUnitFormatter(DataUnitFormatter formatter)Copy constructor.DataUnitFormatter(Locale locale)Deprecated.Since 2.0.0 use setter methods.DataUnitFormatter(Locale locale, int decimalPrecision)Deprecated.Since 2.0.0 use setter methods.DataUnitFormatter(Locale locale, int decimalPrecision, boolean fixedUnit)Deprecated.Since 2.0.0 use setter methods.
-
Method Summary
-
-
-
Constructor Detail
-
DataUnitFormatter
public DataUnitFormatter()
Creates a new DataUnit formatter using decimal notation, with default system locale, and without decimals.
-
DataUnitFormatter
public DataUnitFormatter(DataUnitFormatter formatter)
Copy constructor. Passing anullformatter is the same as invokingDataUnitFormatter()without arguments- Parameters:
formatter- formatter to copy
-
DataUnitFormatter
@Deprecated public DataUnitFormatter(Locale locale)
Deprecated.Since 2.0.0 use setter methods.Creates a new DataUnit formatter without decimals. The decimal and thousand separator symbols are set according to supplied locale.- Parameters:
locale- a locale
-
DataUnitFormatter
@Deprecated public DataUnitFormatter(int decimalPrecision)
Deprecated.Since 2.0.0 use setter methods.Creates a new DataUnit formatter with default system locale with the maximum number of decimals returned when applicable.- Parameters:
decimalPrecision- number of decimals
-
DataUnitFormatter
@Deprecated public DataUnitFormatter(Locale locale, int decimalPrecision)
Deprecated.Since 2.0.0 use setter methods.Creates a new DataUnit formatter with the maximum number of decimals returned when applicable. The decimal and thousand separator symbols are set according to supplied locale.- Parameters:
locale- a localedecimalPrecision- number of decimals
-
DataUnitFormatter
@Deprecated public DataUnitFormatter(Locale locale, int decimalPrecision, boolean fixedUnit)
Deprecated.Since 2.0.0 use setter methods.Creates a new DataUnit formatter with the maximum number of decimals returned when applicable. The decimal and thousand separator symbols are set according to supplied locale. Set thefixedUnittotrueto ensure the data units supplied are not change in the formatting.- Parameters:
locale- a localedecimalPrecision- number of decimalsfixedUnit-trueto keep original unit in formatting
-
-
Method Detail
-
getLocale
public Locale getLocale()
-
setLocale
public DataUnitFormatter setLocale(Locale locale)
-
withLocale
public DataUnitFormatter withLocale(Locale locale)
-
getDecimalPrecision
public int getDecimalPrecision()
-
setDecimalPrecision
public DataUnitFormatter setDecimalPrecision(int decimalPrecision)
-
withDecimalPrecision
public DataUnitFormatter withDecimalPrecision(int decimalPrecision)
-
isFixedUnit
public boolean isFixedUnit()
-
setFixedUnit
public DataUnitFormatter setFixedUnit(boolean fixedUnit)
-
withFixedUnit
public DataUnitFormatter withFixedUnit(boolean fixedUnit)
-
isBinaryNotation
public boolean isBinaryNotation()
-
setBinaryNotation
public DataUnitFormatter setBinaryNotation(boolean binaryNotation)
-
withBinaryNotation
public DataUnitFormatter withBinaryNotation(boolean binaryNotation)
-
getRoundingMode
public RoundingMode getRoundingMode()
-
setRoundingMode
public DataUnitFormatter setRoundingMode(RoundingMode roundingMode)
-
withRoundingMode
public DataUnitFormatter withRoundingMode(RoundingMode roundingMode)
-
format
public String format(double amount, DataUnit unit)
Formats a data amount of the given unit to a human-readable representation.- Parameters:
amount- the amount to formatunit- the data unit type of the amount- Returns:
- formatted string
-
format
public String format(BigDecimal amount, DataUnit unit)
Formats a data amount of the given unit to a human-readable representation.- Parameters:
amount- the amount to formatunit- the data unit type of the amount- Returns:
- formatted string
-
-