-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract parsing functions from BSFormatter to ParsingUtils
Parsing and formatting are 2 different concerns, its better to have 2 classes that handle these.
- Loading branch information
Justin Carter
committed
Sep 11, 2019
1 parent
fe33a80
commit 3b46f35
Showing
23 changed files
with
164 additions
and
136 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
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
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,83 @@ | ||
package bisq.core.util; | ||
|
||
import bisq.core.monetary.Price; | ||
|
||
import bisq.common.util.MathUtils; | ||
|
||
import org.bitcoinj.core.Coin; | ||
import org.bitcoinj.utils.MonetaryFormat; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class ParsingUtils { | ||
public static Coin parseToCoin(String input, BSFormatter bsFormatter) { | ||
return parseToCoin(input, bsFormatter.getMonetaryFormat()); | ||
} | ||
|
||
public static Coin parseToCoin(String input, MonetaryFormat coinFormat) { | ||
if (input != null && input.length() > 0) { | ||
try { | ||
return coinFormat.parse(cleanDoubleInput(input)); | ||
} catch (Throwable t) { | ||
log.warn("Exception at parseToBtc: " + t.toString()); | ||
return Coin.ZERO; | ||
} | ||
} else { | ||
return Coin.ZERO; | ||
} | ||
} | ||
|
||
public static double parseNumberStringToDouble(String input) throws NumberFormatException { | ||
return Double.parseDouble(cleanDoubleInput(input)); | ||
} | ||
|
||
public static double parsePercentStringToDouble(String percentString) throws NumberFormatException { | ||
String input = percentString.replace("%", ""); | ||
input = cleanDoubleInput(input); | ||
double value = Double.parseDouble(input); | ||
return MathUtils.roundDouble(value / 100d, 4); | ||
} | ||
|
||
public static long parsePriceStringToLong(String currencyCode, String amount, int precision) { | ||
if (amount == null || amount.isEmpty()) | ||
return 0; | ||
|
||
long value = 0; | ||
try { | ||
double amountValue = Double.parseDouble(amount); | ||
amount = BSFormatter.formatRoundedDoubleWithPrecision(amountValue, precision); | ||
value = Price.parse(currencyCode, amount).getValue(); | ||
} catch (NumberFormatException ignore) { | ||
// expected NumberFormatException if input is not a number | ||
} catch (Throwable t) { | ||
log.error("parsePriceStringToLong: " + t.toString()); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
public static String convertCharsForNumber(String input) { | ||
// Some languages like finnish use the long dash for the minus | ||
input = input.replace("−", "-"); | ||
input = StringUtils.deleteWhitespace(input); | ||
return input.replace(",", "."); | ||
} | ||
|
||
public static String cleanDoubleInput(String input) { | ||
input = convertCharsForNumber(input); | ||
if (input.equals(".")) | ||
input = input.replace(".", "0."); | ||
if (input.equals("-.")) | ||
input = input.replace("-.", "-0."); | ||
// don't use String.valueOf(Double.parseDouble(input)) as return value as it gives scientific | ||
// notation (1.0E-6) which screw up coinFormat.parse | ||
//noinspection ResultOfMethodCallIgnored | ||
// Just called to check if we have a valid double, throws exception otherwise | ||
//noinspection ResultOfMethodCallIgnored | ||
Double.parseDouble(input); | ||
return input; | ||
} | ||
} |
Oops, something went wrong.