From 2ac061d8bacb7185b6982e407175b3f8bd9f2c05 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 2 May 2016 15:37:19 +0200 Subject: [PATCH] Remove scientificNotation preference and use Output.notation instead --- .../org/solovyev/android/calculator/Engine.java | 15 +++++++++------ .../calculator/wizard/CalculatorMode.java | 8 +++++--- jscl/src/main/java/jscl/JsclMathEngine.java | 16 ++++++---------- jscl/src/main/java/jscl/MathContext.java | 2 +- jscl/src/test/java/jscl/JsclMathEngineTest.java | 4 ++-- jscl/src/test/java/jscl/math/ExpressionTest.java | 15 ++++++++------- 6 files changed, 31 insertions(+), 29 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 ae17c886..cdf8263b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -189,10 +189,17 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene migratePreference(preferences, Preferences.numeralBase, "org.solovyev.android.calculator.CalculatorActivity_numeral_bases", editor); migratePreference(preferences, Preferences.angleUnit, "org.solovyev.android.calculator.CalculatorActivity_angle_units", editor); migratePreference(preferences, Preferences.Output.precision, "org.solovyev.android.calculator.CalculatorModel_result_precision", editor); - migratePreference(preferences, Preferences.Output.scientificNotation, "calculation.output.science_notation", editor); + if (preferences.contains("engine.output.science_notation")) { + final boolean scientific = preferences.getBoolean("engine.output.science_notation", false); + Preferences.Output.notation.putPreference(editor, scientific ? Notation.sci : Notation.dec); + } migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor); } else if (oldVersion == 1) { migratePreference(preferences, Preferences.Output.separator, "engine.groupingSeparator", editor); + if (preferences.contains("engine.output.scientificNotation")) { + final boolean scientific = preferences.getBoolean("engine.output.scientificNotation", false); + Preferences.Output.notation.putPreference(editor, scientific ? Notation.sci : Notation.dec); + } } Preferences.version.putDefault(editor); editor.apply(); @@ -220,7 +227,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); mathEngine.setPrecision(Preferences.Output.precision.getPreference(preferences)); - mathEngine.setScienceNotation(Preferences.Output.scientificNotation.getPreference(preferences)); + mathEngine.setNotation(Preferences.Output.notation.getPreference(preferences).id); mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences)); mathEngine.setGroupingSeparator(Preferences.Output.separator.getPreference(preferences)); @@ -277,7 +284,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene preferenceKeys.add(numeralBase.getKey()); preferenceKeys.add(angleUnit.getKey()); preferenceKeys.add(Output.precision.getKey()); - preferenceKeys.add(Output.scientificNotation.getKey()); preferenceKeys.add(Output.round.getKey()); preferenceKeys.add(Output.notation.getKey()); preferenceKeys.add(Output.separator.getKey()); @@ -290,12 +296,9 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene public static class Output { public static final StringPreference precision = StringPreference.ofTypedValue("engine.output.precision", "5", NumberMapper.of(Integer.class)); - // todo serso: remove - public static final BooleanPreference scientificNotation = BooleanPreference.of("engine.output.scientificNotation", false); public static final BooleanPreference round = BooleanPreference.of("engine.output.round", true); public static final StringPreference notation = StringPreference.ofEnum("engine.output.notation", Notation.dec, Notation.class); public static final CharacterPreference separator = CharacterPreference.of("engine.output.separator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); - } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java b/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java index 301d6bd3..6720ac8d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java +++ b/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java @@ -23,12 +23,14 @@ package org.solovyev.android.calculator.wizard; import android.content.SharedPreferences; -import jscl.AngleUnit; + import org.solovyev.android.calculator.Engine; import org.solovyev.android.calculator.Preferences; import javax.annotation.Nonnull; +import jscl.AngleUnit; + enum CalculatorMode { simple() { @@ -38,7 +40,7 @@ enum CalculatorMode { Preferences.Gui.mode.putPreference(editor, Preferences.Gui.Mode.simple); Engine.Preferences.angleUnit.putPreference(editor, AngleUnit.deg); - Engine.Preferences.Output.scientificNotation.putPreference(editor, false); + Engine.Preferences.Output.notation.putPreference(editor, Engine.Notation.dec); Engine.Preferences.Output.round.putPreference(editor, true); editor.apply(); @@ -52,7 +54,7 @@ enum CalculatorMode { Preferences.Gui.mode.putPreference(editor, Preferences.Gui.Mode.engineer); Engine.Preferences.angleUnit.putPreference(editor, AngleUnit.rad); - Engine.Preferences.Output.scientificNotation.putPreference(editor, true); + Engine.Preferences.Output.notation.putPreference(editor, Engine.Notation.eng); Engine.Preferences.Output.round.putPreference(editor, false); editor.apply(); diff --git a/jscl/src/main/java/jscl/JsclMathEngine.java b/jscl/src/main/java/jscl/JsclMathEngine.java index 00550d93..43fec261 100644 --- a/jscl/src/main/java/jscl/JsclMathEngine.java +++ b/jscl/src/main/java/jscl/JsclMathEngine.java @@ -51,7 +51,7 @@ public class JsclMathEngine implements MathEngine { }; private char groupingSeparator = GROUPING_SEPARATOR_NO; private boolean roundResult = false; - private int numberFormat = FSE_NONE; + private int notation = FSE_NONE; private int precision = 5; @Nonnull private AngleUnit angleUnits = DEFAULT_ANGLE_UNITS; @@ -183,7 +183,7 @@ public class JsclMathEngine implements MathEngine { final NumberFormatter nf = numberFormatter.get(); nf.setGroupingSeparator(hasGroupingSeparator() ? getGroupingSeparatorChar(nb) : NumberFormatter.NO_GROUPING); nf.setPrecision(roundResult ? precision : NumberFormatter.NO_ROUNDING); - switch (numberFormat) { + switch (notation) { case FSE_ENG: nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE); break; @@ -355,15 +355,11 @@ public class JsclMathEngine implements MathEngine { this.precision = precision; } - public void setScienceNotation(boolean scienceNotation) { - setNumberFormat(scienceNotation ? FSE_SCI : FSE_NONE); - } - - public void setNumberFormat(int numberFormat) { - if (numberFormat != FSE_SCI && numberFormat != FSE_ENG && numberFormat != FSE_NONE) { - throw new IllegalArgumentException("Unsupported format: " + numberFormat); + public void setNotation(int notation) { + if (notation != FSE_SCI && notation != FSE_ENG && notation != FSE_NONE) { + throw new IllegalArgumentException("Unsupported notation: " + notation); } - this.numberFormat = numberFormat; + this.notation = notation; } public char getGroupingSeparator() { diff --git a/jscl/src/main/java/jscl/MathContext.java b/jscl/src/main/java/jscl/MathContext.java index 1b298121..f71c3d3e 100644 --- a/jscl/src/main/java/jscl/MathContext.java +++ b/jscl/src/main/java/jscl/MathContext.java @@ -55,5 +55,5 @@ public interface MathContext { @Nonnull String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedIntValue); - void setScienceNotation(boolean scienceNotation); + void setNotation(int notation); } diff --git a/jscl/src/test/java/jscl/JsclMathEngineTest.java b/jscl/src/test/java/jscl/JsclMathEngineTest.java index b6336305..49eec1f8 100644 --- a/jscl/src/test/java/jscl/JsclMathEngineTest.java +++ b/jscl/src/test/java/jscl/JsclMathEngineTest.java @@ -107,7 +107,7 @@ public class JsclMathEngineTest { @Test public void testEngineeringNotationWithRounding() throws Exception { - me.setNumberFormat(Real.NumberFormat.FSE_ENG); + me.setNotation(Real.NumberFormat.FSE_ENG); me.setRoundResult(true); me.setPrecision(5); @@ -163,7 +163,7 @@ public class JsclMathEngineTest { @Test public void testEngineeringNotationWithoutRounding() throws Exception { - me.setNumberFormat(Real.NumberFormat.FSE_ENG); + me.setNotation(Real.NumberFormat.FSE_ENG); me.setRoundResult(false); assertEquals("10E6", me.format(10000000d)); diff --git a/jscl/src/test/java/jscl/math/ExpressionTest.java b/jscl/src/test/java/jscl/math/ExpressionTest.java index 416d754a..7e76fb0b 100644 --- a/jscl/src/test/java/jscl/math/ExpressionTest.java +++ b/jscl/src/test/java/jscl/math/ExpressionTest.java @@ -21,6 +21,7 @@ import jscl.math.function.Constant; import jscl.math.function.ExtendedConstant; import jscl.math.function.IConstant; import jscl.text.ParseException; +import midpcalc.Real; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -797,17 +798,17 @@ public class ExpressionTest { assertEquals("0.000001222", Expression.valueOf("1222/(10^9)").numeric().toString()); assertEquals("12 345", JsclInteger.valueOf(12345L).toString()); - me.setScienceNotation(true); + me.setNotation(Real.NumberFormat.FSE_SCI); assertEquals("0", Expression.valueOf("0.0").simplify().toString()); assertEquals("1", Expression.valueOf("1.0").simplify().toString()); assertEquals("100", Expression.valueOf("100.0").simplify().toString()); - me.setScienceNotation(false); + me.setNotation(Real.NumberFormat.FSE_NONE); me.setRoundResult(true); me.setPrecision(5); assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString()); - me.setScienceNotation(true); + me.setNotation(Real.NumberFormat.FSE_SCI); me.setRoundResult(true); me.setPrecision(5); assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); @@ -819,24 +820,24 @@ public class ExpressionTest { me.setRoundResult(false); assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); - me.setScienceNotation(false); + me.setNotation(Real.NumberFormat.FSE_NONE); assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); - me.setScienceNotation(true); + me.setNotation(Real.NumberFormat.FSE_SCI); assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); me.setRoundResult(true); me.setPrecision(10); assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); - me.setScienceNotation(false); + me.setNotation(Real.NumberFormat.FSE_NONE); me.setRoundResult(true); me.setPrecision(10); assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); } finally { me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_NO); - me.setScienceNotation(false); + me.setNotation(Real.NumberFormat.FSE_NONE); me.setRoundResult(false); } }