@Override public byte[] read(String resourceId) { File f = this.cachedResources.get(resourceId); if (Objects.isNull(f)) { return null; } try { return Files.readAllBytes(f.toPath()); } catch (IOException exception) { throw new MonetaryException("An error on retrieve the resource id: " + resourceId, exception); } }
@SuppressWarnings("unchecked") @Override public <T extends MonetaryAmount> MonetaryAmountFactory<T> getAmountFactory(Class<T> amountType) { MonetaryAmountFactoryProviderSpi<T> f = MonetaryAmountFactoryProviderSpi.class.cast(factories.get(amountType)); if (Objects.nonNull(f)) { return f.createMonetaryAmountFactory(); } throw new MonetaryException("No matching MonetaryAmountFactory found, type=" + amountType.getName()); }
@Override public Money with(MonetaryOperator operator) { Objects.requireNonNull(operator); try { return Money.class.cast(operator.apply(this)); } catch (MonetaryException e) { throw e; } catch (Exception e) { throw new MonetaryException("Operator failed: " + operator, e); } }
@Override public <R> R query(MonetaryQuery<R> query) { Objects.requireNonNull(query); try { return query.queryFrom(this); } catch (MonetaryException e) { throw e; } catch (Exception e) { throw new MonetaryException("Query failed: " + query, e); } }
/** * The SPI currently active, use {@link ServiceLoader} to register an * alternate implementation. */ private static MonetaryConversionsSingletonSpi getMonetaryConversionsSpi() { return Optional.ofNullable(Bootstrap.getService(MonetaryConversionsSingletonSpi.class)) .orElseThrow(() -> new MonetaryException("No MonetaryConversionsSingletonSpi " + "loaded, " + "query functionality is not " + "available.")); }
@Override public <T> T query(MonetaryQuery<T> query) { Objects.requireNonNull(query); try { return query.queryFrom(this); } catch (MonetaryException | ArithmeticException e) { throw e; } catch (Exception e) { throw new MonetaryException("Query failed: " + query, e); } }
@Override public FastMoney with(MonetaryOperator operator) { Objects.requireNonNull(operator); try { return FastMoney.class.cast(operator.apply(this)); } catch (ArithmeticException e) { throw e; } catch (Exception e) { throw new MonetaryException("Operator failed: " + operator, e); } }
@Override public <R> R query(MonetaryQuery<R> query) { Objects.requireNonNull(query); try { return query.queryFrom(this); } catch (MonetaryException | ArithmeticException e) { throw e; } catch (Exception e) { throw new MonetaryException("Query failed: " + query, e); } }
@Override public MonetaryAmount with(MonetaryOperator operator) { try { return operator.apply(this); } catch (MonetaryException e) { throw e; } catch (Exception e) { throw new MonetaryException("Exception during operator execution.", e); } }
/** * Get the default provider chain, identified by the unique provider names in order as evaluated and used. * * @return the default provider chain, never null. */ public static List<String> getDefaultFormatProviderChain() { return Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow(() -> new MonetaryException( "No MonetaryFormatsSingletonSpi " + "loaded, query functionality is not available.")) .getDefaultProviderChain(); }
/** * Allows to access the names of the current registered providers. * * @return the set of provider names, never {@code null}. */ public static Set<String> getRoundingProviderNames() { return Optional.ofNullable(monetaryRoundingsSingletonSpi()).orElseThrow( () -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available.")) .getProviderNames(); }
/** * Allows to access the default providers chain used if no provider chain was passed explicitly.. * * @return the chained list of provider names, never {@code null}. */ public static List<String> getDefaultRoundingProviderChain() { return Optional.ofNullable(monetaryRoundingsSingletonSpi()).orElseThrow( () -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available.")) .getDefaultProviderChain(); }
/** * Access multiple {@link MonetaryRounding} instances using a possibly complex query * * @param roundingQuery The {@link javax.money.RoundingQuery} that may contains arbitrary parameters to be * evaluated. * @return all {@link javax.money.MonetaryRounding} instances matching the query, never {@code null}. */ public static Collection<MonetaryRounding> getRoundings(RoundingQuery roundingQuery) { return Optional.ofNullable(monetaryRoundingsSingletonSpi()).orElseThrow( () -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available.")) .getRoundings(roundingQuery); }
/** * Allows to access the names of the current defined roundings. * * @param providers the providers and ordering to be used. By default providers and ordering as defined in * #getDefaultProviders is used. * @return the set of custom rounding ids, never {@code null}. */ public static Set<String> getRoundingNames(String... providers) { return Optional.ofNullable(monetaryRoundingsSingletonSpi()).orElseThrow( () -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available.")) .getRoundingNames(providers); }
/** * Access all currently available {@link MonetaryAmount} implementation classes that are * accessible from this {@link MonetaryAmount} singleton. * * @return all currently available {@link MonetaryAmount} implementation classes that have * corresponding {@link MonetaryAmountFactory} instances provided, never {@code null} */ public static Collection<Class<? extends MonetaryAmount>> getAmountTypes() { return Optional.ofNullable(monetaryAmountsSingletonSpi()) .orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded.")).getAmountTypes(); }
/** * Query all currencies matching the given query. * * @param query The {@link javax.money.CurrencyQuery}, not null. * @return the list of known currencies, never null. */ public static CurrencyUnit getCurrency(CurrencyQuery query) { return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow( () -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup.")) .getCurrency(query); }
/** * Query all currencies matching the given query. * * @param query The {@link javax.money.CurrencyQuery}, not null. * @return the list of known currencies, never null. */ public static Collection<CurrencyUnit> getCurrencies(CurrencyQuery query) { return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow( () -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup.")) .getCurrencies(query); }
/** * Access the default {@link MonetaryAmount} implementation class that is * accessible from this {@link MonetaryAmount} singleton. * * @return all current default {@link MonetaryAmount} implementation class, never {@code null} */ public static Class<? extends MonetaryAmount> getDefaultAmountType() { return Optional.ofNullable(monetaryAmountsSingletonSpi()) .orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded.")) .getDefaultAmountType(); }
/** * Query all currencies matching the given query. * * @return the list of known currencies, never null. */ public static Set<String> getCurrencyProviderNames() { return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow( () -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup.")) .getProviderNames(); }
/** * Access all currently available {@link MonetaryAmount} implementation classes that are * accessible from this {@link MonetaryAmount} singleton. * * @return all currently available {@link MonetaryAmount} implementation classes that have * corresponding {@link MonetaryAmountFactory} instances provided, never {@code null} */ public static Collection<MonetaryAmountFactory<?>> getAmountFactories() { return Optional.ofNullable(monetaryAmountsSingletonSpi()) .orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded.")) .getAmountFactories(); }