Class Unit

    • Constructor Detail

      • Unit

        Unit​(UnitData data)
        Creates a unit item.
        Parameters:
        data - the data
    • Method Detail

      • getNew

        public static Unit getNew​(DbControl dc,
                                  Quantity quantity,
                                  String displaySymbol)
                           throws BaseException
        Create a new Unit item. The unit will have default values for the reference factor (1.0) and offset (0.0). Use changeReferenceFactorAndOffset(double, double) to change those.
        Parameters:
        dc - The DbControl which will be used for permission checking and database access
        quantity - The quantity the unit belongs to
        displaySymbol - The symbol used when displaying values with this unit
        Returns:
        The new Unit item
        Throws:
        BaseException - If there is an error
      • getQuery

        public static ItemQuery<Unit> getQuery()
        Get a query configured to retrieve units.
        Returns:
        An ItemQuery object
      • isUsed

        public boolean isUsed()
                       throws BaseException
        Check if:
        • Annotation types are using this unit as default unit
        • Annotations are using this unit
        Overrides:
        isUsed in class BasicItem
        Returns:
        TRUE if this item is used, FALSE otherwise
        Throws:
        BaseException - If not able to tell if item is used or not.
        See Also:
        BasicItem.getUsingItems()
      • getType

        public Item getType()
        Description copied from interface: Identifiable
        Get the type of item represented by the object. The returned value is one of the values defined in the Item enumeration.
        Specified by:
        getType in interface Identifiable
        Returns:
        A value indicating the type of item
      • isRemoved

        public boolean isRemoved()
        Description copied from interface: Removable
        Check if the removed flag is set for this item.
        Specified by:
        isRemoved in interface Removable
        Returns:
        TRUE if the item is flagged as removed, FALSE otherwise
      • getName

        public String getName()
        Description copied from interface: Nameable
        Get the name of the item.
        Specified by:
        getName in interface Nameable
        Returns:
        A String with the name of the item
      • getDescription

        public String getDescription()
        Description copied from interface: Nameable
        Get the description for the item.
        Specified by:
        getDescription in interface Nameable
        Returns:
        A String with a description of the item
      • getQuantity

        public Quantity getQuantity()
        Get the quantity this unit belongs to.
      • setQuantity

        private void setQuantity​(Quantity quantity)
      • getDisplaySymbol

        public String getDisplaySymbol()
        Get the display symbol to use when displaying values with this unit.
        Returns:
        A String object
      • getSymbols

        public Set<String> getSymbols()
        Get all symbols that are registered for this unit.
        Returns:
        All symbols as a set
      • setSymbols

        public void setSymbols​(Set<String> symbols)
        Set the symbols that should be registered for this unit. Note that all units must be unique within a given Quantity. Symbols that are not in the given set will be unregistered except for the display symbol.
        Parameters:
        symbols - A set with the symbols
      • addSymbol

        public void addSymbol​(String symbol)
        Add a symbol to this unit.
        Parameters:
        symbol - The symbol to add
      • removeSymbol

        public void removeSymbol​(String symbol)
        Remove a symbol from this unit. It is not possible to remove the display symbol.
        Parameters:
        symbol - The symbol to remove
      • changeReferenceFactorAndOffset

        public void changeReferenceFactorAndOffset​(double factor,
                                                   double offset)
        Changes the factor and offset used for converting values in this unit into the reference unit. Calling this method with values that are different from the old ones will result in a re-calculation of all annotation values that has used this unit to make sure that the actual values stays the same.
        Parameters:
        factor - The new factor
        offset - The new offset
      • changeReferenceFactorAndOffset

        static void changeReferenceFactorAndOffset​(SessionControl sc,
                                                   Session session,
                                                   UnitData unit,
                                                   double factor,
                                                   double offset)
      • toReferenceUnit

        public double toReferenceUnit​(double value)
        Convert a value from this unit to the reference unit.
        Parameters:
        value - The value in this unit
        Returns:
        The value converted to the reference unit
      • fromReferenceUnit

        public double fromReferenceUnit​(double value)
        Convert a value from the reference unit to this unit.
        Parameters:
        value - The reference value
        Returns:
        The value converted to this unit
      • toUnit

        public double toUnit​(double value,
                             Unit other)
        Convert a value from this unit to another unit. This method is equivalent to calling other.fromReferenceUnit(this.toReferenceUnit(value)).
        Parameters:
        value - The value in this unit
        other - The other unit
        Returns:
        The value converted to the other unit
      • fromOther

        public double fromOther​(double value,
                                Unit other)
        Convert a value from another unit to this unit. This method is equivalent to calling this.fromReferenceUnit(other.toReferenceUnit(value))
        Parameters:
        value - The value in the other unit
        other - The other unit
        Returns:
        The value converted to this unit
      • getFormatter

        public <T> Formatter<T> getFormatter​(Formatter<T> parent)
        Create a formatter for formatting values with this unit. The returned formatter delegates the formatting of the values to a parent formatter and uses a PrefixSuffixFormatter to append the unit symbol.
        Parameters:
        parent - The parent formatter for formatting the actual values
        Returns:
        The new formatter
      • getUnitConverter

        public UnitConverter getUnitConverter​(Unit other)
        Create a unit converter for converting from the reference unit (other) to this unit.
        Parameters:
        other - The reference unit
        Returns:
        A unit converter
        Since:
        3.4
      • findOtherUnit

        UnitData findOtherUnit​(Session session,
                               String symbol)
        Find the unit with a given symbol that has the same quantity as this unit.
        Returns:
        The unit, or null if no unit is found