Class Currency

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable

    public class Currency
    extends GeneratedCurrency
    The hybris Platform currency item.

    Currencies are used mainly for pricing purpose. For this reason the specified in SessionContext.getCurrency() of the current JaloSession defines the default Currency of this session. All pricing information will use this Currency by default.

    Some examples for the use of Currency items:

    Furthermore currencies offer convenience methods for price formatting and conversion. If you want to format a price according to its currency's digits you got to do it like this:
          PriceValue pv = ...;
          String priceStr = pv.getCurrency().formatPrice( pv.getValue() );
     

    Rounding a price is also provided by this class:

          Currency c = ...;
          c.setDigits( 3 );
          // now c rounds any value to 3 digits
          double rounded = c.round( 1.234567 );
     

    Converting a value between two currencies can be done this way:

          Currency source = ... , target = ... ;
          source.setConversionFactor( 2 );
          target.setConversionFactor( 1 );
          target.setDigits( 1 );
          // now convert from source to target value * 1 / 2
          double result = source.convert( target , 4.44444 );
          // result should be 2.2 , since conversion does rounding to 1 target digit
     
    See Also:
    Serialized Form
    • Constructor Detail

      • Currency

        public Currency()
    • Method Detail

      • createItem

        protected Item createItem​(SessionContext ctx,
                                  ComposedType type,
                                  Item.ItemAttributeMap allAttributes)
                           throws JaloBusinessException
        Description copied from class: Item
        Has to be implemented for each concrete subtype of item. This method is responsible for creating a new item instance ( by calling managers, ejb homes, etc. ) during ComposedType.newInstance(Map).

        In case this method uses any of the attribute values during creation it is required to override Item.getNonInitialAttributes(SessionContext, ItemAttributeMap) too.
        Sn example:

        
         public static final String MY_ATTRIBUTE = "someAttribute"; ... protected Item createItem(SessionContext
         ctx, ComposedType type, Map allAttributes ) throws JaloBusinessException { MyManager man = ... return
         man.createMyItem( (String)allAttributes.get(MY_ATTRIBUTE) );
         // here MY_ATTRIBUTE is used for creation, so it must not be set again } protected Map getNonInitialAttributes(
         SessionContext ctx, Map allAttributes ) { // let superclass remove its own initial attributes Map ret =
         super.getNonInitialAttributes( ctx, allAttributes );
         // remove MY_ATTRIBUTE from all attributes since if has already been set ret.remove(MY_ATTRIBUTE); return ret; }
        
         
        Overrides:
        createItem in class GenericItem
        Parameters:
        ctx - the current session context which this item is created within
        type - the actual item type ( since subtypes may not provide a own jalo class this may be different from the type which this method was implemented for )
        Returns:
        the new item instance
        Throws:
        JaloBusinessException - indicates an error during creation - any changes will be rollbacked
      • getJavaCurrency

        public java.util.Currency getJavaCurrency()
        Since:
        3.0 M2
      • matchJavaCurrency

        protected java.util.Currency matchJavaCurrency​(java.lang.String code)
      • setBase

        public void setBase()
        Set this Currency as the base currency of this system.
      • convertAndRound

        @Deprecated
        public double convertAndRound​(Currency targetCurrency,
                                      double value)
        Converts a (price) value of this currency into a value for the specified target currency and rounds the result according to the target currency's digits.
        Parameters:
        targetCurrency - the target Currency
        value - the value which should be converted
        Returns:
        the converted and rounded value
      • convert

        @Deprecated
        public double convert​(Currency targetCurrency,
                              double value)
        Deprecated.
        Converts a (price) value of this currency into a value of the specified target currency. The result is not rounded.
        Parameters:
        targetCurrency - the target Currency
        value - the value which should be converted
        Returns:
        the converted value
      • setDigits

        public void setDigits​(SessionContext ctx,
                              java.lang.Integer digits)
        Changes the number of digits used by this currency. This attribute affects rounding of price values for this Currency globally!
        Overrides:
        setDigits in class GeneratedCurrency
        Parameters:
        ctx - the SessionContext which will be used
        digits - the new number of digits used by this currency
      • round

        @Deprecated
        public double round​(double value)
        Deprecated.
        Rounds a price value according to this currency's number of digits.
        Parameters:
        value - the currency value which should be rounded.
        Returns:
        the rounded currency value
      • adjustDigits

        @Deprecated
        public static java.text.DecimalFormat adjustDigits​(java.text.DecimalFormat nf,
                                                           Currency c)
        Deprecated.
        since ages
        Since:
        3.0 M2
      • adjustSymbol

        @Deprecated
        public static java.text.DecimalFormat adjustSymbol​(java.text.DecimalFormat nf,
                                                           Currency c)
        Deprecated.
        since ages
        Since:
        3.0 M2
      • formatPrice

        @Deprecated
        public java.lang.String formatPrice​(double price)
        Deprecated.
        since ages
        Formats the specified price value according to this currency's properties (includes rounding). Please note that the result string will not contain the currency symbol - use format(double) instead!
        Parameters:
        price - the price value which should be formated
        Returns:
        the formated price value
      • formatPrice

        @Deprecated
        public java.lang.String formatPrice​(double price,
                                            java.text.NumberFormat numberFormat)
        Deprecated.
        since ages
        Formats the specified price value according to this currency's properties (includes rounding) and uses the given NumberFormat.
        Parameters:
        price - the price value which should be formatted
        numberFormat - the java.text.NumberFormat for formatting the price
        Returns:
        the formatted price value
        Since:
        1.3.1
      • format

        @Deprecated
        public java.lang.String format​(double price,
                                       java.text.NumberFormat numberFormat)
        Deprecated.
        since ages - seeUtilities#getCurrencyInstance()
        Formats a price according using a specified number format. The currency will modify the minimum and maximum digits to reflect its number of digits. In addition it will try to set its symbol as currency symbol of the given number format, which will of course only work if it's a currency format.
        Parameters:
        numberFormat - the specified number format
        price - the price value
        See Also:
        #format(double, Locale), format(double, NumberFormat), Utilities#getCurrencyInstance(), Utilities.getCurrencyInstance(Currency), Utilities.getCurrencyInstance(Currency, Locale)
      • getConversionFactor

        @Deprecated
        public double getConversionFactor()
        Deprecated.
        since ages - useGeneratedCurrency.getConversion() instead
        The conversion factor of this currency. This factor denotes the virtual value of this currency in respect to the base currency (whose conversion factor should normally be 1.0 ). Since every currency has one factor you can use the convert(Currency, double) method to convert values between different currencies.
        Returns:
        the conversion factor of the currency.
      • getConversionFactor

        @Deprecated
        public double getConversionFactor​(SessionContext ctx)
        Deprecated.
        since ages - usegetConversion(SessionContext) instead
        The conversion factor of this currency. This factor denotes the virtual value of this currency in respect to the base currency (whose conversion factor should normally be '1' ).

        Since every currency has one factor you can use the convert(Currency, double) method to convert values among them.

        Parameters:
        ctx - the session context can be used for cached acces of this field
        Returns:
        the conversion factor