Class SymbolMap


  • public final class SymbolMap
    extends java.lang.Object

    This class provides a set of mappings between units and symbols (both ways), between prefixes and symbols (both ways), and from unit converters to prefixes (one way). No attempt is made to verify the uniqueness of the mappings.

    Mappings are read from a ResourceBundle, the keys of which should consist of a fully-qualified class name, followed by a dot ('.'), and then the name of a static field belonging to that class, followed optionally by another dot and a number. If the trailing dot and number are not present, the value associated with the key is treated as a label, otherwise if the trailing dot and number are present, the value is treated as an alias. Aliases map from String to Unit only, whereas labels map in both directions. A given unit may have any number of aliases, but may have only one label.

    Version:
    1.7, February 25, 2017
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<javax.measure.UnitConverter,​Prefix> converterToPrefix  
      private static java.util.logging.Logger logger  
      private java.util.Map<java.lang.Object,​java.lang.String> prefixToSymbol  
      private java.util.Map<java.lang.String,​java.lang.Object> symbolToPrefix  
      private java.util.Map<java.lang.String,​javax.measure.Unit<?>> symbolToUnit  
      private java.util.Map<javax.measure.Unit<?>,​java.lang.String> unitToSymbol  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SymbolMap()
      Creates an empty mapping.
      private SymbolMap​(java.util.ResourceBundle rb)
      Creates a symbol map from the specified resource bundle,
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void alias​(javax.measure.Unit<?> unit, java.lang.String symbol)
      Attaches an alias to the specified unit.
      Prefix getPrefix​(java.lang.String symbol)
      Returns the prefix (if any) for the specified symbol.
      Prefix getPrefix​(javax.measure.UnitConverter converter)
      Returns the prefix for the specified converter.
      java.lang.String getSymbol​(javax.measure.Unit<?> unit)
      Returns the symbol (label) for the specified unit.
      java.lang.String getSymbol​(Prefix prefix)
      Returns the symbol for the specified prefix.
      javax.measure.Unit<?> getUnit​(java.lang.String symbol)
      Returns the unit for the specified symbol.
      void label​(javax.measure.Unit<?> unit, java.lang.String symbol)
      Attaches a label to the specified unit.
      void label​(Prefix prefix, java.lang.String symbol)
      Attaches a label to the specified prefix.
      static SymbolMap of​(java.util.ResourceBundle rb)
      Creates a symbol map from the specified resource bundle,
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • logger

        private static final java.util.logging.Logger logger
      • symbolToUnit

        private final java.util.Map<java.lang.String,​javax.measure.Unit<?>> symbolToUnit
      • unitToSymbol

        private final java.util.Map<javax.measure.Unit<?>,​java.lang.String> unitToSymbol
      • symbolToPrefix

        private final java.util.Map<java.lang.String,​java.lang.Object> symbolToPrefix
      • prefixToSymbol

        private final java.util.Map<java.lang.Object,​java.lang.String> prefixToSymbol
      • converterToPrefix

        private final java.util.Map<javax.measure.UnitConverter,​Prefix> converterToPrefix
    • Constructor Detail

      • SymbolMap

        private SymbolMap()
        Creates an empty mapping.
      • SymbolMap

        private SymbolMap​(java.util.ResourceBundle rb)
        Creates a symbol map from the specified resource bundle,
        Parameters:
        rb - the resource bundle.
    • Method Detail

      • of

        public static SymbolMap of​(java.util.ResourceBundle rb)
        Creates a symbol map from the specified resource bundle,
        Parameters:
        rb - the resource bundle.
      • label

        public void label​(javax.measure.Unit<?> unit,
                          java.lang.String symbol)
        Attaches a label to the specified unit. For example:
        symbolMap.label(DAY.multiply(365), "year"); symbolMap.label(US.FOOT, "ft");
        Parameters:
        unit - the unit to label.
        symbol - the new symbol for the unit.
      • alias

        public void alias​(javax.measure.Unit<?> unit,
                          java.lang.String symbol)
        Attaches an alias to the specified unit. Multiple aliases may be attached to the same unit. Aliases are used during parsing to recognize different variants of the same unit. symbolMap.alias(US.FOOT, "foot"); symbolMap.alias(US.FOOT, "feet"); symbolMap.alias(Units.METER, "meter"); symbolMap.alias(Units.METER, "metre");
        Parameters:
        unit - the unit to label.
        symbol - the new symbol for the unit.
      • label

        public void label​(Prefix prefix,
                          java.lang.String symbol)
        Attaches a label to the specified prefix. For example:
        symbolMap.label(MetricPrefix.GIGA, "G"); symbolMap.label(MetricPrefix.MICRO, "ยต"); TODO should be able to do this with a generic Prefix
      • getUnit

        public javax.measure.Unit<?> getUnit​(java.lang.String symbol)
        Returns the unit for the specified symbol.
        Parameters:
        symbol - the symbol.
        Returns:
        the corresponding unit or null if none.
      • getSymbol

        public java.lang.String getSymbol​(javax.measure.Unit<?> unit)
        Returns the symbol (label) for the specified unit.
        Parameters:
        unit - the corresponding symbol.
        Returns:
        the corresponding symbol or null if none.
      • getPrefix

        public Prefix getPrefix​(java.lang.String symbol)
        Returns the prefix (if any) for the specified symbol.
        Parameters:
        symbol - the unit symbol.
        Returns:
        the corresponding prefix or null if none.
      • getPrefix

        public Prefix getPrefix​(javax.measure.UnitConverter converter)
        Returns the prefix for the specified converter.
        Parameters:
        converter - the unit converter.
        Returns:
        the corresponding prefix or null if none.
      • getSymbol

        public java.lang.String getSymbol​(Prefix prefix)
        Returns the symbol for the specified prefix.
        Parameters:
        prefix - the prefix.
        Returns:
        the corresponding symbol or null if none.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object