Skip to content

Commit 4a3e190

Browse files
committed
Move parsing to Currency.fromCurrencyAmount()
streamshub#55 (comment)
1 parent 94563f7 commit 4a3e190

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

docs/user-defined-functions/index.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ Now, we can begin implementing our function logic in the `eval` method.
250250
As a reminder, we want to convert a string like "€100" into "100 EUR". To do this, we can use the following steps:
251251

252252
1. Get the first character of the string, which is the currency symbol (e.g. "€").
253-
2. Get the rest of the string, which is the amount (e.g. "100").
254-
3. Look up the currency symbol in our `enum` to get the corresponding currency code (e.g. "€" => "EUR").
255-
4. If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code.
253+
2. Look up the currency symbol in our `enum` to get the corresponding currency code (e.g. "€" => "EUR").
254+
3. If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code.
255+
4. Get the rest of the string, which is the amount (e.g. "100").
256256
5. Concatenate the currency code to the amount, and return the result (e.g. "100 EUR").
257257

258258
A possible implementation could look like this:
@@ -286,30 +286,33 @@ public class CurrencyConverter extends ScalarFunction {
286286
return isoCode;
287287
}
288288

289+
public static Currency fromCurrencyAmount(String currencyAmount) {
290+
// 1. Get the first character of the string, which is the currency symbol (e.g. '€').
291+
String currencySymbol = currencyAmount.substring(0, 1);
292+
293+
// 2. Look up the currency symbol in our enum to get the corresponding currency code (e.g. "€" => "EUR").
294+
try {
295+
return Currency.valueOf(currencySymbol);
296+
} catch (Exception e) {
297+
// 3. If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code (e.g. ">" => "ERR").
298+
return Currency.ERR;
299+
}
300+
}
301+
289302
public String concatToAmount(String amount) {
290303
return amount + SEPARATOR + isoCode;
291304
}
292305
}
293306

294307
// Value of passed field (e.g. "unit_cost") is passed in e.g. "€100"
295308
public String eval(String currencyAmount) {
296-
// 1. Get the first character of the string, which is the currency symbol (e.g. '€').
297-
String currencySymbol = currencyAmount.substring(0, 1);
309+
String currencySymbol = Currency.fromCurrencyAmount(currencyAmount);
298310

299-
// 2. Get the rest of the string, which is the amount (e.g. "100").
311+
// 4. Get the rest of the string, which is the amount (e.g. "100").
300312
String amount = currencyAmount.substring(1);
301313

302-
Currency currency;
303-
try {
304-
// 3. Look up the currency symbol in our enum to get the corresponding currency code (e.g. "€" => "EUR").
305-
currency = Currency.valueOf(currencySymbol);
306-
} catch (Exception e) {
307-
// 4. If the lookup failed (e.g. currency symbol was not found), we can return "ERR" as the currency code.
308-
currency = Currency.ERR; // e.g. ">" => "ERR"
309-
}
310-
311314
// 5. Concatenate the currency code to the amount, and return the result (e.g. "100 EUR").
312-
return currency.concatToAmount(amount); // e.g. "100 EUR"
315+
return currency.concatToAmount(amount);
313316
}
314317
}
315318
```

tutorials/currency-converter/src/main/java/com/github/streamshub/flink/functions/CurrencyConverter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@ public String getIsoCode() {
2626
return isoCode;
2727
}
2828

29+
public static Currency fromCurrencyAmount(String currencyAmount) {
30+
String currencySymbol = currencyAmount.substring(0, 1); // e.g. '€'
31+
try {
32+
return Currency.valueOf(currencySymbol); // e.g. "€" => "EUR"
33+
} catch (Exception e) {
34+
return Currency.ERR; // e.g. ">" => "ERR"
35+
}
36+
}
37+
2938
public String concatToAmount(String amount) {
3039
return amount + SEPARATOR + isoCode;
3140
}
3241
}
3342

3443
// e.g. currencyAmount = "€100"
3544
public String eval(String currencyAmount) {
36-
String currencySymbol = currencyAmount.substring(0, 1); // e.g. "€"
45+
Currency currency = Currency.fromCurrencyAmount(currencyAmount);
46+
3747
String amount = currencyAmount.substring(1); // e.g. "100"
3848

39-
Currency currency;
40-
try {
41-
currency = Currency.valueOf(currencySymbol); // e.g. "€" => "EUR"
42-
} catch (Exception e) {
43-
currency = Currency.ERR; // e.g. ">" => "ERR"
44-
}
45-
4649
return currency.concatToAmount(amount); // e.g. "100 EUR"
4750
}
4851
}

0 commit comments

Comments
 (0)