| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package com.timjohnstondev.unitconverter.logic; |
| 12 | |
|
| 13 | |
import java.math.BigDecimal; |
| 14 | |
import java.math.MathContext; |
| 15 | |
import java.math.RoundingMode; |
| 16 | |
import java.text.DecimalFormat; |
| 17 | |
import java.util.ResourceBundle; |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
public final class CalculationLogic |
| 23 | |
{ |
| 24 | 1 | private static ResourceBundle resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View"); |
| 25 | |
|
| 26 | |
private CalculationLogic() |
| 27 | 0 | {} |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public static String getConversionFactor(final String conversion) |
| 36 | |
{ |
| 37 | 2 | String text = conversion; |
| 38 | 2 | if (conversion.indexOf(FormulaParser.VARIABLE) < 0) |
| 39 | |
{ |
| 40 | 1 | text = formatNumber(new BigDecimal(conversion)); |
| 41 | |
} |
| 42 | 2 | return text; |
| 43 | |
} |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public static String getResult(final String input, final String conversionFactor, |
| 55 | |
final BigDecimal molecularWeightCorrection) |
| 56 | |
{ |
| 57 | 3 | BigDecimal resultNumber = BigDecimal.ZERO; |
| 58 | 3 | String result = null; |
| 59 | |
|
| 60 | 3 | if (conversionFactor != null && verifyInput(input)) |
| 61 | |
{ |
| 62 | 3 | if (conversionFactor.indexOf(FormulaParser.VARIABLE) >= 0) |
| 63 | |
{ |
| 64 | 1 | resultNumber = FormulaParser.parse(input, conversionFactor); |
| 65 | |
} |
| 66 | |
else |
| 67 | |
{ |
| 68 | 2 | resultNumber = new BigDecimal(input).multiply(new BigDecimal(conversionFactor)); |
| 69 | |
} |
| 70 | 3 | resultNumber = resultNumber.multiply(molecularWeightCorrection); |
| 71 | 3 | result = formatNumber(resultNumber); |
| 72 | |
} |
| 73 | 3 | return result; |
| 74 | |
} |
| 75 | |
|
| 76 | |
private static boolean verifyInput(final String input) |
| 77 | |
{ |
| 78 | 3 | boolean matches = false; |
| 79 | 3 | final String[] patterns = {resources.getString("InputFilter.inputPatternSNDecimal"), |
| 80 | |
resources.getString("InputFilter.inputPatternSNFraction"), |
| 81 | |
resources.getString("InputFilter.inputPatternSNInteger"), |
| 82 | |
resources.getString("InputFilter.inputPatternDecimal"), |
| 83 | |
resources.getString("InputFilter.inputPatternFraction"), |
| 84 | |
resources.getString("InputFilter.inputPatternInteger")}; |
| 85 | |
|
| 86 | 18 | for (String pattern : patterns) |
| 87 | |
{ |
| 88 | 18 | if (input.matches(pattern)) |
| 89 | |
{ |
| 90 | 3 | matches = true; |
| 91 | 3 | break; |
| 92 | |
} |
| 93 | |
} |
| 94 | 3 | return matches; |
| 95 | |
} |
| 96 | |
|
| 97 | |
private static String formatNumber(final BigDecimal number) |
| 98 | |
{ |
| 99 | 4 | final int numberLengthLimit = Integer.parseInt(resources.getString("CalculationPanel.numberLengthLimit")); |
| 100 | 4 | DecimalFormat formatter = new DecimalFormat(resources.getString("CalculationPanel.nonScientificNotationFormat")); |
| 101 | 4 | if (number.toPlainString().length() > numberLengthLimit) |
| 102 | |
{ |
| 103 | 1 | if (number.doubleValue() < .1 || number.doubleValue() > Math.pow(10, numberLengthLimit)) |
| 104 | |
{ |
| 105 | 0 | formatter = new DecimalFormat(resources.getString("CalculationPanel.scientificNotationFormat")); |
| 106 | |
} |
| 107 | |
} |
| 108 | 4 | final MathContext mathContext = new MathContext(numberLengthLimit, RoundingMode.HALF_UP); |
| 109 | 4 | final BigDecimal tempFactor = number.round(mathContext); |
| 110 | 4 | return formatter.format(tempFactor); |
| 111 | |
} |
| 112 | |
|
| 113 | |
} |