org.javamoney.moneta.spi.DefaultNumberValue Java Examples

The following examples show how to use org.javamoney.moneta.spi.DefaultNumberValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: USFederalReserveRateReadingHandler.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
private void addRate(CurrencyUnit term, LocalDate localDate, Number rate, boolean inverse) {
    RateType rateType = RateType.HISTORIC;
    ExchangeRateBuilder builder =
        new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).set(localDate).build());
    builder.setBase(inverse ? term : USFederalReserveRateProvider.BASE_CURRENCY);
    builder.setTerm(inverse ? USFederalReserveRateProvider.BASE_CURRENCY : term);
    builder.setFactor(DefaultNumberValue.of(rate));
    ExchangeRate exchangeRate = builder.build();
    if(inverse) {
        exchangeRate = USFederalReserveRateProvider.reverse(exchangeRate);
    }
    Map<String, ExchangeRate> rateMap = this.historicRates.get(localDate);
    if (Objects.isNull(rateMap)) {
        synchronized (this.historicRates) {
            rateMap = Optional.ofNullable(this.historicRates.get(localDate)).orElse(new ConcurrentHashMap<>());
            this.historicRates.putIfAbsent(localDate, rateMap);
        }
    }
    rateMap.put(term.getCurrencyCode(), exchangeRate);
}
 
Example #2
Source File: YahooRateReadingHandler.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
private void addRate(YahooQuoteItemInformation information) {

        final ExchangeRateBuilder builder = new ExchangeRateBuilder(
        		ConversionContextBuilder.create(context, RateType.DEFERRED).build());
        builder.setBase(YahooAbstractRateProvider.BASE_CURRENCY);
        builder.setTerm(information.getCurrency());
        builder.setFactor(DefaultNumberValue.of(information.getValue()));
        final ExchangeRate exchangeRate = builder.build();

        Map<String, ExchangeRate> rateMap = this.excangeRates.get(information.getLocalDate());
        if (Objects.isNull(rateMap)) {
            synchronized (this.excangeRates) {
                rateMap = Optional.ofNullable(this.excangeRates.get(information.getLocalDate())).orElse(new ConcurrentHashMap<>());
                this.excangeRates.putIfAbsent(information.getLocalDate(), rateMap);
            }
        }
        rateMap.put(information.getCurrency().getCurrencyCode(), exchangeRate);

    }
 
Example #3
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void testNumberInsignificanceForRates(){
	ExchangeRate rateFromString = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(new BigDecimal("1.1")))
			.build();

	ExchangeRate rateFromDouble = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(1.1))
			.build();

	assertEquals(rateFromDouble, rateFromString, "Rates are not equal for same factor.");
}
 
Example #4
Source File: ReactiveKnowmExchangeProvider.java    From consensusj with Apache License 2.0 5 votes vote down vote up
public ExchangeRateUpdate tickerMapper(Ticker ticker) {
    Date timestamp = ticker.getTimestamp();
    long time = (timestamp != null) ? timestamp.getTime() : 0;
    return new ExchangeRateUpdate(pair,
            DefaultNumberValue.of(ticker.getBid()),
            time);
}
 
Example #5
Source File: KnowmExchangeRateProvider.java    From consensusj with Apache License 2.0 5 votes vote down vote up
private ExchangeRate buildExchangeRate(CurrencyUnitPair pair, Ticker ticker) {
    return new ExchangeRateBuilder(name, RateType.DEFERRED)
            .setBase(pair.getBase())
            .setTerm(pair.getTarget())
            .setFactor(DefaultNumberValue.of(ticker.getLast()))
            .build();
}
 
Example #6
Source File: ECBAbstractRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
private ExchangeRate reverse(ExchangeRate rate) {
    if (Objects.isNull(rate)) {
        throw new IllegalArgumentException("Rate null is not reversible.");
    }
    return new ExchangeRateBuilder(rate).setRate(rate).setBase(rate.getCurrency()).setTerm(rate.getBaseCurrency())
            .setFactor(divide(DefaultNumberValue.ONE, rate.getFactor(), MathContext.DECIMAL64)).build();
}
 
Example #7
Source File: USFederalReserveRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
private ExchangeRate createExchangeRate(ConversionQuery query, ExchangeRateBuilder builder,
    ExchangeRate sourceRate, ExchangeRate target) {

    if (areBothBaseCurrencies(query)) {
        builder.setFactor(DefaultNumberValue.ONE);
        return builder.build();
    } else if (BASE_CURRENCY_CODE.equals(query.getCurrency().getCurrencyCode())) {
        if (Objects.isNull(sourceRate)) {
            return null;
        }
        return reverse(sourceRate);
    } else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency().getCurrencyCode())) {
        return target;
    } else {

        ExchangeRate rate1 =
            getExchangeRate(query.toBuilder().setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).build());
        ExchangeRate rate2 =
            getExchangeRate(query.toBuilder().setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
                .setTermCurrency(query.getCurrency()).build());
        if (Objects.nonNull(rate1) && Objects.nonNull(rate2)) {
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);
            return builder.build();
        }
        throw new CurrencyConversionException(query.getBaseCurrency(), query.getCurrency(), sourceRate.getContext());
    }
}
 
Example #8
Source File: USFederalReserveRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
protected static ExchangeRate reverse(ExchangeRate rate) {
    if (Objects.isNull(rate)) {
        throw new IllegalArgumentException("Rate null is not reversible.");
    }
    return new ExchangeRateBuilder(rate).setRate(rate).setBase(rate.getCurrency()).setTerm(rate.getBaseCurrency())
        .setFactor(divide(DefaultNumberValue.ONE, rate.getFactor(), MathContext.DECIMAL64)).build();
}
 
Example #9
Source File: YahooAbstractRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
private ExchangeRate createExchangeRate(ConversionQuery query,
		ExchangeRateBuilder builder, ExchangeRate sourceRate,
		ExchangeRate target) {

	if (areBothBaseCurrencies(query)) {
		builder.setFactor(DefaultNumberValue.ONE);
		return builder.build();
	} else if (BASE_CURRENCY_CODE.equals(query.getCurrency()
			.getCurrencyCode())) {
		if (Objects.isNull(sourceRate)) {
			return null;
		}

		return reverse(sourceRate);
	} else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency()
			.getCurrencyCode())) {
		return target;
	} else {

		ExchangeRate rate1 = getExchangeRate(query.toBuilder()
				.setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
				.build());

		ExchangeRate rate2 = getExchangeRate(query.toBuilder()
				.setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
				.setTermCurrency(query.getCurrency()).build());

		if (Objects.nonNull(rate1) && Objects.nonNull(rate2)) {
			builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
			builder.setRateChain(rate1, rate2);
			return builder.build();
		}

		throw new CurrencyConversionException(query.getBaseCurrency(),
				query.getCurrency(), sourceRate.getContext());
	}
}
 
Example #10
Source File: YahooAbstractRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
private ExchangeRate reverse(ExchangeRate rate) {
    if (Objects.isNull(rate)) {
        throw new IllegalArgumentException("Rate null is not reversible.");
    }

    return new ExchangeRateBuilder(rate).setRate(rate).setBase(rate.getCurrency())
    		.setTerm(rate.getBaseCurrency()).setContext(getExchangeContext(DIGIT_FRACTION_KEY))
            .setFactor(divide(DefaultNumberValue.ONE, rate.getFactor(), MathContext.DECIMAL64)).build();
}
 
Example #11
Source File: ECBAbstractRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
private ExchangeRate createExchangeRate(ConversionQuery query,
                                        ExchangeRateBuilder builder, ExchangeRate sourceRate,
                                        ExchangeRate target) {

    if (areBothBaseCurrencies(query)) {
        builder.setFactor(DefaultNumberValue.ONE);
        return builder.build();
    } else if (BASE_CURRENCY_CODE.equals(query.getCurrency().getCurrencyCode())) {
        if (Objects.isNull(sourceRate)) {
            return null;
        }
        return reverse(sourceRate);
    } else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency()
            .getCurrencyCode())) {
        return target;
    } else {

        ExchangeRate rate1 = getExchangeRate(
                query.toBuilder().setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).build());
        ExchangeRate rate2 = getExchangeRate(
                query.toBuilder().setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
                        .setTermCurrency(query.getCurrency()).build());
        if (Objects.nonNull(rate1) && Objects.nonNull(rate2)) {
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);
            return builder.build();
        }
        throw new CurrencyConversionException(query.getBaseCurrency(),
                query.getCurrency(), sourceRate.getContext());
    }
}
 
Example #12
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsTest() {
	DefaultNumberValue factor = new DefaultNumberValue(1.1);
	DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1"));
	CurrencyUnit EUR = Monetary.getCurrency("EUR");
	CurrencyUnit GBP = Monetary.getCurrency("GBP");
	ExchangeRate rate1 = new ExchangeRateBuilder("myprovider", RateType.ANY)
			.setBase(EUR)
			.setTerm(GBP)
			.setFactor(factor)
			.build();

	ExchangeRate rate2 = new ExchangeRateBuilder("myprovider", RateType.ANY)
			.setBase(EUR)
			.setTerm(GBP)
			.setFactor(factor)
			.build();

	ExchangeRate rate3 = new ExchangeRateBuilder("myprovider", RateType.ANY)
			.setBase(EUR)
			.setTerm(GBP)
			.setFactor(bigDecimalFactor)
			.build();

	assertEquals(rate1, rate2, "Rates with same factor");
	assertEquals(rate1, rate3, "Rates with numerically equal factor");
	assertEquals(rate1.hashCode(), rate2.hashCode(), "Hashes with same factor");
	assertEquals(rate1.hashCode(), rate3.hashCode(), "Hashes with numerically equal factor");
}
 
Example #13
Source File: ExchangeRateSimpleTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsTest() {
    DefaultNumberValue factor = new DefaultNumberValue(1.1);
    DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1"));

    ExchangeRate rate1 = new ExchangeRateBuilder("myprovider", RateType.ANY)
            .setBase(EUR)
            .setTerm(GBP)
            .setFactor(factor)
            .build();

    ExchangeRate rate2 = new ExchangeRateBuilder("myprovider", RateType.ANY)
            .setBase(EUR)
            .setTerm(GBP)
            .setFactor(factor)
            .build();

    ExchangeRate rate3 = new ExchangeRateBuilder("myprovider", RateType.ANY)
            .setBase(EUR)
            .setTerm(GBP)
            .setFactor(bigDecimalFactor)
            .build();

    assertEquals(rate1, rate2, "Rates with same factor");
    assertEquals(rate1, rate3, "Rates with numerically equal factor");
    assertEquals(rate1.hashCode(), rate2.hashCode(), "Hashes with same factor");
    assertEquals(rate1.hashCode(), rate3.hashCode(), "Hashes with numerically equal factor");
}
 
Example #14
Source File: IdentityRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public ExchangeRate getReversed(ExchangeRate rate) {
    if (rate.getContext().getProviderName().equals(CONTEXT.getProviderName())) {
        return new ExchangeRateBuilder(rate.getContext()).setTerm(rate.getBaseCurrency())
                .setBase(rate.getCurrency()).setFactor(new DefaultNumberValue(BigDecimal.ONE)).build();
    }
    return null;
}
 
Example #15
Source File: IdentityRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
       if (conversionQuery.getBaseCurrency().getCurrencyCode().equals(conversionQuery.getCurrency().getCurrencyCode())) {
           ExchangeRateBuilder builder = new ExchangeRateBuilder(getContext().getProviderName(), RateType.OTHER)
                   .setBase(conversionQuery.getBaseCurrency());
           builder.setTerm(conversionQuery.getCurrency());
           builder.setFactor(DefaultNumberValue.of(BigDecimal.ONE));
           return builder.build();
       }
       return null;
   }
 
Example #16
Source File: KnowmExchangeRateProvider.java    From consensusj with Apache License 2.0 4 votes vote down vote up
private ExchangeRateUpdate tickerToUpdate(CurrencyUnitPair pair, Ticker ticker) {
    return (ticker != null) ?
            new ExchangeRateUpdate(pair, DefaultNumberValue.of(ticker.getBid()), ticker.getTimestamp().getTime()) :
            new ExchangeRateUpdate(pair, null, 0);
}
 
Example #17
Source File: RoundedMoney.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public NumberValue getNumber() {
    return new DefaultNumberValue(number);
}
 
Example #18
Source File: FastMoney.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the number representation of the numeric value of this item.
 *
 * @return The {@link Number} representation matching best.
 */
@Override
public NumberValue getNumber() {
    return new DefaultNumberValue(getBigDecimal());
}
 
Example #19
Source File: Money.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the number representation of the numeric value of this item.
 *
 * @return The {@link Number} representation matching best.
 */
@Override
public NumberValue getNumber() {
    return new DefaultNumberValue(number);
}
 
Example #20
Source File: MonetaryOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the percentage of the amount.
 * @param number to be used in percent
 * @see #percent(BigDecimal)}
 * @return the percent of {@link MonetaryOperator}
 */
public static MonetaryOperator percent(Number number) {
    return percent(new DefaultNumberValue(number).numberValue(BigDecimal.class));
}
 
Example #21
Source File: MonetaryOperators.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link #percent(BigDecimal)} converting
 * this number to {@link BigDecimal} and using the {@link MathContext} in parameters
 * @param number to be converted to {@link BigDecimal}
 * @param mathContext the mathContext to be used
 * @see #permil(BigDecimal)
 * @return the permil {@link MonetaryOperator}
 */
public static MonetaryOperator permil(Number number, MathContext mathContext) {
    return new PermilOperator(new DefaultNumberValue(number).numberValue(BigDecimal.class));
}
 
Example #22
Source File: ExchangeRateUpdate.java    From consensusj with Apache License 2.0 2 votes vote down vote up
/**
 * Create an ExchangeRateValue for "unknown" exchange rate (no data yet)
 *
 * @param pair Exchange pair we don't have information on
 * @return An ExchangeRateUpdate with "unknown" values
 */
public static ExchangeRateUpdate unknown(CurrencyUnitPair pair) {
    return new ExchangeRateUpdate(pair, DefaultNumberValue.of(0), 0);
}