-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: #406
- Loading branch information
Showing
9 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name.abuchen.portfolio.money.impl.GBXExchangeRateProviderTest |
39 changes: 39 additions & 0 deletions
39
...en.portfolio.tests/src/name/abuchen/portfolio/money/impl/GBXExchangeRateProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package name.abuchen.portfolio.money.impl; | ||
|
||
import static org.hamcrest.number.OrderingComparison.comparesEqualTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
|
||
import org.junit.Test; | ||
|
||
import name.abuchen.portfolio.money.ExchangeRateProviderFactory; | ||
import name.abuchen.portfolio.money.ExchangeRateTimeSeries; | ||
|
||
@SuppressWarnings("nls") | ||
public class GBXExchangeRateProviderTest | ||
{ | ||
@Test | ||
public void testIt() | ||
{ | ||
ExchangeRateProviderFactory factory = new ExchangeRateProviderFactory(); | ||
|
||
// default value EUR -> GBP is 0.72666 | ||
ExchangeRateTimeSeries eur_gbx = factory.getTimeSeries("EUR", "GBX"); | ||
assertThat(eur_gbx.lookupRate(LocalDate.now()).get().getValue(), comparesEqualTo(new BigDecimal("72.666"))); | ||
|
||
// inverse of default EUR -> GBP | ||
ExchangeRateTimeSeries gbx_eur = factory.getTimeSeries("GBX", "EUR"); | ||
assertThat(gbx_eur.lookupRate(LocalDate.now()).get().getValue(), comparesEqualTo( | ||
BigDecimal.ONE.divide(new BigDecimal("72.666"), 12, BigDecimal.ROUND_HALF_DOWN))); | ||
|
||
// GBX -> GBP | ||
ExchangeRateTimeSeries gbx_gbp = factory.getTimeSeries("GBX", "GBP"); | ||
assertThat(gbx_gbp.lookupRate(LocalDate.now()).get().getValue(), comparesEqualTo(new BigDecimal("0.01"))); | ||
|
||
// GBP -> GBX | ||
ExchangeRateTimeSeries gbp_gbx = factory.getTimeSeries("GBP", "GBX"); | ||
assertThat(gbp_gbx.lookupRate(LocalDate.now()).get().getValue(), comparesEqualTo(new BigDecimal(100.0))); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
name.abuchen.portfolio/META-INF/services/name.abuchen.portfolio.money.ExchangeRateProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
name.abuchen.portfolio.money.impl.ECBExchangeRateProvider | ||
name.abuchen.portfolio.money.impl.GBXExchangeRateProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
name.abuchen.portfolio/src/name/abuchen/portfolio/money/impl/GBXExchangeRateProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package name.abuchen.portfolio.money.impl; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
|
||
import name.abuchen.portfolio.money.CurrencyUnit; | ||
import name.abuchen.portfolio.money.ExchangeRate; | ||
import name.abuchen.portfolio.money.ExchangeRateProvider; | ||
import name.abuchen.portfolio.money.ExchangeRateProviderFactory; | ||
import name.abuchen.portfolio.money.ExchangeRateTimeSeries; | ||
|
||
public class GBXExchangeRateProvider implements ExchangeRateProvider | ||
{ | ||
private static final String GBX = "GBX"; //$NON-NLS-1$ | ||
private static final String GBP = "GBP"; //$NON-NLS-1$ | ||
|
||
private ExchangeRateProviderFactory factory; | ||
|
||
private ExchangeRateTimeSeries gbp2gbx = new GBPGBX(this); | ||
private ExchangeRateTimeSeries gbx2gbp = new GBXGBP(this); | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return CurrencyUnit.getInstance(GBX).getDisplayName(); | ||
} | ||
|
||
@Override | ||
public void init(ExchangeRateProviderFactory factory) | ||
{ | ||
this.factory = factory; | ||
} | ||
|
||
@Override | ||
public void load(IProgressMonitor monitor) throws IOException | ||
{} | ||
|
||
@Override | ||
public void update(IProgressMonitor monitor) throws IOException | ||
{} | ||
|
||
@Override | ||
public void save(IProgressMonitor monitor) throws IOException | ||
{} | ||
|
||
@Override | ||
public List<ExchangeRateTimeSeries> getAvailableTimeSeries() | ||
{ | ||
List<ExchangeRateTimeSeries> answer = new ArrayList<>(); | ||
answer.add(new GBXGBP(this)); | ||
return answer; | ||
} | ||
|
||
@Override | ||
public ExchangeRateTimeSeries getTimeSeries(String baseCurrency, String termCurrency) | ||
{ | ||
if (GBX.equals(baseCurrency) && GBP.equals(termCurrency)) | ||
{ | ||
return gbx2gbp; | ||
} | ||
else if (GBP.equals(baseCurrency) && GBX.equals(termCurrency)) | ||
{ | ||
return gbp2gbx; | ||
} | ||
else if (GBX.equals(baseCurrency) && CurrencyUnit.EUR.equals(termCurrency)) | ||
{ | ||
ExchangeRateTimeSeries series = factory.getTimeSeries(GBP, CurrencyUnit.EUR); | ||
return new ChainedExchangeRateTimeSeries(gbx2gbp, series); | ||
} | ||
else if (CurrencyUnit.EUR.equals(baseCurrency) && GBX.equals(termCurrency)) | ||
{ | ||
ExchangeRateTimeSeries series = factory.getTimeSeries(CurrencyUnit.EUR, GBP); | ||
return new ChainedExchangeRateTimeSeries(series, gbp2gbx); | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private static class GBXGBP implements ExchangeRateTimeSeries | ||
{ | ||
private ExchangeRateProvider provider; | ||
private BigDecimal rate = BigDecimal.valueOf(0.01); | ||
|
||
public GBXGBP(ExchangeRateProvider provider) | ||
{ | ||
this.provider = provider; | ||
} | ||
|
||
@Override | ||
public String getBaseCurrency() | ||
{ | ||
return GBX; | ||
} | ||
|
||
@Override | ||
public String getTermCurrency() | ||
{ | ||
return GBP; | ||
} | ||
|
||
@Override | ||
public ExchangeRateProvider getProvider() | ||
{ | ||
return provider; | ||
} | ||
|
||
@Override | ||
public List<ExchangeRate> getRates() | ||
{ | ||
List<ExchangeRate> answer = new ArrayList<>(); | ||
answer.add(new ExchangeRate(LocalDate.now(), rate)); | ||
return answer; | ||
} | ||
|
||
@Override | ||
public Optional<ExchangeRate> lookupRate(LocalDate requestedTime) | ||
{ | ||
return Optional.of(new ExchangeRate(requestedTime, rate)); | ||
} | ||
} | ||
|
||
private static class GBPGBX implements ExchangeRateTimeSeries | ||
{ | ||
private ExchangeRateProvider provider; | ||
private BigDecimal rate = BigDecimal.valueOf(100); | ||
|
||
public GBPGBX(ExchangeRateProvider provider) | ||
{ | ||
this.provider = provider; | ||
} | ||
|
||
@Override | ||
public String getBaseCurrency() | ||
{ | ||
return GBP; | ||
} | ||
|
||
@Override | ||
public String getTermCurrency() | ||
{ | ||
return GBX; | ||
} | ||
|
||
@Override | ||
public ExchangeRateProvider getProvider() | ||
{ | ||
return provider; | ||
} | ||
|
||
@Override | ||
public List<ExchangeRate> getRates() | ||
{ | ||
List<ExchangeRate> answer = new ArrayList<>(); | ||
answer.add(new ExchangeRate(LocalDate.now(), rate)); | ||
return answer; | ||
} | ||
|
||
@Override | ||
public Optional<ExchangeRate> lookupRate(LocalDate requestedTime) | ||
{ | ||
return Optional.of(new ExchangeRate(requestedTime, rate)); | ||
} | ||
} | ||
} |