Remove DecimalFormatSymbols from JsclMathEngine

This commit is contained in:
serso 2016-04-10 21:29:34 +02:00
parent 0fbd6c44e8
commit a2d5d89fe4
5 changed files with 10 additions and 38 deletions

View File

@ -238,7 +238,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
} }
public static class Preferences { public static class Preferences {
public static final StringPreference<String> groupingSeparator = StringPreference.of("engine.groupingSeparator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); public static final StringPreference<String> groupingSeparator = StringPreference.of("engine.groupingSeparator", String.valueOf(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT));
public static final StringPreference<String> multiplicationSign = StringPreference.of("engine.multiplicationSign", "×"); public static final StringPreference<String> multiplicationSign = StringPreference.of("engine.multiplicationSign", "×");
public static final StringPreference<NumeralBase> numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class)); public static final StringPreference<NumeralBase> numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class));
public static final StringPreference<AngleUnit> angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class)); public static final StringPreference<AngleUnit> angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class));

View File

@ -26,8 +26,6 @@ import org.junit.Test;
import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor; import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor;
import org.solovyev.android.calculator.variables.CppVariable; import org.solovyev.android.calculator.variables.CppVariable;
import java.text.DecimalFormatSymbols;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class FromJsclSimplifyTextProcessorTest { public class FromJsclSimplifyTextProcessorTest {
@ -39,9 +37,7 @@ public class FromJsclSimplifyTextProcessorTest {
//Assert.assertEquals("(e)", tp.process("(2.718281828459045)")); //Assert.assertEquals("(e)", tp.process("(2.718281828459045)"));
//Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045")); //Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045"));
//Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))")); //Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))"));
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(); engine.getMathEngine().setGroupingSeparator(' ');
decimalGroupSymbols.setGroupingSeparator(' ');
engine.getMathEngine().setDecimalGroupSymbols(decimalGroupSymbols);
//Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045")); //Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045"));
//Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045")); //Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045"));
//Assert.assertEquals("t11e", tp.process("t11*2.718281828459045")); //Assert.assertEquals("t11e", tp.process("t11*2.718281828459045"));

View File

@ -36,9 +36,6 @@ import org.solovyev.android.calculator.BaseCalculatorTest;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.variables.CppVariable; import org.solovyev.android.calculator.variables.CppVariable;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public class AndroidEngineTest extends BaseCalculatorTest { public class AndroidEngineTest extends BaseCalculatorTest {
@ -134,10 +131,7 @@ public class AndroidEngineTest extends BaseCalculatorTest {
final MathEngine me = engine.getMathEngine(); final MathEngine me = engine.getMathEngine();
try { try {
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); me.setGroupingSeparator('\'');
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator('\'');
me.setDecimalGroupSymbols(decimalGroupSymbols);
me.setPrecision(2); me.setPrecision(2);
assertEval("12'345'678.9", me.evaluate("1.23456789E7")); assertEval("12'345'678.9", me.evaluate("1.23456789E7"));
me.setPrecision(10); me.setPrecision(10);
@ -146,10 +140,7 @@ public class AndroidEngineTest extends BaseCalculatorTest {
assertEval("1'234'567'890.1", me.evaluate("1.2345678901E9")); assertEval("1'234'567'890.1", me.evaluate("1.2345678901E9"));
} finally { } finally {
me.setPrecision(3); me.setPrecision(3);
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT.charAt(0));
me.setDecimalGroupSymbols(decimalGroupSymbols);
} }
} }

View File

@ -17,15 +17,13 @@ import org.solovyev.common.msg.Messages;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormatSymbols;
import java.util.List; import java.util.List;
import java.util.Locale;
public class JsclMathEngine implements MathEngine { public class JsclMathEngine implements MathEngine {
public static final AngleUnit DEFAULT_ANGLE_UNITS = AngleUnit.deg; public static final AngleUnit DEFAULT_ANGLE_UNITS = AngleUnit.deg;
public static final NumeralBase DEFAULT_NUMERAL_BASE = NumeralBase.dec; 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; public static final int MAX_FRACTION_DIGITS = 20;
@Nonnull @Nonnull
private static JsclMathEngine instance = new JsclMathEngine(); private static JsclMathEngine instance = new JsclMathEngine();
@ -38,8 +36,7 @@ public class JsclMathEngine implements MathEngine {
return new NumberFormatter(); return new NumberFormatter();
} }
}; };
@Nonnull private char groupingSeparator = GROUPING_SEPARATOR_DEFAULT;
private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault());
private boolean roundResult = false; private boolean roundResult = false;
private boolean scienceNotation = false; private boolean scienceNotation = false;
private int precision = 5; private int precision = 5;
@ -51,11 +48,6 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
private MessageRegistry messageRegistry = Messages.synchronizedMessageRegistry(new FixedCapacityListMessageRegistry(10)); private MessageRegistry messageRegistry = Messages.synchronizedMessageRegistry(new FixedCapacityListMessageRegistry(10));
{
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(GROUPING_SEPARATOR_DEFAULT.charAt(0));
}
public JsclMathEngine() { public JsclMathEngine() {
} }
@ -173,7 +165,7 @@ public class JsclMathEngine implements MathEngine {
} }
} }
final NumberFormatter nf = numberFormatter.get(); 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); nf.setPrecision(roundResult ? precision : NumberFormatter.DEFAULT_PRECISION);
if (scienceNotation) { if (scienceNotation) {
nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE); nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE);
@ -250,7 +242,7 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
public String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedDoubleValue) { public String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedDoubleValue) {
if (useGroupingSeparator) { 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("."); final int dotIndex = ungroupedDoubleValue.indexOf(".");
@ -302,10 +294,6 @@ public class JsclMathEngine implements MathEngine {
return result; return result;
} }
public void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols) {
this.decimalGroupSymbols = decimalGroupSymbols;
}
public void setRoundResult(boolean roundResult) { public void setRoundResult(boolean roundResult) {
this.roundResult = roundResult; this.roundResult = roundResult;
} }
@ -323,10 +311,10 @@ public class JsclMathEngine implements MathEngine {
} }
public char getGroupingSeparator() { public char getGroupingSeparator() {
return this.decimalGroupSymbols.getGroupingSeparator(); return this.groupingSeparator;
} }
public void setGroupingSeparator(char groupingSeparator) { public void setGroupingSeparator(char groupingSeparator) {
this.decimalGroupSymbols.setGroupingSeparator(groupingSeparator); this.groupingSeparator = groupingSeparator;
} }
} }

View File

@ -6,7 +6,6 @@ import jscl.math.operator.Operator;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.text.DecimalFormatSymbols;
public interface MathContext { public interface MathContext {
@ -36,8 +35,6 @@ public interface MathContext {
void setNumeralBase(@Nonnull NumeralBase numeralBase); void setNumeralBase(@Nonnull NumeralBase numeralBase);
void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols);
void setRoundResult(boolean roundResult); void setRoundResult(boolean roundResult);
void setPrecision(int precision); void setPrecision(int precision);