001/*
002 * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
003 * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
004 * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
005 * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
006 * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
007 * BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
008 * API ("Specification") Copyright (c) 2012-2014, Credit Suisse All rights
009 * reserved.
010 */
011package org.javamoney.moneta;
012
013import javax.money.AbstractQuery;
014import javax.money.QueryType;
015import javax.money.convert.ConversionQuery;
016import java.util.Arrays;
017import java.util.Collections;
018import java.util.HashSet;
019import java.util.Set;
020
021/**
022 * Enumeration with the most important query selectors.
023 */
024public final class QueryTypes {
025
026    /**
027     * Default QueryType for accessing an {@link javax.money.convert.ExchangeRate}.
028     */
029    public static final QueryType RATE_QUERY = QueryTypes.of("RateQuery", ConversionQuery.class, "base: CurrencyUnit", "term: CurrencyUnit");
030
031    /**
032     * Default QueryType for accessing an historic {@link javax.money.convert.ExchangeRate}.
033     */
034    public static final QueryType RATE_HIST_QUERY = QueryTypes.of("RateHistQuery", ConversionQuery.class, "base: CurrencyUnit", "term: CurrencyUnit", "timestamp: long/TemporalAccessor");
035
036    /**
037     * Default QueryType for accessing an {@link javax.money.convert.CurrencyConversion}.
038     */
039    public static final QueryType CONVERSION_QUERY = QueryTypes.of("ConversionQuery", ConversionQuery.class, "term: CurrencyUnit");
040
041    /**
042     * Default QueryType for accessing an historic {@link javax.money.convert.CurrencyConversion}.
043     */
044    public static final QueryType CONVERSION_HIST_QUERY = QueryTypes.of("ConversionHistQuery", ConversionQuery.class, "term: CurrencyUnit", "timestamp: long/TemporalAccessor");
045
046    /**
047     * Default QueryType for accessing an {@link javax.money.convert.ExchangeRate}.
048     */
049    public static final QueryType ROUNDING_CURRENCY_QUERY = QueryTypes.of("RoundingCurrencyQuery", ConversionQuery.class, "currencyUnit: CurrencyUnit", "OPT java.math.RoundingMode: RoundingMode");
050
051    /**
052     * Default QueryType for accessing an {@link javax.money.convert.ExchangeRate}.
053     */
054    public static final QueryType ROUNDING_MATH_QUERY = QueryTypes.of("RoundingMathQuery", ConversionQuery.class, "OPT java.math.MathContext: MathContext", "OPT java.math.RoundingMode: RoundingMode", "scale: int");
055
056    /**
057     * Default QueryType for accessing an {@link javax.money.convert.ExchangeRate}.
058     */
059    public static final QueryType ROUNDING_NAMED_QUERY = QueryTypes.of("RoundingCurrencyQuery", ConversionQuery.class, "roundingName: String");
060
061    private QueryTypes() {
062    }
063
064    public static QueryType of(String name, Class<? extends AbstractQuery> queryType, String... params) {
065        return new SimpleQueryType(name, queryType, params);
066    }
067
068    public static Set<QueryType> from(QueryType... queryTypes) {
069        Set<QueryType> result = new HashSet<>();
070        result.addAll(Arrays.asList(queryTypes));
071        return Collections.unmodifiableSet(result);
072    }
073
074    private static final class SimpleQueryType implements QueryType {
075        private String desc;
076
077        SimpleQueryType(String name, Class<? extends AbstractQuery> queryType, String... params) {
078            StringBuilder b = new StringBuilder("QueryType(").append(name).append(") -> ")
079                    .append(queryType.getName()).append("[\n");
080            for (String p : params) {
081                b.append("   ").append(p).append('\n');
082            }
083            b.append(']');
084            this.desc = b.toString();
085        }
086
087        @Override
088        public boolean equals(Object o) {
089            if (this == o) return true;
090            if (o == null || getClass() != o.getClass()) return false;
091
092            SimpleQueryType that = (SimpleQueryType) o;
093
094            if (!desc.equals(that.desc)) return false;
095
096            return true;
097        }
098
099        @Override
100        public int hashCode() {
101            return desc.hashCode();
102        }
103
104        @Override
105        public String toString() {
106            return desc;
107        }
108    }
109
110}