Class UnitConverter


  • public class UnitConverter
    extends java.lang.Object
    UnitConverter is a class that converts units to other units It is used to convert units in the Ingredients and Recipe ingredients
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) java.util.HashMap<java.lang.String,​java.lang.Double> ingredientDensityMap
      The ingredient density map is a map of ingredient names to their densities in g/mL
      (package private) java.util.ArrayList<java.lang.String> units
      The units ArrayList is an ArrayList of all the units that are supported by the app
    • Constructor Summary

      Constructors 
      Constructor Description
      UnitConverter​(android.content.Context context)
      The UnitConverter constructor initializes the ingredientDensityMap and units ArrayList
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      android.util.Pair<java.lang.Double,​java.lang.String> availableUnits​(Ingredient ingredient)
      Converts ingredients to mass if possible
      Unit build​(java.lang.String unit)
      build method builds a unit from a string
      java.lang.Double convert​(double value, Unit from, Unit to)
      The convert method converts a quantity from one unit to another unit
      java.lang.Double convert​(double value, java.lang.String ingredient, Unit from, Unit to)
      The convert method converts a quantity from one unit to another unit
      android.util.Pair<java.lang.Double,​java.lang.String> convertToSmallest​(java.lang.String unitString, java.lang.Double value)
      Convert the given unit to the smallest unit in the unit system
      java.lang.String format​(double value, java.lang.String ingredient, Unit from, Unit to)
      The format method formats a quantity to a specified unit
      java.util.ArrayList<java.lang.String> getUnits()
      Gets all of the units being used, out of count/mass/volume
      java.util.ArrayList<Unit> getUnitsLike​(Unit unit)
      getUnitsLike method gets all the units that are like the specified unit
      android.util.Pair<java.lang.Double,​Unit> scaleUnit​(double value, Unit unit)
      the scaleUnit method scales a unit to a specified unit
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ingredientDensityMap

        java.util.HashMap<java.lang.String,​java.lang.Double> ingredientDensityMap
        The ingredient density map is a map of ingredient names to their densities in g/mL
      • units

        java.util.ArrayList<java.lang.String> units
        The units ArrayList is an ArrayList of all the units that are supported by the app
    • Constructor Detail

      • UnitConverter

        public UnitConverter​(android.content.Context context)
        The UnitConverter constructor initializes the ingredientDensityMap and units ArrayList
        Parameters:
        context - the context of the app
    • Method Detail

      • convert

        public java.lang.Double convert​(double value,
                                        java.lang.String ingredient,
                                        Unit from,
                                        Unit to)
        The convert method converts a quantity from one unit to another unit
        Parameters:
        value - the quantity to convert
        ingredient - the ingredient to convert
        from - the unit to convert from
        to - the unit to convert to
        Returns:
        the converted quantity
      • convert

        public java.lang.Double convert​(double value,
                                        Unit from,
                                        Unit to)
        The convert method converts a quantity from one unit to another unit
        Parameters:
        value - the quantity to convert
        from - the unit to convert from
        to - the unit to convert to
        Returns:
        the converted quantity
      • format

        public java.lang.String format​(double value,
                                       java.lang.String ingredient,
                                       Unit from,
                                       Unit to)
        The format method formats a quantity to a specified unit
        Parameters:
        value - the quantity to format
        ingredient - the ingredient to format
        from - the unit to format from
        to - the unit to format to
        Returns:
        the formatted quantity
      • scaleUnit

        public android.util.Pair<java.lang.Double,​Unit> scaleUnit​(double value,
                                                                        Unit unit)
        the scaleUnit method scales a unit to a specified unit
        Parameters:
        value - the quantity to scale
        unit - the unit to scale
        Returns:
        the scaled unit
      • build

        public Unit build​(java.lang.String unit)
        build method builds a unit from a string
        Parameters:
        unit - the string to build the unit from
        Returns:
        the built unit
      • getUnitsLike

        public java.util.ArrayList<Unit> getUnitsLike​(Unit unit)
        getUnitsLike method gets all the units that are like the specified unit
        Parameters:
        unit - the unit to get units like
        Returns:
        the units that are like the specified unit
      • getUnits

        public java.util.ArrayList<java.lang.String> getUnits()
        Gets all of the units being used, out of count/mass/volume
        Returns:
        an arraylist of all units
      • convertToSmallest

        public android.util.Pair<java.lang.Double,​java.lang.String> convertToSmallest​(java.lang.String unitString,
                                                                                            java.lang.Double value)
        Convert the given unit to the smallest unit in the unit system
        Parameters:
        unitString - the unit to convert
        value - the value of the unit
        Returns:
        the converted unit
      • availableUnits

        public android.util.Pair<java.lang.Double,​java.lang.String> availableUnits​(Ingredient ingredient)
        Converts ingredients to mass if possible
        Parameters:
        ingredient - assumes that ingredient has been converted using convertToSmallest(String, Double)
        Returns:
        the converted ingredient