From a2d5d89fe4e8770600623aeef84a81ef1145b7b2 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 10 Apr 2016 21:29:34 +0200 Subject: [PATCH] Remove DecimalFormatSymbols from JsclMathEngine --- .../solovyev/android/calculator/Engine.java | 2 +- .../FromJsclSimplifyTextProcessorTest.java | 6 +---- .../calculator/model/AndroidEngineTest.java | 13 ++-------- jscl/src/main/java/jscl/JsclMathEngine.java | 24 +++++-------------- jscl/src/main/java/jscl/MathContext.java | 3 --- 5 files changed, 10 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/Engine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java index 38b9f5d0..244881f9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -238,7 +238,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene } public static class Preferences { - public static final StringPreference groupingSeparator = StringPreference.of("engine.groupingSeparator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); + public static final StringPreference groupingSeparator = StringPreference.of("engine.groupingSeparator", String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT)); public static final StringPreference multiplicationSign = StringPreference.of("engine.multiplicationSign", "×"); public static final StringPreference numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class)); public static final StringPreference angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class)); diff --git a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java index fca39d99..b175daf3 100644 --- a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java @@ -26,8 +26,6 @@ import org.junit.Test; import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor; import org.solovyev.android.calculator.variables.CppVariable; -import java.text.DecimalFormatSymbols; - import static org.junit.Assert.assertEquals; public class FromJsclSimplifyTextProcessorTest { @@ -39,9 +37,7 @@ public class FromJsclSimplifyTextProcessorTest { //Assert.assertEquals("(e)", tp.process("(2.718281828459045)")); //Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045")); //Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))")); - DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(); - decimalGroupSymbols.setGroupingSeparator(' '); - engine.getMathEngine().setDecimalGroupSymbols(decimalGroupSymbols); + engine.getMathEngine().setGroupingSeparator(' '); //Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045")); //Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045")); //Assert.assertEquals("t11e", tp.process("t11*2.718281828459045")); diff --git a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java index 271e38a8..8f6cd6aa 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java @@ -36,9 +36,6 @@ import org.solovyev.android.calculator.BaseCalculatorTest; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.variables.CppVariable; -import java.text.DecimalFormatSymbols; -import java.util.Locale; - import static org.junit.Assert.fail; public class AndroidEngineTest extends BaseCalculatorTest { @@ -134,10 +131,7 @@ public class AndroidEngineTest extends BaseCalculatorTest { final MathEngine me = engine.getMathEngine(); try { - DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); - decimalGroupSymbols.setDecimalSeparator('.'); - decimalGroupSymbols.setGroupingSeparator('\''); - me.setDecimalGroupSymbols(decimalGroupSymbols); + me.setGroupingSeparator('\''); me.setPrecision(2); assertEval("12'345'678.9", me.evaluate("1.23456789E7")); me.setPrecision(10); @@ -146,10 +140,7 @@ public class AndroidEngineTest extends BaseCalculatorTest { assertEval("1'234'567'890.1", me.evaluate("1.2345678901E9")); } finally { me.setPrecision(3); - DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); - decimalGroupSymbols.setDecimalSeparator('.'); - decimalGroupSymbols.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT.charAt(0)); - me.setDecimalGroupSymbols(decimalGroupSymbols); + me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); } } diff --git a/jscl/src/main/java/jscl/JsclMathEngine.java b/jscl/src/main/java/jscl/JsclMathEngine.java index c295fec7..dbca872d 100644 --- a/jscl/src/main/java/jscl/JsclMathEngine.java +++ b/jscl/src/main/java/jscl/JsclMathEngine.java @@ -17,15 +17,13 @@ import org.solovyev.common.msg.Messages; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.math.BigDecimal; -import java.text.DecimalFormatSymbols; import java.util.List; -import java.util.Locale; public class JsclMathEngine implements MathEngine { public static final AngleUnit DEFAULT_ANGLE_UNITS = AngleUnit.deg; public static final NumeralBase DEFAULT_NUMERAL_BASE = NumeralBase.dec; - public static final String GROUPING_SEPARATOR_DEFAULT = " "; + public static final char GROUPING_SEPARATOR_DEFAULT = ' '; public static final int MAX_FRACTION_DIGITS = 20; @Nonnull private static JsclMathEngine instance = new JsclMathEngine(); @@ -38,8 +36,7 @@ public class JsclMathEngine implements MathEngine { return new NumberFormatter(); } }; - @Nonnull - private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); + private char groupingSeparator = GROUPING_SEPARATOR_DEFAULT; private boolean roundResult = false; private boolean scienceNotation = false; private int precision = 5; @@ -51,11 +48,6 @@ public class JsclMathEngine implements MathEngine { @Nonnull private MessageRegistry messageRegistry = Messages.synchronizedMessageRegistry(new FixedCapacityListMessageRegistry(10)); - { - decimalGroupSymbols.setDecimalSeparator('.'); - decimalGroupSymbols.setGroupingSeparator(GROUPING_SEPARATOR_DEFAULT.charAt(0)); - } - public JsclMathEngine() { } @@ -173,7 +165,7 @@ public class JsclMathEngine implements MathEngine { } } final NumberFormatter nf = numberFormatter.get(); - nf.setGroupingSeparator(useGroupingSeparator ? decimalGroupSymbols.getGroupingSeparator() : NumberFormatter.NO_GROUPING); + nf.setGroupingSeparator(useGroupingSeparator ? groupingSeparator : NumberFormatter.NO_GROUPING); nf.setPrecision(roundResult ? precision : NumberFormatter.DEFAULT_PRECISION); if (scienceNotation) { nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE); @@ -250,7 +242,7 @@ public class JsclMathEngine implements MathEngine { @Nonnull public String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedDoubleValue) { if (useGroupingSeparator) { - String groupingSeparator = nb == NumeralBase.dec ? String.valueOf(decimalGroupSymbols.getGroupingSeparator()) : " "; + final String groupingSeparator = nb == NumeralBase.dec ? String.valueOf(this.groupingSeparator) : " "; final int dotIndex = ungroupedDoubleValue.indexOf("."); @@ -302,10 +294,6 @@ public class JsclMathEngine implements MathEngine { return result; } - public void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols) { - this.decimalGroupSymbols = decimalGroupSymbols; - } - public void setRoundResult(boolean roundResult) { this.roundResult = roundResult; } @@ -323,10 +311,10 @@ public class JsclMathEngine implements MathEngine { } public char getGroupingSeparator() { - return this.decimalGroupSymbols.getGroupingSeparator(); + return this.groupingSeparator; } public void setGroupingSeparator(char groupingSeparator) { - this.decimalGroupSymbols.setGroupingSeparator(groupingSeparator); + this.groupingSeparator = groupingSeparator; } } diff --git a/jscl/src/main/java/jscl/MathContext.java b/jscl/src/main/java/jscl/MathContext.java index fae52488..30400a29 100644 --- a/jscl/src/main/java/jscl/MathContext.java +++ b/jscl/src/main/java/jscl/MathContext.java @@ -6,7 +6,6 @@ import jscl.math.operator.Operator; import org.solovyev.common.math.MathRegistry; import javax.annotation.Nonnull; -import java.text.DecimalFormatSymbols; public interface MathContext { @@ -36,8 +35,6 @@ public interface MathContext { void setNumeralBase(@Nonnull NumeralBase numeralBase); - void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols); - void setRoundResult(boolean roundResult); void setPrecision(int precision);