Class Percentage

  • All Implemented Interfaces:
    Serializable

    public final class Percentage
    extends Object
    implements Serializable
    Represents a coverage percentage value which can be used in order to show and serialize coverage values. The class can also be used for transforming a coverage fraction into its percentage representation. The percentage is represented by a numerator and a denominator.
    Author:
    Florian Orendi
    See Also:
    Serialized Form
    • Field Detail

      • ZERO

        public static final Percentage ZERO
        null value.
    • Method Detail

      • valueOf

        public static Percentage valueOf​(org.apache.commons.lang3.math.Fraction fraction)
        Creates an instance of Percentage in the range [0,100] from a fraction within the range [0,1]. I.e., a percentage is a fraction value multiplied by one hundert.
        Parameters:
        fraction - the value as a fraction within the range [0,1]
        Returns:
        the created instance
      • valueOf

        public static Percentage valueOf​(int items,
                                         int total)
        Creates an instance of Percentage from the two number items and total. The percentage is calculated as value (items / total) * 100.
        Parameters:
        items - the number of items in the range [0,total]
        total - the total number of items available
        Returns:
        the created instance
        Throws:
        IllegalArgumentException - if the denominator is zero or items are greater than total
      • valueOf

        public static Percentage valueOf​(String stringRepresentation)
        Creates a new Percentage instance from the provided string representation. The string representation is expected to contain the numerator and the denominator - separated by a slash, e.g. "300/345", or "1/100". Whitespace characters will be ignored.
        Parameters:
        stringRepresentation - string representation to convert from
        Returns:
        the created Percentage
        Throws:
        IllegalArgumentException - if the string is not a valid Percentage instance
      • toDouble

        public double toDouble()
        Returns this percentage as a double value in the interval [0, 100].
        Returns:
        the coverage percentage
      • formatPercentage

        public String formatPercentage​(Locale locale)
        Formats a percentage to plain text and rounds the value to two decimals.
        Parameters:
        locale - the used locale
        Returns:
        the formatted percentage as plain text
      • subtract

        public org.apache.commons.lang3.math.Fraction subtract​(Percentage subtrahend)
        Subtracts the other percentage from this percentage, returning the result as a Fraction.
        Parameters:
        subtrahend - the percentage to subtract
        Returns:
        a Fraction instance with the resulting values
      • formatDeltaPercentage

        public String formatDeltaPercentage​(Locale locale)
        Formats a delta percentage to its plain text representation with a leading sign and rounds the value to two decimals.
        Parameters:
        locale - the used locale
        Returns:
        the formatted delta percentage as plain text with a leading sign
      • getItems

        public int getItems()
      • getTotal

        public int getTotal()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • serializeToString

        public String serializeToString()
        Returns a string representation for this Percentage that can be used to serialize this instance in a simple but still readable way. The serialization contains the numerator and the denominator - separated by a slash, e.g. "100/345", or "0/1".
        Returns:
        a string representation for this Percentage