diff --git a/calculatorpp-core/pom.xml b/calculatorpp-core/pom.xml index 4e954233..ad384cd1 100644 --- a/calculatorpp-core/pom.xml +++ b/calculatorpp-core/pom.xml @@ -1,46 +1,58 @@ - - - 4.0.0 - - - org.solovyev.android - calculatorpp-parent - 1.3.2 - - - org.solovyev.android - calculatorpp-core - 1.3.2 - Calculator++ Application Core - - jar - - - - - junit - junit - test - - - - com.intellij - annotations - - - - org.solovyev - jscl - - - - org.simpleframework - simple-xml - - - - - + + + 4.0.0 + + + org.solovyev.android + calculatorpp-parent + 1.3.2 + + + org.solovyev.android + calculatorpp-core + 1.3.2 + Calculator++ Application Core + + jar + + + + + org.solovyev + common-text + + + + junit + junit + test + + + + org.mockito + mockito-core + 1.9.0 + test + + + + com.intellij + annotations + + + + org.solovyev + jscl + + + + org.simpleframework + simple-xml + + + + + \ No newline at end of file diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/AbstractCalculatorMathRegistry.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/AbstractCalculatorMathRegistry.java new file mode 100644 index 00000000..b8be5c95 --- /dev/null +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/AbstractCalculatorMathRegistry.java @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.JBuilder; +import org.solovyev.common.math.MathEntity; +import org.solovyev.common.math.MathRegistry; + +import java.util.List; +import java.util.Map; + +/** + * User: serso + * Date: 10/30/11 + * Time: 1:03 AM + */ +public abstract class AbstractCalculatorMathRegistry implements CalculatorMathRegistry { + + @NotNull + private final MathRegistry mathRegistry; + + @NotNull + private final String prefix; + + @NotNull + private final MathEntityDao

mathEntityDao; + + protected AbstractCalculatorMathRegistry(@NotNull MathRegistry mathRegistry, + @NotNull String prefix, + @NotNull MathEntityDao

mathEntityDao) { + this.mathRegistry = mathRegistry; + this.prefix = prefix; + this.mathEntityDao = mathEntityDao; + } + + + + @NotNull + protected abstract Map getSubstitutes(); + + @Nullable + @Override + public String getDescription(@NotNull String mathEntityName) { + final String stringName; + + final Map substitutes = getSubstitutes(); + final String substitute = substitutes.get(mathEntityName); + if (substitute == null) { + stringName = prefix + mathEntityName; + } else { + stringName = prefix + substitute; + } + + return mathEntityDao.getDescription(stringName); + } + + public synchronized void load() { + final MathEntityPersistenceContainer

persistenceContainer = mathEntityDao.load(); + + if (persistenceContainer != null) { + for (P entity : persistenceContainer.getEntities()) { + if (!contains(entity.getName())) { + add(createBuilder(entity)); + } + } + } + + /*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!"); + for (Var var : vars) { + Log.d(AndroidVarsRegistry.class.getName(), var.toString()); + }*/ + } + + @NotNull + protected abstract JBuilder createBuilder(@NotNull P entity); + + + @Override + public synchronized void save() { + final MathEntityPersistenceContainer

container = createPersistenceContainer(); + + for (T entity : this.getEntities()) { + if (!entity.isSystem()) { + final P persistenceEntity = transform(entity); + if (persistenceEntity != null) { + container.getEntities().add(persistenceEntity); + } + } + } + + this.mathEntityDao.save(container); + } + + @Nullable + protected abstract P transform(@NotNull T entity); + + @NotNull + protected abstract MathEntityPersistenceContainer

createPersistenceContainer(); + + @NotNull + @Override + public List getEntities() { + return mathRegistry.getEntities(); + } + + @NotNull + @Override + public List getSystemEntities() { + return mathRegistry.getSystemEntities(); + } + + @Override + public T add(@NotNull JBuilder JBuilder) { + return mathRegistry.add(JBuilder); + } + + @Override + public void remove(@NotNull T var) { + mathRegistry.remove(var); + } + + @NotNull + @Override + public List getNames() { + return mathRegistry.getNames(); + } + + @Override + public boolean contains(@NotNull String name) { + return mathRegistry.contains(name); + } + + @Override + public T get(@NotNull String name) { + return mathRegistry.get(name); + } + + @Override + public T getById(@NotNull Integer id) { + return mathRegistry.getById(id); + } +} diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathEntityDao.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathEntityDao.java new file mode 100644 index 00000000..110d73d2 --- /dev/null +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathEntityDao.java @@ -0,0 +1,20 @@ +package org.solovyev.android.calculator; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * User: serso + * Date: 10/7/12 + * Time: 6:43 PM + */ +public interface MathEntityDao { + + void save(@NotNull MathEntityPersistenceContainer container); + + @Nullable + MathEntityPersistenceContainer load(); + + @Nullable + String getDescription(@NotNull String descriptionId); +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/MathEntityPersistenceContainer.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathEntityPersistenceContainer.java similarity index 68% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/model/MathEntityPersistenceContainer.java rename to calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathEntityPersistenceContainer.java index 7406b223..1a7b7f07 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/MathEntityPersistenceContainer.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathEntityPersistenceContainer.java @@ -1,4 +1,4 @@ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator; import java.util.List; @@ -8,5 +8,7 @@ import java.util.List; * Time: 5:03 PM */ public interface MathEntityPersistenceContainer { - public List getEntities(); + + public List getEntities(); + } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/MathPersistenceEntity.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathPersistenceEntity.java similarity index 87% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/model/MathPersistenceEntity.java rename to calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathPersistenceEntity.java index d3912862..40aa08cd 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/MathPersistenceEntity.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/MathPersistenceEntity.java @@ -4,7 +4,7 @@ * or visit http://se.solovyev.org */ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator; import org.jetbrains.annotations.NotNull; diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/math/MathType.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/math/MathType.java index ddfe896e..88979204 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -354,7 +354,7 @@ public enum MathType { final String s = CollectionsUtils.find(mathType.getTokens(), startsWithFinder); if (s != null) { if ( s.length() == 1 ) { - if (hexMode || JsclMathEngine.instance.getNumeralBase() == NumeralBase.hex) { + if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) { final Character ch = s.charAt(0); if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) ) { return new Result(MathType.digit, s); diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AFunction.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java similarity index 96% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/model/AFunction.java rename to calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java index 16889278..24df6db6 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AFunction.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; +import org.solovyev.android.calculator.MathPersistenceEntity; import org.solovyev.common.collections.CollectionsUtils; import org.solovyev.common.text.CollectionTransformations; import org.solovyev.common.text.StringMapper; diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Functions.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/Functions.java similarity index 88% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/model/Functions.java rename to calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/Functions.java index 35ae2b76..fd674f98 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Functions.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/Functions.java @@ -1,10 +1,9 @@ package org.solovyev.android.calculator.model; import jscl.math.function.CustomFunction; -import jscl.math.function.Function; -import jscl.math.function.IConstant; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; +import org.solovyev.android.calculator.MathEntityPersistenceContainer; import java.util.ArrayList; import java.util.List; diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java new file mode 100644 index 00000000..c4b67ff3 --- /dev/null +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java @@ -0,0 +1,32 @@ +package org.solovyev.android.calculator; + +import jscl.JsclMathEngine; +import org.jetbrains.annotations.NotNull; +import org.mockito.Mockito; +import org.solovyev.android.calculator.history.CalculatorHistory; + +/** + * User: serso + * Date: 10/7/12 + * Time: 6:30 PM + */ +public class AbstractCalculatorTest { + + protected static void staticSetUp() throws Exception { + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); + } + + protected void setUp() throws Exception { + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); + } + + @NotNull + private static CalculatorEngineImpl newCalculatorEngine() { + final CalculatorMathRegistry varsRegistry = Mockito.mock(CalculatorMathRegistry.class); + //Mockito.when(varsRegistry.get()) + + final CalculatorEngineImpl result = new CalculatorEngineImpl(JsclMathEngine.getInstance(), varsRegistry, Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), null); + result.init(); + return result; + } +} diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java index 3313fd02..ae3fcbaf 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java @@ -1,200 +1,201 @@ -package org.solovyev.android.calculator; - -import junit.framework.Assert; -import org.jetbrains.annotations.NotNull; -import org.junit.Before; -import org.junit.Test; - -/** - * User: Solovyev_S - * Date: 21.09.12 - * Time: 12:44 - */ -public class CalculatorEditorImplTest { - - @NotNull - private CalculatorEditor calculatorEditor; - - @Before - public void setUp() throws Exception { - this.calculatorEditor = new CalculatorEditorImpl(new CalculatorImpl()); - } - - @Test - public void testInsert() throws Exception { - CalculatorEditorViewState viewState = this.calculatorEditor.getViewState(); - - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.insert(""); - - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.insert("test"); - - Assert.assertEquals("test", viewState.getText()); - Assert.assertEquals(4, viewState.getSelection()); - - viewState = this.calculatorEditor.insert("test"); - Assert.assertEquals("testtest", viewState.getText()); - Assert.assertEquals(8, viewState.getSelection()); - - viewState = this.calculatorEditor.insert(""); - Assert.assertEquals("testtest", viewState.getText()); - Assert.assertEquals(8, viewState.getSelection()); - - viewState = this.calculatorEditor.insert("1234567890"); - Assert.assertEquals("testtest1234567890", viewState.getText()); - Assert.assertEquals(18, viewState.getSelection()); - - viewState = this.calculatorEditor.moveCursorLeft(); - viewState = this.calculatorEditor.insert("9"); - Assert.assertEquals("testtest12345678990", viewState.getText()); - Assert.assertEquals(18, viewState.getSelection()); - - viewState = this.calculatorEditor.setCursorOnStart(); - viewState = this.calculatorEditor.insert("9"); - Assert.assertEquals("9testtest12345678990", viewState.getText()); - Assert.assertEquals(1, viewState.getSelection()); - - viewState = this.calculatorEditor.erase(); - viewState = this.calculatorEditor.insert("9"); - Assert.assertEquals("9testtest12345678990", viewState.getText()); - Assert.assertEquals(1, viewState.getSelection()); - } - - @Test - public void testErase() throws Exception { - this.calculatorEditor.setText(""); - this.calculatorEditor.erase(); - - Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.setText("test"); - this.calculatorEditor.erase(); - Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.erase(); - Assert.assertEquals("te", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.erase(); - Assert.assertEquals("t", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.erase(); - Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.erase(); - Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.setText("1234"); - this.calculatorEditor.moveCursorLeft(); - this.calculatorEditor.erase(); - Assert.assertEquals("124", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.erase(); - Assert.assertEquals("14", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.erase(); - Assert.assertEquals("4", this.calculatorEditor.getViewState().getText()); - - this.calculatorEditor.setText("1"); - this.calculatorEditor.moveCursorLeft(); - this.calculatorEditor.erase(); - Assert.assertEquals("1", this.calculatorEditor.getViewState().getText()); - } - - @Test - public void testMoveSelection() throws Exception { - this.calculatorEditor.setText(""); - - CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(2); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(100); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(-3); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(-100); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("0123456789"); - - viewState = this.calculatorEditor.moveSelection(0); - Assert.assertEquals(10, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(1); - Assert.assertEquals(10, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(-2); - Assert.assertEquals(8, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(1); - Assert.assertEquals(9, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(-9); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(-10); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(2); - Assert.assertEquals(2, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(2); - Assert.assertEquals(4, viewState.getSelection()); - - viewState = this.calculatorEditor.moveSelection(-6); - Assert.assertEquals(0, viewState.getSelection()); - } - - @Test - public void testSetText() throws Exception { - CalculatorEditorViewState viewState = this.calculatorEditor.setText("test"); - - Assert.assertEquals("test", viewState.getText()); - Assert.assertEquals(4, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("testtest"); - Assert.assertEquals("testtest", viewState.getText()); - Assert.assertEquals(8, viewState.getSelection()); - - viewState = this.calculatorEditor.setText(""); - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("testtest", 0); - Assert.assertEquals("testtest", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("testtest", 2); - Assert.assertEquals("testtest", viewState.getText()); - Assert.assertEquals(2, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("", 0); - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("", 3); - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("", -3); - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("test"); - Assert.assertEquals("test", viewState.getText()); - Assert.assertEquals(4, viewState.getSelection()); - - viewState = this.calculatorEditor.setText("", 2); - Assert.assertEquals("", viewState.getText()); - Assert.assertEquals(0, viewState.getSelection()); - } -} +package org.solovyev.android.calculator; + +import junit.framework.Assert; +import org.jetbrains.annotations.NotNull; +import org.junit.Before; +import org.junit.Test; + +/** + * User: Solovyev_S + * Date: 21.09.12 + * Time: 12:44 + */ +public class CalculatorEditorImplTest extends AbstractCalculatorTest { + + @NotNull + private CalculatorEditor calculatorEditor; + + @Before + public void setUp() throws Exception { + super.setUp(); + this.calculatorEditor = new CalculatorEditorImpl(CalculatorLocatorImpl.getInstance().getCalculator()); + } + + @Test + public void testInsert() throws Exception { + CalculatorEditorViewState viewState = this.calculatorEditor.getViewState(); + + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.insert(""); + + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.insert("test"); + + Assert.assertEquals("test", viewState.getText()); + Assert.assertEquals(4, viewState.getSelection()); + + viewState = this.calculatorEditor.insert("test"); + Assert.assertEquals("testtest", viewState.getText()); + Assert.assertEquals(8, viewState.getSelection()); + + viewState = this.calculatorEditor.insert(""); + Assert.assertEquals("testtest", viewState.getText()); + Assert.assertEquals(8, viewState.getSelection()); + + viewState = this.calculatorEditor.insert("1234567890"); + Assert.assertEquals("testtest1234567890", viewState.getText()); + Assert.assertEquals(18, viewState.getSelection()); + + viewState = this.calculatorEditor.moveCursorLeft(); + viewState = this.calculatorEditor.insert("9"); + Assert.assertEquals("testtest12345678990", viewState.getText()); + Assert.assertEquals(18, viewState.getSelection()); + + viewState = this.calculatorEditor.setCursorOnStart(); + viewState = this.calculatorEditor.insert("9"); + Assert.assertEquals("9testtest12345678990", viewState.getText()); + Assert.assertEquals(1, viewState.getSelection()); + + viewState = this.calculatorEditor.erase(); + viewState = this.calculatorEditor.insert("9"); + Assert.assertEquals("9testtest12345678990", viewState.getText()); + Assert.assertEquals(1, viewState.getSelection()); + } + + @Test + public void testErase() throws Exception { + this.calculatorEditor.setText(""); + this.calculatorEditor.erase(); + + Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.setText("test"); + this.calculatorEditor.erase(); + Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.erase(); + Assert.assertEquals("te", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.erase(); + Assert.assertEquals("t", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.erase(); + Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.erase(); + Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.setText("1234"); + this.calculatorEditor.moveCursorLeft(); + this.calculatorEditor.erase(); + Assert.assertEquals("124", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.erase(); + Assert.assertEquals("14", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.erase(); + Assert.assertEquals("4", this.calculatorEditor.getViewState().getText()); + + this.calculatorEditor.setText("1"); + this.calculatorEditor.moveCursorLeft(); + this.calculatorEditor.erase(); + Assert.assertEquals("1", this.calculatorEditor.getViewState().getText()); + } + + @Test + public void testMoveSelection() throws Exception { + this.calculatorEditor.setText(""); + + CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(2); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(100); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(-3); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(-100); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("0123456789"); + + viewState = this.calculatorEditor.moveSelection(0); + Assert.assertEquals(10, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(1); + Assert.assertEquals(10, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(-2); + Assert.assertEquals(8, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(1); + Assert.assertEquals(9, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(-9); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(-10); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(2); + Assert.assertEquals(2, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(2); + Assert.assertEquals(4, viewState.getSelection()); + + viewState = this.calculatorEditor.moveSelection(-6); + Assert.assertEquals(0, viewState.getSelection()); + } + + @Test + public void testSetText() throws Exception { + CalculatorEditorViewState viewState = this.calculatorEditor.setText("test"); + + Assert.assertEquals("test", viewState.getText()); + Assert.assertEquals(4, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("testtest"); + Assert.assertEquals("testtest", viewState.getText()); + Assert.assertEquals(8, viewState.getSelection()); + + viewState = this.calculatorEditor.setText(""); + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("testtest", 0); + Assert.assertEquals("testtest", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("testtest", 2); + Assert.assertEquals("testtest", viewState.getText()); + Assert.assertEquals(2, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("", 0); + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("", 3); + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("", -3); + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("test"); + Assert.assertEquals("test", viewState.getText()); + Assert.assertEquals(4, viewState.getSelection()); + + viewState = this.calculatorEditor.setText("", 2); + Assert.assertEquals("", viewState.getText()); + Assert.assertEquals(0, viewState.getSelection()); + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java similarity index 93% rename from calculatorpp/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java rename to calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java index 3e581663..2d678522 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java @@ -9,18 +9,18 @@ package org.solovyev.android.calculator.math; import junit.framework.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.AbstractCalculatorTest; /** * User: serso * Date: 10/5/11 * Time: 1:25 AM */ -public class MathTypeTest { +public class MathTypeTest extends AbstractCalculatorTest { @BeforeClass - public static void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().getEngine().init(); + public static void staticSetUp() throws Exception { + AbstractCalculatorTest.staticSetUp(); } @Test diff --git a/calculatorpp/pom.xml b/calculatorpp/pom.xml index b90fa62c..69e4a62e 100644 --- a/calculatorpp/pom.xml +++ b/calculatorpp/pom.xml @@ -156,6 +156,13 @@ annotations + + com.pivotallabs + robolectric + 1.1 + test + + diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java new file mode 100644 index 00000000..59b0cb75 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java @@ -0,0 +1,39 @@ +package org.solovyev.android.calculator; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * User: serso + * Date: 10/7/12 + * Time: 7:17 PM + */ +public enum AndroidFunctionCategory { + + trigonometric(R.string.c_fun_category_trig), + hyperbolic_trigonometric(R.string.c_fun_category_hyper_trig), + comparison(R.string.c_fun_category_comparison), + my(R.string.c_fun_category_my), + common(R.string.c_fun_category_common); + + private final int captionId; + + AndroidFunctionCategory(int captionId) { + this.captionId = captionId; + } + + public int getCaptionId() { + return captionId; + } + + @Nullable + public AndroidFunctionCategory valueOf( @NotNull FunctionCategory functionCategory ) { + for (AndroidFunctionCategory androidFunctionCategory : values()) { + if ( androidFunctionCategory.name().equals(functionCategory.name()) ) { + return androidFunctionCategory; + } + } + + return null; + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidFunctionsMathRegistry.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidFunctionsMathRegistry.java new file mode 100644 index 00000000..bfcc919f --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidFunctionsMathRegistry.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator; + +import jscl.math.function.CustomFunction; +import jscl.math.function.Function; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.model.AFunction; +import org.solovyev.android.calculator.model.Functions; +import org.solovyev.common.JBuilder; +import org.solovyev.common.math.MathRegistry; + +import java.util.HashMap; +import java.util.Map; + +/** + * User: serso + * Date: 11/17/11 + * Time: 11:28 PM + */ +public class AndroidFunctionsMathRegistry extends AbstractCalculatorMathRegistry { + + @NotNull + private static final Map substitutes = new HashMap(); + static { + substitutes.put("√", "sqrt"); + } + + @NotNull + private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_"; + + public AndroidFunctionsMathRegistry(@NotNull MathRegistry functionsRegistry, + @NotNull MathEntityDao mathEntityDao) { + super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao); + } + + @Override + public void load() { + super.load(); + + add(new CustomFunction.Builder(true, "log", new String[]{"base", "x"}, "ln(x)/ln(base)")); + } + + @NotNull + @Override + protected Map getSubstitutes() { + return substitutes; + } + + @Override + public String getCategory(@NotNull Function function) { + for (FunctionCategory category : FunctionCategory.values()) { + if ( category.isInCategory(function) ) { + return category.name(); + } + } + + return null; + } + + @NotNull + @Override + protected JBuilder createBuilder(@NotNull AFunction entity) { + return new CustomFunction.Builder(entity.getName(), entity.getParameterNamesAsArray(), entity.getContent()); + } + + @Override + protected AFunction transform(@NotNull Function entity) { + if (entity instanceof CustomFunction) { + final AFunction result = new AFunction(); + result.setName(entity.getName()); + result.setContent(((CustomFunction) entity).getContent()); + result.setParameterNames(((CustomFunction) entity).getParameterNames()); + return result; + } else { + return null; + } + } + + @NotNull + @Override + protected MathEntityPersistenceContainer createPersistenceContainer() { + return new Functions(); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/FunctionCategory.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/FunctionCategory.java new file mode 100644 index 00000000..f3bfcd5c --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/FunctionCategory.java @@ -0,0 +1,92 @@ +package org.solovyev.android.calculator; + +import jscl.math.function.ArcTrigonometric; +import jscl.math.function.Comparison; +import jscl.math.function.Function; +import jscl.math.function.Trigonometric; +import org.jetbrains.annotations.NotNull; +import org.solovyev.common.collections.CollectionsUtils; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** +* User: serso +* Date: 10/7/12 +* Time: 7:15 PM +*/ +public enum FunctionCategory { + + trigonometric(100){ + @Override + public boolean isInCategory(@NotNull Function function) { + return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function); + } + }, + + hyperbolic_trigonometric(300) { + + private final List names = Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth"); + + @Override + public boolean isInCategory(@NotNull Function function) { + return names.contains(function.getName()); + } + }, + + comparison(200) { + @Override + public boolean isInCategory(@NotNull Function function) { + return function instanceof Comparison; + } + }, + + my(0) { + @Override + public boolean isInCategory(@NotNull Function function) { + return !function.isSystem(); + } + }, + + common(50) { + @Override + public boolean isInCategory(@NotNull Function function) { + for (FunctionCategory category : values()) { + if ( category != this ) { + if ( category.isInCategory(function) ) { + return false; + } + } + } + + return true; + } + }; + + private final int tabOrder; + + FunctionCategory(int tabOrder) { + this.tabOrder = tabOrder; + } + + public abstract boolean isInCategory(@NotNull Function function); + + @NotNull + public static List getCategoriesByTabOrder() { + final List result = CollectionsUtils.asList(FunctionCategory.values()); + + Collections.sort(result, new Comparator() { + @Override + public int compare(FunctionCategory category, FunctionCategory category1) { + return category.tabOrder - category1.tabOrder; + } + }); + + // todo serso: current solution (as creating functions is not implemented yet) + result.remove(my); + + return result; + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java index 76d785b6..263ae6a7 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java @@ -1,79 +1,78 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.os.Bundle; -import com.actionbarsherlock.app.SherlockFragmentActivity; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.about.CalculatorFragmentType; -import org.solovyev.android.calculator.history.CalculatorHistoryActivity; -import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; - -/** - * User: serso - * Date: 12/21/11 - * Time: 10:33 PM - */ -public class CalculatorFunctionsActivity extends SherlockFragmentActivity implements CalculatorEventListener { - - @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - activityHelper.onCreate(this, savedInstanceState); - - final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions; - - for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) { - activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout); - } - - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - activityHelper.onSaveInstanceState(this, outState); - } - - @Override - protected void onResume() { - super.onResume(); - - activityHelper.onResume(this); - } - - @Override - protected void onPause() { - this.activityHelper.onPause(this); - - super.onPause(); - } - - - @Override - protected void onDestroy() { - super.onDestroy(); - - this.activityHelper.onDestroy(this); - } - - @Override - public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { - switch (calculatorEventType) { - case use_function: - this.finish(); - break; - } - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.os.Bundle; +import com.actionbarsherlock.app.SherlockFragmentActivity; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.calculator.history.CalculatorHistoryActivity; + +/** + * User: serso + * Date: 12/21/11 + * Time: 10:33 PM + */ +public class CalculatorFunctionsActivity extends SherlockFragmentActivity implements CalculatorEventListener { + + @NotNull + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + activityHelper.onCreate(this, savedInstanceState); + + final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions; + + for (FunctionCategory category : FunctionCategory.getCategoriesByTabOrder()) { + activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout); + } + + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + activityHelper.onSaveInstanceState(this, outState); + } + + @Override + protected void onResume() { + super.onResume(); + + activityHelper.onResume(this); + } + + @Override + protected void onPause() { + this.activityHelper.onPause(this); + + super.onPause(); + } + + + @Override + protected void onDestroy() { + super.onDestroy(); + + this.activityHelper.onDestroy(this); + } + + @Override + public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { + switch (calculatorEventType) { + case use_function: + this.finish(); + break; + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java deleted file mode 100644 index 5305a3ce..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.model; - -import android.app.Application; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.preference.PreferenceManager; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.simpleframework.xml.Serializer; -import org.simpleframework.xml.core.Persister; -import org.solovyev.android.calculator.CalculatorMathRegistry; -import org.solovyev.android.calculator.R; -import org.solovyev.common.JBuilder; -import org.solovyev.common.math.MathEntity; -import org.solovyev.common.math.MathRegistry; - -import java.io.StringWriter; -import java.util.List; -import java.util.Map; - -/** - * User: serso - * Date: 10/30/11 - * Time: 1:03 AM - */ -public abstract class AbstractAndroidMathRegistry implements CalculatorMathRegistry { - - @NotNull - private final MathRegistry mathRegistry; - - @NotNull - private final String prefix; - - @NotNull - private final Context context; - - protected AbstractAndroidMathRegistry(@NotNull MathRegistry mathRegistry, - @NotNull String prefix, - @NotNull Application application) { - this.mathRegistry = mathRegistry; - this.prefix = prefix; - this.context = application; - } - - - - @NotNull - protected abstract Map getSubstitutes(); - - @Nullable - @Override - public String getDescription(@NotNull String mathEntityName) { - final String stringName; - - final Map substitutes = getSubstitutes(); - final String substitute = substitutes.get(mathEntityName); - if (substitute == null) { - stringName = prefix + mathEntityName; - } else { - stringName = prefix + substitute; - } - - final Resources resources = context.getResources(); - final int stringId = resources.getIdentifier(stringName, "string", R.class.getPackage().getName()); - try { - return resources.getString(stringId); - } catch (Resources.NotFoundException e) { - return null; - } - } - - public synchronized void load() { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - - if (preferences != null) { - final Integer preferenceStringId = getPreferenceStringId(); - if (preferenceStringId != null) { - final String value = preferences.getString(context.getString(preferenceStringId), null); - if (value != null) { - final Serializer serializer = new Persister(); - try { - final MathEntityPersistenceContainer

persistenceContainer = serializer.read(getPersistenceContainerClass(), value); - for (P entity : persistenceContainer.getEntities()) { - if (!contains(entity.getName())) { - add(createBuilder(entity)); - } - } - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - } - - /*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!"); - for (Var var : vars) { - Log.d(AndroidVarsRegistry.class.getName(), var.toString()); - }*/ - } - - @NotNull - protected abstract JBuilder createBuilder(@NotNull P entity); - - @NotNull - protected abstract Class> getPersistenceContainerClass(); - - @Nullable - protected abstract Integer getPreferenceStringId(); - - - @Override - public synchronized void save() { - final Integer preferenceStringId = getPreferenceStringId(); - - if (preferenceStringId != null) { - final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); - final SharedPreferences.Editor editor = settings.edit(); - - final MathEntityPersistenceContainer

container = createPersistenceContainer(); - for (T entity : this.getEntities()) { - if (!entity.isSystem()) { - final P persistenceEntity = transform(entity); - if (persistenceEntity != null) { - container.getEntities().add(persistenceEntity); - } - } - } - - final StringWriter sw = new StringWriter(); - final Serializer serializer = new Persister(); - try { - serializer.write(container, sw); - } catch (Exception e) { - throw new RuntimeException(e); - } - - editor.putString(context.getString(preferenceStringId), sw.toString()); - - editor.commit(); - } - } - - @Nullable - protected abstract P transform(@NotNull T entity); - - @NotNull - protected abstract MathEntityPersistenceContainer

createPersistenceContainer(); - - @NotNull - @Override - public List getEntities() { - return mathRegistry.getEntities(); - } - - @NotNull - @Override - public List getSystemEntities() { - return mathRegistry.getSystemEntities(); - } - - @Override - public T add(@NotNull JBuilder JBuilder) { - return mathRegistry.add(JBuilder); - } - - @Override - public void remove(@NotNull T var) { - mathRegistry.remove(var); - } - - @NotNull - @Override - public List getNames() { - return mathRegistry.getNames(); - } - - @Override - public boolean contains(@NotNull String name) { - return mathRegistry.contains(name); - } - - @Override - public T get(@NotNull String name) { - return mathRegistry.get(name); - } - - @Override - public T getById(@NotNull Integer id) { - return mathRegistry.getById(id); - } -} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java index 14a8a772..2e5414ed 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java @@ -17,10 +17,7 @@ import jscl.math.function.Function; import jscl.math.function.IConstant; import jscl.math.operator.Operator; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorEngine; -import org.solovyev.android.calculator.CalculatorEngineImpl; -import org.solovyev.android.calculator.CalculatorMathEngine; -import org.solovyev.android.calculator.CalculatorMathRegistry; +import org.solovyev.android.calculator.*; import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.StringPreference; @@ -109,12 +106,12 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen this.lock = new Object(); - final JsclMathEngine engine = JsclMathEngine.instance; + final JsclMathEngine engine = JsclMathEngine.getInstance(); this.calculatorEngine = new CalculatorEngineImpl(engine, - new AndroidVarsRegistryImpl(engine.getConstantsRegistry(), application), - new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry(), application), - new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry(), application), - new AndroidPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), application), + new AndroidVarsRegistryImpl(engine.getConstantsRegistry(), new AndroidMathEntityDao(R.string.p_calc_vars, application, Vars.class)), + new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao(R.string.p_calc_functions, application, Functions.class)), + new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry(), new AndroidMathEntityDao(null, application, null)), + new AndroidPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao(null, application, null)), this.lock); } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsMathRegistry.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsMathRegistry.java deleted file mode 100644 index 6570398d..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsMathRegistry.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.model; - -import android.app.Application; -import jscl.math.function.*; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.R; -import org.solovyev.common.JBuilder; -import org.solovyev.common.collections.CollectionsUtils; -import org.solovyev.common.math.MathRegistry; - -import java.util.*; - -/** - * User: serso - * Date: 11/17/11 - * Time: 11:28 PM - */ -public class AndroidFunctionsMathRegistry extends AbstractAndroidMathRegistry { - - public static enum Category { - - trigonometric(R.string.c_fun_category_trig, 100){ - @Override - boolean isInCategory(@NotNull Function function) { - return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function); - } - }, - - hyperbolic_trigonometric(R.string.c_fun_category_hyper_trig, 300) { - - private final List names = Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth"); - - @Override - boolean isInCategory(@NotNull Function function) { - return names.contains(function.getName()); - } - }, - - comparison(R.string.c_fun_category_comparison, 200) { - @Override - boolean isInCategory(@NotNull Function function) { - return function instanceof Comparison; - } - }, - - my(R.string.c_fun_category_my, 0) { - @Override - boolean isInCategory(@NotNull Function function) { - return !function.isSystem(); - } - }, - - common(R.string.c_fun_category_common, 50) { - @Override - boolean isInCategory(@NotNull Function function) { - for (Category category : values()) { - if ( category != this ) { - if ( category.isInCategory(function) ) { - return false; - } - } - } - - return true; - } - }; - - private final int captionId; - - private final int tabOrder; - - Category(int captionId, int tabOrder) { - this.captionId = captionId; - this.tabOrder = tabOrder; - } - - public int getCaptionId() { - return captionId; - } - - abstract boolean isInCategory(@NotNull Function function); - - @NotNull - public static List getCategoriesByTabOrder() { - final List result = CollectionsUtils.asList(Category.values()); - - Collections.sort(result, new Comparator() { - @Override - public int compare(Category category, Category category1) { - return category.tabOrder - category1.tabOrder; - } - }); - - // todo serso: current solution (as creating functions is not implemented yet) - result.remove(my); - - return result; - } - } - - @NotNull - private static final Map substitutes = new HashMap(); - static { - substitutes.put("√", "sqrt"); - } - - @NotNull - private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_"; - - public AndroidFunctionsMathRegistry(@NotNull MathRegistry functionsRegistry, - @NotNull Application application) { - super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, application); - } - - @Override - public void load() { - super.load(); - - add(new CustomFunction.Builder(true, "log", new String[]{"base", "x"}, "ln(x)/ln(base)")); - } - - @NotNull - @Override - protected Map getSubstitutes() { - return substitutes; - } - - @Override - public String getCategory(@NotNull Function function) { - for (Category category : Category.values()) { - if ( category.isInCategory(function) ) { - return category.name(); - } - } - - return null; - } - - @NotNull - @Override - protected JBuilder createBuilder(@NotNull AFunction entity) { - return new CustomFunction.Builder(entity.getName(), entity.getParameterNamesAsArray(), entity.getContent()); - } - - @NotNull - @Override - protected Class> getPersistenceContainerClass() { - return Functions.class; - } - - @Override - protected Integer getPreferenceStringId() { - return R.string.p_calc_functions; - } - - @Override - protected AFunction transform(@NotNull Function entity) { - if (entity instanceof CustomFunction) { - final AFunction result = new AFunction(); - result.setName(entity.getName()); - result.setContent(((CustomFunction) entity).getContent()); - result.setParameterNames(((CustomFunction) entity).getParameterNames()); - return result; - } else { - return null; - } - } - - @NotNull - @Override - protected MathEntityPersistenceContainer createPersistenceContainer() { - return new Functions(); - } -} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java new file mode 100644 index 00000000..fc17153c --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java @@ -0,0 +1,95 @@ +package org.solovyev.android.calculator.model; + +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.preference.PreferenceManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.solovyev.android.calculator.MathEntityDao; +import org.solovyev.android.calculator.MathEntityPersistenceContainer; +import org.solovyev.android.calculator.MathPersistenceEntity; +import org.solovyev.android.calculator.R; + +import java.io.StringWriter; + +/** + * User: serso + * Date: 10/7/12 + * Time: 6:46 PM + */ +public class AndroidMathEntityDao implements MathEntityDao { + + @Nullable + private final Integer preferenceStringId; + + @NotNull + private final Context context; + + @Nullable + private final Class> persistenceContainerClass; + + public AndroidMathEntityDao(@Nullable Integer preferenceStringId, + @NotNull Application application, + @Nullable Class> persistenceContainerClass) { + this.preferenceStringId = preferenceStringId; + this.context = application; + this.persistenceContainerClass = persistenceContainerClass; + } + + @Override + public void save(@NotNull MathEntityPersistenceContainer container) { + if (preferenceStringId != null) { + final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); + final SharedPreferences.Editor editor = settings.edit(); + + final StringWriter sw = new StringWriter(); + final Serializer serializer = new Persister(); + try { + serializer.write(container, sw); + } catch (Exception e) { + throw new RuntimeException(e); + } + + editor.putString(context.getString(preferenceStringId), sw.toString()); + + editor.commit(); + } + } + + @Nullable + @Override + public MathEntityPersistenceContainer load() { + if (persistenceContainerClass != null && preferenceStringId != null) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + if (preferences != null) { + final String value = preferences.getString(context.getString(preferenceStringId), null); + if (value != null) { + final Serializer serializer = new Persister(); + try { + return serializer.read(persistenceContainerClass, value); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + } + + return null; + } + + @Nullable + public String getDescription(@NotNull String descriptionId) { + final Resources resources = context.getResources(); + final int stringId = resources.getIdentifier(descriptionId, "string", R.class.getPackage().getName()); + try { + return resources.getString(stringId); + } catch (Resources.NotFoundException e) { + return null; + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidOperatorsMathRegistry.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidOperatorsMathRegistry.java index a9a0b75f..b0cdec68 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidOperatorsMathRegistry.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidOperatorsMathRegistry.java @@ -1,180 +1,165 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.model; - -import android.app.Application; -import jscl.math.function.ArcTrigonometric; -import jscl.math.function.Comparison; -import jscl.math.function.Trigonometric; -import jscl.math.operator.*; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.R; -import org.solovyev.common.JBuilder; -import org.solovyev.common.collections.CollectionsUtils; -import org.solovyev.common.math.MathRegistry; - -import java.util.*; - -/** - * User: serso - * Date: 11/17/11 - * Time: 11:29 PM - */ -public class AndroidOperatorsMathRegistry extends AbstractAndroidMathRegistry { - - @NotNull - private static final Map substitutes = new HashMap(); - static { - substitutes.put("Σ", "sum"); - substitutes.put("∏", "product"); - substitutes.put("∂", "derivative"); - substitutes.put("∫ab", "integral_ab"); - substitutes.put("∫", "integral"); - substitutes.put("Σ", "sum"); - } - - @NotNull - private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_"; - - protected AndroidOperatorsMathRegistry(@NotNull MathRegistry functionsRegistry, - @NotNull Application application) { - super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, application); - } - - @NotNull - @Override - protected Map getSubstitutes() { - return substitutes; - } - - @Override - public String getCategory(@NotNull Operator operator) { - for (Category category : Category.values()) { - if ( category.isInCategory(operator) ) { - return category.name(); - } - } - return null; - } - - @Override - public void load() { - // not supported yet - } - - @NotNull - @Override - protected JBuilder createBuilder(@NotNull MathPersistenceEntity entity) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @NotNull - @Override - protected Class> getPersistenceContainerClass() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - protected Integer getPreferenceStringId() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void save() { - // not supported yet - } - - @Override - protected MathPersistenceEntity transform(@NotNull Operator entity) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @NotNull - @Override - protected MathEntityPersistenceContainer createPersistenceContainer() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - public static enum Category { - - derivatives(R.string.derivatives, 100){ - @Override - boolean isInCategory(@NotNull Operator operator) { - return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral; - } - }, - - other(R.string.other, 200) { - @Override - boolean isInCategory(@NotNull Operator operator) { - return operator instanceof Sum || operator instanceof Product; - } - }, - - my(R.string.c_fun_category_my, 0) { - @Override - boolean isInCategory(@NotNull Operator operator) { - return !operator.isSystem(); - } - }, - - common(R.string.c_fun_category_common, 50) { - @Override - boolean isInCategory(@NotNull Operator operator) { - for (Category category : values()) { - if ( category != this ) { - if ( category.isInCategory(operator) ) { - return false; - } - } - } - - return true; - } - }; - - private final int captionId; - - private final int tabOrder; - - Category(int captionId, int tabOrder) { - this.captionId = captionId; - this.tabOrder = tabOrder; - } - - public int getCaptionId() { - return captionId; - } - - abstract boolean isInCategory(@NotNull Operator operator); - - @NotNull - public static List getCategoriesByTabOrder() { - final List result = CollectionsUtils.asList(Category.values()); - - Collections.sort(result, new Comparator() { - @Override - public int compare(Category category, Category category1) { - return category.tabOrder - category1.tabOrder; - } - }); - - // todo serso: current solution (as creating operators is not implemented yet) - result.remove(my); - - return result; - } - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.model; + +import jscl.math.operator.*; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.*; +import org.solovyev.common.JBuilder; +import org.solovyev.common.collections.CollectionsUtils; +import org.solovyev.common.math.MathRegistry; + +import java.util.*; + +/** + * User: serso + * Date: 11/17/11 + * Time: 11:29 PM + */ +public class AndroidOperatorsMathRegistry extends AbstractCalculatorMathRegistry { + + @NotNull + private static final Map substitutes = new HashMap(); + static { + substitutes.put("Σ", "sum"); + substitutes.put("∏", "product"); + substitutes.put("∂", "derivative"); + substitutes.put("∫ab", "integral_ab"); + substitutes.put("∫", "integral"); + substitutes.put("Σ", "sum"); + } + + @NotNull + private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_"; + + protected AndroidOperatorsMathRegistry(@NotNull MathRegistry functionsRegistry, + @NotNull MathEntityDao mathEntityDao) { + super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao); + } + + @NotNull + @Override + protected Map getSubstitutes() { + return substitutes; + } + + @Override + public String getCategory(@NotNull Operator operator) { + for (Category category : Category.values()) { + if ( category.isInCategory(operator) ) { + return category.name(); + } + } + return null; + } + + @Override + public void load() { + // not supported yet + } + + @NotNull + @Override + protected JBuilder createBuilder(@NotNull MathPersistenceEntity entity) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public void save() { + // not supported yet + } + + @Override + protected MathPersistenceEntity transform(@NotNull Operator entity) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @NotNull + @Override + protected MathEntityPersistenceContainer createPersistenceContainer() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + public static enum Category { + + derivatives(R.string.derivatives, 100){ + @Override + boolean isInCategory(@NotNull Operator operator) { + return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral; + } + }, + + other(R.string.other, 200) { + @Override + boolean isInCategory(@NotNull Operator operator) { + return operator instanceof Sum || operator instanceof Product; + } + }, + + my(R.string.c_fun_category_my, 0) { + @Override + boolean isInCategory(@NotNull Operator operator) { + return !operator.isSystem(); + } + }, + + common(R.string.c_fun_category_common, 50) { + @Override + boolean isInCategory(@NotNull Operator operator) { + for (Category category : values()) { + if ( category != this ) { + if ( category.isInCategory(operator) ) { + return false; + } + } + } + + return true; + } + }; + + private final int captionId; + + private final int tabOrder; + + Category(int captionId, int tabOrder) { + this.captionId = captionId; + this.tabOrder = tabOrder; + } + + public int getCaptionId() { + return captionId; + } + + abstract boolean isInCategory(@NotNull Operator operator); + + @NotNull + public static List getCategoriesByTabOrder() { + final List result = CollectionsUtils.asList(Category.values()); + + Collections.sort(result, new Comparator() { + @Override + public int compare(Category category, Category category1) { + return category.tabOrder - category1.tabOrder; + } + }); + + // todo serso: current solution (as creating operators is not implemented yet) + result.remove(my); + + return result; + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidPostfixFunctionsRegistry.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidPostfixFunctionsRegistry.java index b4acc5b7..2575fefd 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidPostfixFunctionsRegistry.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidPostfixFunctionsRegistry.java @@ -1,96 +1,88 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.model; - -import android.app.Application; -import jscl.math.operator.Operator; -import org.jetbrains.annotations.NotNull; -import org.solovyev.common.JBuilder; -import org.solovyev.common.math.MathRegistry; - -import java.util.HashMap; -import java.util.Map; - -/** - * User: serso - * Date: 11/19/11 - * Time: 1:48 PM - */ -public class AndroidPostfixFunctionsRegistry extends AbstractAndroidMathRegistry { - - @NotNull - private static final Map substitutes = new HashMap(); - static { - substitutes.put("%", "percent"); - substitutes.put("!", "factorial"); - substitutes.put("!!", "double_factorial"); - substitutes.put("°", "degree"); - } - - @NotNull - private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_"; - - protected AndroidPostfixFunctionsRegistry(@NotNull MathRegistry functionsRegistry, - @NotNull Application application) { - super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, application); - } - - - @NotNull - @Override - protected Map getSubstitutes() { - return substitutes; - } - - @Override - public String getCategory(@NotNull Operator operator) { - for (AndroidOperatorsMathRegistry.Category category : AndroidOperatorsMathRegistry.Category.values()) { - if ( category.isInCategory(operator) ) { - return category.name(); - } - } - return null; - } - - @Override - public void load() { - // not supported yet - } - - @NotNull - @Override - protected JBuilder createBuilder(@NotNull MathPersistenceEntity entity) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @NotNull - @Override - protected Class> getPersistenceContainerClass() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - protected Integer getPreferenceStringId() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void save() { - // not supported yet - } - - @Override - protected MathPersistenceEntity transform(@NotNull Operator entity) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @NotNull - @Override - protected MathEntityPersistenceContainer createPersistenceContainer() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.model; + +import jscl.math.operator.Operator; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.AbstractCalculatorMathRegistry; +import org.solovyev.android.calculator.MathEntityDao; +import org.solovyev.android.calculator.MathEntityPersistenceContainer; +import org.solovyev.android.calculator.MathPersistenceEntity; +import org.solovyev.common.JBuilder; +import org.solovyev.common.math.MathRegistry; + +import java.util.HashMap; +import java.util.Map; + +/** + * User: serso + * Date: 11/19/11 + * Time: 1:48 PM + */ +public class AndroidPostfixFunctionsRegistry extends AbstractCalculatorMathRegistry { + + @NotNull + private static final Map substitutes = new HashMap(); + static { + substitutes.put("%", "percent"); + substitutes.put("!", "factorial"); + substitutes.put("!!", "double_factorial"); + substitutes.put("°", "degree"); + } + + @NotNull + private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_"; + + protected AndroidPostfixFunctionsRegistry(@NotNull MathRegistry functionsRegistry, + @NotNull MathEntityDao mathEntityDao) { + super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao); + } + + + @NotNull + @Override + protected Map getSubstitutes() { + return substitutes; + } + + @Override + public String getCategory(@NotNull Operator operator) { + for (AndroidOperatorsMathRegistry.Category category : AndroidOperatorsMathRegistry.Category.values()) { + if ( category.isInCategory(operator) ) { + return category.name(); + } + } + return null; + } + + @Override + public void load() { + // not supported yet + } + + @NotNull + @Override + protected JBuilder createBuilder(@NotNull MathPersistenceEntity entity) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public void save() { + // not supported yet + } + + @Override + protected MathPersistenceEntity transform(@NotNull Operator entity) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @NotNull + @Override + protected MathEntityPersistenceContainer createPersistenceContainer() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java index 4783d2ec..e69c29da 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java @@ -6,10 +6,11 @@ package org.solovyev.android.calculator.model; -import android.app.Application; import jscl.math.function.IConstant; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.AbstractCalculatorMathRegistry; +import org.solovyev.android.calculator.MathEntityDao; +import org.solovyev.android.calculator.MathEntityPersistenceContainer; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathRegistry; @@ -21,7 +22,7 @@ import java.util.Map; * Date: 9/29/11 * Time: 4:57 PM */ -class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry { +class AndroidVarsRegistryImpl extends AbstractCalculatorMathRegistry { @NotNull private static final Map substitutes = new HashMap(); @@ -34,8 +35,8 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry mathRegistry, - @NotNull Application application) { - super(mathRegistry, "c_var_description_", application); + @NotNull MathEntityDao mathEntityDao) { + super(mathRegistry, "c_var_description_", mathEntityDao); } @NotNull @@ -66,24 +67,13 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry> getPersistenceContainerClass() { - return Vars.class; - } - - @NotNull + @NotNull @Override protected MathEntityPersistenceContainer createPersistenceContainer() { return new Vars(); } - @NotNull - protected Integer getPreferenceStringId() { - return R.string.p_calc_vars; - } - - private void tryToAddAuxVar(@NotNull String name) { + private void tryToAddAuxVar(@NotNull String name) { if ( !contains(name) ) { add(new Var.Builder(name, (String)null)); } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Var.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Var.java index c9335b0f..e3e7b396 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Var.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Var.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; import org.simpleframework.xml.Transient; +import org.solovyev.android.calculator.MathPersistenceEntity; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathEntity; import org.solovyev.common.text.StringUtils; diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Vars.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Vars.java index edd94ffb..f6ef4f43 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Vars.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/Vars.java @@ -1,8 +1,8 @@ package org.solovyev.android.calculator.model; -import jscl.math.function.IConstant; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; +import org.solovyev.android.calculator.MathEntityPersistenceContainer; import java.util.ArrayList; import java.util.List; diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/PlotUtils.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/PlotUtils.java index 807eaa9c..e120980d 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/PlotUtils.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/PlotUtils.java @@ -6,7 +6,6 @@ package org.solovyev.android.calculator.plot; -import android.util.Log; import jscl.math.Expression; import jscl.math.Generic; import jscl.math.JsclInteger; @@ -375,7 +374,7 @@ public final class PlotUtils { // double dydx1 = dy2 / dx1; if ( dy2 > MAX_Y_DIFF && dx2 < MAX_X_DIFF && isDifferentSign(point.getY2(), point.getY1()) && isDifferentSign(point.getDyDx1(), point.getDyDx2())) { - Log.d(CalculatorPlotActivity.class.getName(), "Singularity: " + point); + //Log.d(CalculatorPlotActivity.class.getName(), "Singularity: " + point); //Log.d(CalculatorPlotActivity.class.getName(), String.valueOf(prevX + Math.abs(x - prevX) / 2) + ", null"); series.add(point.getX1() + point.getAbsDx2() / 2, MathHelper.NULL_VALUE); point.clearHistory(); diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java index bb94cae3..33fde252 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java @@ -31,7 +31,7 @@ public class FromJsclNumericTextProcessorTest { public void testCreateResultForComplexNumber() throws Exception { final FromJsclNumericTextProcessor cm = new FromJsclNumericTextProcessor(); - final JsclMathEngine me = JsclMathEngine.instance; + final JsclMathEngine me = JsclMathEngine.getInstance(); final AngleUnit defaultAngleUnits = me.getAngleUnits(); Assert.assertEquals("1.22133+23 123i", cm.process(Expression.valueOf("1.22133232+23123*i").numeric())); diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java index 14a5a63b..57f2600a 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java @@ -36,7 +36,7 @@ public class NumeralBaseTest { public void testConversion() throws Exception { CSVReader reader = null; try { - final MathEngine me = JsclMathEngine.instance; + final MathEngine me = JsclMathEngine.getInstance(); reader = new CSVReader(new InputStreamReader(NumeralBaseTest.class.getResourceAsStream("/org/solovyev/android/calculator/model/nb_table.csv")), '\t'); diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index de861595..0d8a8b17 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -152,15 +152,15 @@ public class ToJsclTextProcessorTest { public void testNumeralBases() throws Exception { final TextProcessor processor = ToJsclTextProcessor.getInstance(); - final NumeralBase defaultNumeralBase = JsclMathEngine.instance.getNumeralBase(); + final NumeralBase defaultNumeralBase = JsclMathEngine.getInstance().getNumeralBase(); try{ - JsclMathEngine.instance.setNumeralBase(NumeralBase.bin); - Assert.assertEquals("101", JsclMathEngine.instance.evaluate("10+11")); + JsclMathEngine.getInstance().setNumeralBase(NumeralBase.bin); + Assert.assertEquals("101", JsclMathEngine.getInstance().evaluate("10+11")); - JsclMathEngine.instance.setNumeralBase(NumeralBase.hex); + JsclMathEngine.getInstance().setNumeralBase(NumeralBase.hex); Assert.assertEquals("56CE+CAD", processor.process("56CE+CAD").getExpression()); } finally { - JsclMathEngine.instance.setNumeralBase(defaultNumeralBase); + JsclMathEngine.getInstance().setNumeralBase(defaultNumeralBase); } } } diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/CalculatorppTestRunner.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/CalculatorppTestRunner.java new file mode 100644 index 00000000..c5155e17 --- /dev/null +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/CalculatorppTestRunner.java @@ -0,0 +1,19 @@ +package org.solovyev.android.calculator.plot; + +import com.xtremelabs.robolectric.RobolectricTestRunner; +import org.jetbrains.annotations.NotNull; +import org.junit.runners.model.InitializationError; + +import java.io.File; + +/** + * User: serso + * Date: 10/7/12 + * Time: 5:36 PM + */ +public class CalculatorppTestRunner extends RobolectricTestRunner { + + public CalculatorppTestRunner(@NotNull Class testClass) throws InitializationError { + super(testClass, new File("calculatorpp")); + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/PlotUtilsTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/PlotUtilsTest.java index 7314ce9b..4ddc77c8 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/PlotUtilsTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/PlotUtilsTest.java @@ -12,13 +12,13 @@ import junit.framework.Assert; import org.achartengine.model.XYSeries; import org.jetbrains.annotations.NotNull; import org.junit.Test; +import org.junit.runner.RunWith; /** * User: serso * Date: 12/5/11 * Time: 9:07 PM */ - public class PlotUtilsTest { @Test diff --git a/pom.xml b/pom.xml index d44d317a..471d7388 100644 --- a/pom.xml +++ b/pom.xml @@ -1,265 +1,275 @@ - - - - 4.0.0 - - org.solovyev.android - calculatorpp-parent - pom - 1.3.2 - Calculator++ - - - calculatorpp - calculatorpp-test - calculatorpp-core - - - - UTF-8 - - - - - - - org.solovyev - common-core - 1.0.0 - - - - org.solovyev - common-text - 1.0.1 - - - - org.solovyev.android - android-common-core - apklib - 1.0.0 - - - - org.solovyev.android - android-common-ads - apklib - 1.0.0 - - - - org.solovyev.android - android-common-view - apklib - 1.0.0 - - - - org.solovyev.android - android-common-preferences - apklib - 1.0.0 - - - - org.solovyev.android - android-common-menu - apklib - 1.0.0 - - - - org.solovyev.android - android-common-sherlock - 1.0.0 - apklib - - - - com.actionbarsherlock - library - 4.1.0 - apklib - - - - org.solovyev - jscl - 0.0.2 - - - xercesImpl - xerces - - - - - - org.solovyev.android - android-common-other - apklib - 1.0.0 - - - - junit - junit - 4.8.2 - - - - com.intellij - annotations - 7.0.3 - - - - com.google.android - android - 4.0.1.2 - provided - - - - com.google.android - support-v4 - r7 - - - - com.google.android - android-test - 2.3.1 - - - - com.google.guava - guava - 11.0.2 - - - - org.simpleframework - simple-xml - 2.6.1 - - - stax-api - stax - - - xpp3 - xpp3 - - - - - - - - - - - - - com.electriccloud - javac2-maven-plugin - 1.0.1 - - - @NotNull Instrumentation - - instrument - - - compile - - - - - - - - - - - org.apache.maven.plugins - maven-jarsigner-plugin - 1.2 - - - - com.jayway.maven.plugins.android.generation2 - android-maven-plugin - 3.1.1 - - - - ${project.basedir}/src/main/java - - - - 15 - - - - 23 - 10000 - - - - - true - - - true - - - - - - - com.pyx4me - proguard-maven-plugin - 2.0.4 - - - - org.codehaus.mojo - build-helper-maven-plugin - 1.5 - - - - - - - - - - - - standard - - - - - release - - - - performRelease - true - - - - - - - + + + + 4.0.0 + + org.solovyev.android + calculatorpp-parent + pom + 1.3.2 + Calculator++ + + + calculatorpp + calculatorpp-test + calculatorpp-core + + + + UTF-8 + + + + + + + org.solovyev + common-core + 1.0.0 + + + + org.solovyev + common-text + 1.0.1 + + + + org.solovyev.android + android-common-core + apklib + 1.0.0 + + + + org.solovyev.android + android-common-ads + apklib + 1.0.0 + + + + org.solovyev.android + android-common-view + apklib + 1.0.0 + + + + org.solovyev.android + android-common-preferences + apklib + 1.0.0 + + + + org.solovyev.android + android-common-menu + apklib + 1.0.0 + + + + org.solovyev.android + android-common-sherlock + 1.0.0 + apklib + + + + com.actionbarsherlock + library + 4.1.0 + apklib + + + + org.solovyev + jscl + 0.0.3 + + + xercesImpl + xerces + + + + + + org.solovyev.android + android-common-other + apklib + 1.0.0 + + + + junit + junit + 4.8.2 + + + + com.intellij + annotations + 7.0.3 + + + + com.google.android + android + 4.0.1.2 + provided + + + + com.google.android + support-v4 + r7 + + + + com.google.android + android-test + 2.3.1 + + + + com.google.guava + guava + 11.0.2 + + + + org.simpleframework + simple-xml + 2.6.1 + + + stax-api + stax + + + xpp3 + xpp3 + + + + + + + + + + + + + com.electriccloud + javac2-maven-plugin + 1.0.1 + + + @NotNull Instrumentation + + instrument + + + compile + + + + + + + + + + + org.apache.maven.plugins + maven-jarsigner-plugin + 1.2 + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + 3.1.1 + + + + ${project.basedir}/src/main/java + + + + 15 + + + + 23 + 10000 + + + + + true + + + true + + + + + + + com.pyx4me + proguard-maven-plugin + 2.0.4 + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.5 + + + + + + + + + + + + + + + standard + + + + + release + + + + performRelease + true + + + + + + + \ No newline at end of file