001/*
002 * Copyright (c) 2012, 2015, Anatole Tresch, Werner Keil and others by the @author tag.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016package org.javamoney.moneta.convert;
017
018import java.util.Objects;
019
020import javax.money.CurrencyUnit;
021import javax.money.MonetaryAmount;
022import javax.money.MonetaryOperator;
023
024import org.javamoney.moneta.convert.ExchangeCurrencyOperator;
025
026/**
027 * This singleton class provides access to the predefined monetary functions.
028 *
029 * The class is thread-safe, which is also true for all functions returned by
030 * this class.
031 * <pre>
032 * {@code
033 *      MonetaryAmount money = Money.parse("EUR 2.35");
034 *  MonetaryAmount result = operator.apply(money);
035 * }
036 * </pre>
037 * Or using:
038 * <pre>
039 * {@code
040 *      MonetaryAmount money = Money.parse("EUR 2.35");
041 *  MonetaryAmount result = money.with(operator);
042 * }
043 * </pre>
044 * @see MonetaryAmount#with(MonetaryOperator)
045 * @see MonetaryOperator
046 * @see MonetaryOperator#apply(MonetaryAmount)
047 * @author Werner Keil
048 * @since 1.0.1
049 */
050public final class ConversionOperators {
051 
052    private ConversionOperators() {
053    }
054
055        /**
056         * Do exchange of currency, in other words, create the monetary amount with the
057         * same value but with currency different.
058         *
059         * For example, 'EUR 2.35', using the currency 'USD' as exchange parameter, will return 'USD 2.35',
060         * and 'BHD -1.345', using the currency 'USD' as exchange parameter, will return 'BHD -1.345'.
061         *
062         *<pre>
063         *{@code
064         *Currency real = Monetary.getCurrency("BRL");
065         *MonetaryAmount money = Money.parse("EUR 2.355");
066         *MonetaryAmount result = ConversionOperators.exchangeCurrency(real).apply(money);//BRL 2.355
067         *}
068         *</pre>
069         * @param currencyUnit the currency
070         * @return the major part as {@link MonetaryOperator}
071         */
072        public static MonetaryOperator exchange(CurrencyUnit currencyUnit){
073                return new ExchangeCurrencyOperator(Objects.requireNonNull(currencyUnit));
074        }
075}