BAEL-1570 (#3762)
* Create pom.xml * Update pom.xml * Create pom.xml * Update pom.xml * add impl * add app
This commit is contained in:
14
java-spi/exchange-rate-api/pom.xml
Normal file
14
java-spi/exchange-rate-api/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>exchange-rate-api</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-spi</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.rate;
|
||||
|
||||
import com.baeldung.rate.exception.ProviderNotFoundException;
|
||||
import com.baeldung.rate.spi.ExchangeRateProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public final class ExchangeRate {
|
||||
|
||||
private static final String DEFAULT_PROVIDER = "com.baeldung.rate.spi.YahooFinanceExchangeRateProvider";
|
||||
|
||||
//All providers
|
||||
public static List<ExchangeRateProvider> providers() {
|
||||
List<ExchangeRateProvider> services = new ArrayList<>();
|
||||
ServiceLoader<ExchangeRateProvider> loader = ServiceLoader.load(ExchangeRateProvider.class);
|
||||
loader.forEach(exchangeRateProvider -> {
|
||||
services.add(exchangeRateProvider);
|
||||
});
|
||||
return services;
|
||||
}
|
||||
|
||||
//Default provider
|
||||
public static ExchangeRateProvider provider() {
|
||||
return provider(DEFAULT_PROVIDER);
|
||||
}
|
||||
|
||||
//provider by name
|
||||
public static ExchangeRateProvider provider(String providerName) {
|
||||
ServiceLoader<ExchangeRateProvider> loader = ServiceLoader.load(ExchangeRateProvider.class);
|
||||
Iterator<ExchangeRateProvider> it = loader.iterator();
|
||||
while (it.hasNext()) {
|
||||
ExchangeRateProvider provider = it.next();
|
||||
if (providerName.equals(provider.getClass().getName())) {
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
throw new ProviderNotFoundException("Exchange Rate provider " + providerName + " not found");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.rate.api;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class Quote {
|
||||
private String currency;
|
||||
private BigDecimal ask;
|
||||
private BigDecimal bid;
|
||||
private LocalDate date;
|
||||
//...
|
||||
|
||||
public String getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public void setCurrency(String currency) {
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
public BigDecimal getAsk() {
|
||||
return ask;
|
||||
}
|
||||
|
||||
public void setAsk(BigDecimal ask) {
|
||||
this.ask = ask;
|
||||
}
|
||||
|
||||
public BigDecimal getBid() {
|
||||
return bid;
|
||||
}
|
||||
|
||||
public void setBid(BigDecimal bid) {
|
||||
this.bid = bid;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.rate.api;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public interface QuoteManager {
|
||||
List<Quote> getQuotes(String baseCurrency, LocalDate date);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.rate.exception;
|
||||
|
||||
public class ProviderNotFoundException extends RuntimeException {
|
||||
|
||||
public ProviderNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ProviderNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.rate.spi;
|
||||
|
||||
import com.baeldung.rate.api.QuoteManager;
|
||||
|
||||
public interface ExchangeRateProvider {
|
||||
QuoteManager create();
|
||||
}
|
||||
Reference in New Issue
Block a user