001/*
002 * Copyright (c) 2012, 2014, Credit Suisse (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.function;
017
018import javax.money.MonetaryAmount;
019import javax.money.MonetaryQuery;
020
021/**
022 * This class has utility queries, {@link MonetaryQuery}, to {@link MonetaryAmount}.
023 *
024 * {@code
025 *      MonetaryAmount monetaryAmount = Money.parse("EUR 2.35");
026 *      Long result = monetaryAmount.query(query);// 2L
027 * }
028 *  Or using:
029 * {@code
030 *      MonetaryAmount monetaryAmount = Money.parse("EUR 2.35");
031 *      Long result = query.queryFrom(monetaryAmount);// 2L
032 * }
033 * @see MonetaryAmount#query(MonetaryQuery)
034 * @see MonetaryQuery
035 * @see MonetaryQuery#queryFrom(MonetaryAmount)
036 * @author Otavio Santana
037 * @since 1.0.1
038 */
039public final class MonetaryQueries {
040
041        private static final ExtractorMajorPartQuery EXTRACTOR_MAJOR_PART = new ExtractorMajorPartQuery();
042
043        private static final ConvertMinorPartQuery CONVERT_MINOR_PART = new ConvertMinorPartQuery();
044
045        private static final ExtractorMinorPartQuery EXTRACTOR_MINOR_PART = new ExtractorMinorPartQuery();
046
047        private MonetaryQueries() {
048        }
049
050        /**
051         * Allows to extract the major part of a {@link MonetaryAmount} instance.
052         * Gets the amount in major units as a {@code long}.
053         *
054         * For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
055         *
056         * { @code
057         *      MonetaryAmount monetaryAmount = Money.parse("EUR 2.35");
058         *      Long result = monetaryAmount.query(MonetaryQueries.majorPart());// 2L
059         * }
060         */
061        public static MonetaryQuery<Long> extractMajorPart() {
062                return EXTRACTOR_MAJOR_PART;
063        }
064
065        /**
066         * Convert to minor part a {@link MonetaryAmount} instance.
067         *
068         * This returns the monetary amount in terms of the minor units of the
069         * currency, truncating the amount if necessary. For example, 'EUR 2.35'
070         * will return 235, and 'BHD -1.345' will return -1345.
071         *
072         * {@code
073         *      MonetaryAmount monetaryAmount = Money.parse("EUR 2.35");
074         *      Long result = monetaryAmount.query(MonetaryQueries.convertMinorPart());// 235L
075         * }
076         */
077        public static MonetaryQuery<Long> convertMinorPart() {
078                return CONVERT_MINOR_PART;
079        }
080
081        /**
082         * Convert to minor part a {@link MonetaryAmount} instance.
083         *
084         * This returns the monetary amount in terms of the minor units of the
085         * currency, truncating the whole part if necessary. For example, 'EUR 2.35'
086         * will return 35, and 'BHD -1.345' will return -345.
087         *
088         *
089         * {@code
090         *      MonetaryAmount monetaryAmount = Money.parse("EUR 2.35");
091         *      Long result = monetaryAmount.query(MonetaryQueries.convertMinorPart());// 35L
092         * }
093         */
094        public static MonetaryQuery<Long> extractMinorPart() {
095                return EXTRACTOR_MINOR_PART;
096        }
097}