| 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.util.ResourceBundle; |
| 17 | |
import javax.swing.JButton; |
| 18 | |
import javax.swing.JTextField; |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public final class MolarMassLogic |
| 24 | |
{ |
| 25 | 1 | private static final ResourceBundle RESOURCES = ResourceBundle |
| 26 | |
.getBundle("com.timjohnstondev.unitconverter.view.View"); |
| 27 | |
|
| 28 | |
private MolarMassLogic() |
| 29 | 0 | {} |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public static BigDecimal getMolecularWeightCorrection(final JTextField massMoleField, final JButton mass2Mole, |
| 42 | |
final Object selectedButton, final boolean isMolUnitDivisor) |
| 43 | |
{ |
| 44 | 7 | BigDecimal mwc = BigDecimal.ONE; |
| 45 | 7 | if (massMoleField.isVisible() && massMoleField.getText() != null && !massMoleField.getText().equals("") |
| 46 | |
&& selectedButton != null) |
| 47 | |
{ |
| 48 | 3 | final boolean isMassToMoleConversion = isMassToMoleConversion(mass2Mole, selectedButton); |
| 49 | 3 | mwc = new BigDecimal(massMoleField.getText()); |
| 50 | 3 | if ((isMassToMoleConversion && !isMolUnitDivisor) || (!isMassToMoleConversion && isMolUnitDivisor)) |
| 51 | |
{ |
| 52 | 2 | final String numberLengthLimit = RESOURCES.getString("CalculationPanel.numberLengthLimit"); |
| 53 | 2 | final MathContext mathContext = new MathContext(Integer.parseInt(numberLengthLimit), RoundingMode.HALF_UP); |
| 54 | 2 | mwc = BigDecimal.ONE.divide(mwc, mathContext); |
| 55 | |
} |
| 56 | |
} |
| 57 | 7 | return mwc; |
| 58 | |
} |
| 59 | |
|
| 60 | |
private static boolean isMassToMoleConversion(final JButton mass2Mole, final Object selectedButton) |
| 61 | |
{ |
| 62 | 3 | boolean isMass2Mole = false; |
| 63 | 3 | if (mass2Mole == selectedButton) |
| 64 | |
{ |
| 65 | 2 | isMass2Mole = true; |
| 66 | |
} |
| 67 | 3 | return isMass2Mole; |
| 68 | |
} |
| 69 | |
} |