Skip to content

Commit d9753a0

Browse files
committed
1 parent 9eacfa1 commit d9753a0

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
/*
2+
* Copyright (c) 2013, 2014, Werner Keil and others by the @author tag.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.javamoney.shelter.bitcoin.provider;
16+
17+
import static javax.money.convert.RateType.DEFERRED;
18+
19+
import java.io.IOException;
20+
import java.net.URL;
21+
import java.util.Locale;
22+
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
24+
25+
import javax.money.CurrencyUnit;
26+
import javax.money.MonetaryException;
27+
import javax.money.NumberValue;
28+
import javax.money.UnknownCurrencyException;
29+
import javax.money.convert.ConversionContext;
30+
import javax.money.convert.CurrencyConversion;
31+
import javax.money.convert.ExchangeRate;
32+
import javax.money.convert.ExchangeRateProvider;
33+
import javax.money.convert.ProviderContext;
34+
import javax.money.convert.RateType;
35+
36+
import org.codehaus.jackson.JsonNode;
37+
import org.codehaus.jackson.map.ObjectMapper;
38+
// TODO consider switching to JSR 353
39+
40+
import org.javamoney.moneta.spi.DefaultNumberValue;
41+
import org.slf4j.Logger;
42+
import org.slf4j.LoggerFactory;
43+
44+
import java.text.NumberFormat;
45+
import java.text.ParseException;
46+
47+
/**
48+
* A Currency conversion provider for Bitcoin.de service.
49+
*
50+
* @author Werner Keil
51+
*/
52+
public class BitcoinDeRateProvider implements ExchangeRateProvider {
53+
public static final RateType RATE_TYPE = DEFERRED;
54+
55+
private static final String PROVIDER_URL = "https://bitcoinapi.de/v1/${YOUR_API_KEY}/rate.json";
56+
57+
private static final Logger LOGGER = LoggerFactory.getLogger(BitcoinDeRateProvider.class);
58+
59+
/**
60+
* Contains supported currency codes.
61+
*/
62+
public static enum SupportedCurrency {
63+
64+
EUR("EUR"), USD("USD");
65+
private final String currencyCode;
66+
67+
public String code() {
68+
return currencyCode;
69+
}
70+
71+
private SupportedCurrency(String code) {
72+
this.currencyCode = code;
73+
74+
}
75+
76+
public static boolean isSupported(String newCurrency) {
77+
try {
78+
valueOf(newCurrency);
79+
} catch (IllegalArgumentException e) {
80+
return false;
81+
}
82+
return true;
83+
84+
}
85+
}
86+
87+
private final String forCurrency;
88+
89+
private Map<String, Number> currentRates = new ConcurrentHashMap<String, Number>();
90+
91+
public BitcoinDeRateProvider() {
92+
this(null);
93+
}
94+
95+
public BitcoinDeRateProvider(String currencyCode) {
96+
this.forCurrency = currencyCode;
97+
98+
if(forCurrency == null) {
99+
for (SupportedCurrency currency : SupportedCurrency.values()) {
100+
loadRate(currency.code());
101+
}
102+
} else {
103+
loadRate(forCurrency);
104+
}
105+
}
106+
107+
public RateType getRateType() {
108+
return RATE_TYPE;
109+
}
110+
111+
@Override
112+
public boolean isAvailable(CurrencyUnit base, CurrencyUnit term) {
113+
return getExchangeRate(base, term) != null;
114+
}
115+
116+
public boolean isAvailable(CurrencyUnit base, CurrencyUnit term, long timestamp) {
117+
return getExchangeRate(base, term, timestamp) != null;
118+
}
119+
120+
public ExchangeRate getExchangeRate(CurrencyUnit base, CurrencyUnit term, long timestamp) {
121+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
122+
}
123+
124+
@Override
125+
public ExchangeRate getExchangeRate(CurrencyUnit base, CurrencyUnit term) {
126+
if (!SupportedCurrency.isSupported(base.getCurrencyCode()) || SupportedCurrency.isSupported(term.getCurrencyCode())) {
127+
return null;
128+
}
129+
final NumberValue factor = DefaultNumberValue.of(currentRates.get(base.getCurrencyCode()));
130+
if (factor!=null) {
131+
return new ExchangeRate.Builder("MtGox", RATE_TYPE).setBase(base).setTerm(term).setFactor(factor).create();
132+
} else {
133+
return null;
134+
}
135+
}
136+
137+
/**
138+
* Looks up the rate for a given currencyCode
139+
*/
140+
void loadRate(String curCode, boolean verbose) {
141+
if (SupportedCurrency.isSupported(curCode)) {
142+
ObjectMapper m = new ObjectMapper();
143+
JsonNode root;
144+
try {
145+
root = m.readTree(new URL(PROVIDER_URL + curCode + "/money/ticker").openStream());
146+
} catch (IOException e) {
147+
throw new MonetaryException("Lookup Error", e);
148+
}
149+
LOGGER.debug("Result : " +root.path("result"));
150+
if (verbose) System.out.println( "Result : " +root.path("result").getTextValue());
151+
152+
JsonNode lastNode = root.findValue("last");
153+
String value = lastNode.path("value").getTextValue();
154+
try {
155+
currentRates.put(curCode, NumberFormat.getInstance(Locale.ENGLISH).parse(value));
156+
} catch (ParseException e) {
157+
LOGGER.warn("Warning", e);
158+
}
159+
if (verbose) System.out.println( "display_short : " +lastNode.path("display_short").getTextValue());
160+
} else {
161+
throw new UnknownCurrencyException(curCode);
162+
}
163+
}
164+
165+
/**
166+
* Looks up the rate for a given currencyCode
167+
*/
168+
void loadRate(String curCode) {
169+
loadRate(curCode, false);
170+
}
171+
172+
@Override
173+
public ProviderContext getProviderContext() {
174+
// TODO Auto-generated method stub
175+
return null;
176+
}
177+
178+
@Override
179+
public boolean isAvailable(CurrencyUnit base, CurrencyUnit term,
180+
ConversionContext conversionContext) {
181+
// TODO Auto-generated method stub
182+
return false;
183+
}
184+
185+
@Override
186+
public boolean isAvailable(String baseCode, String termCode) {
187+
// TODO Auto-generated method stub
188+
return false;
189+
}
190+
191+
@Override
192+
public boolean isAvailable(String baseCode, String termCode,
193+
ConversionContext conversionContext) {
194+
// TODO Auto-generated method stub
195+
return false;
196+
}
197+
198+
@Override
199+
public ExchangeRate getExchangeRate(CurrencyUnit base, CurrencyUnit term,
200+
ConversionContext conversionContext) {
201+
// TODO Auto-generated method stub
202+
return null;
203+
}
204+
205+
@Override
206+
public ExchangeRate getExchangeRate(String baseCode, String termCode) {
207+
// TODO Auto-generated method stub
208+
return null;
209+
}
210+
211+
@Override
212+
public ExchangeRate getExchangeRate(String baseCode, String termCode,
213+
ConversionContext conversionContext) {
214+
// TODO Auto-generated method stub
215+
return null;
216+
}
217+
218+
@Override
219+
public ExchangeRate getReversed(ExchangeRate rate) {
220+
// TODO Auto-generated method stub
221+
return null;
222+
}
223+
224+
@Override
225+
public CurrencyConversion getCurrencyConversion(CurrencyUnit term) {
226+
// TODO Auto-generated method stub
227+
return null;
228+
}
229+
230+
@Override
231+
public CurrencyConversion getCurrencyConversion(CurrencyUnit term,
232+
ConversionContext conversionContext) {
233+
// TODO Auto-generated method stub
234+
return null;
235+
}
236+
237+
@Override
238+
public CurrencyConversion getCurrencyConversion(String termCode) {
239+
// TODO Auto-generated method stub
240+
return null;
241+
}
242+
243+
@Override
244+
public CurrencyConversion getCurrencyConversion(String termCode,
245+
ConversionContext conversionContext) {
246+
// TODO Auto-generated method stub
247+
return null;
248+
}
249+
}

0 commit comments

Comments
 (0)