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 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<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));

View File

@ -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"));

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);