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