Engine interface removed

This commit is contained in:
serso 2016-01-19 22:48:35 +01:00
parent 712f94484f
commit 67a386f573
11 changed files with 55 additions and 214 deletions

View File

@ -28,6 +28,7 @@ import android.preference.PreferenceManager;
import android.util.Log;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.otto.Bus;
import jscl.MathEngine;
import org.acra.ACRA;
import org.acra.ACRAConfiguration;
import org.acra.sender.HttpSender;
@ -143,7 +144,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
private void warmUpEngine() {
try {
// warm-up engine
CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
MathEngine mathEngine = Locator.getInstance().getEngine().getEngine();
mathEngine.evaluate("1+1");
mathEngine.evaluate("1*1");
} catch (Throwable e) {

View File

@ -47,10 +47,7 @@ public interface CalculatorEngine {
@Nonnull
EntitiesRegistry<Operator> getPostfixFunctionsRegistry();
@Nonnull
CalculatorMathEngine getMathEngine();
@Deprecated
@Nonnull
MathEngine getMathEngine0();
MathEngine getEngine();
@Nonnull
String getMultiplicationSign();

View File

@ -26,11 +26,9 @@ import jscl.AngleUnit;
import jscl.JsclMathEngine;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.math.Generic;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
import jscl.math.operator.Operator;
import jscl.text.ParseException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -58,9 +56,6 @@ public class CalculatorEngineImpl implements CalculatorEngine {
@Nonnull
private final MathEngine engine;
@Nonnull
private final CalculatorMathEngine mathEngine;
@Nonnull
private final EntitiesRegistry<IConstant> varsRegistry;
@ -96,7 +91,6 @@ public class CalculatorEngineImpl implements CalculatorEngine {
@Nullable Object lock) {
this.engine = engine;
this.mathEngine = new JsclCalculatorMathEngine(engine);
this.engine.setRoundResult(true);
this.engine.setUseGroupingSeparator(true);
@ -141,14 +135,8 @@ public class CalculatorEngineImpl implements CalculatorEngine {
@Nonnull
@Override
public CalculatorMathEngine getMathEngine() {
return this.mathEngine;
}
@Nonnull
@Override
public MathEngine getMathEngine0() {
return this.engine;
public MathEngine getEngine() {
return engine;
}
/*
@ -271,58 +259,4 @@ public class CalculatorEngineImpl implements CalculatorEngine {
this.engine.setDecimalGroupSymbols(decimalGroupSymbols);
}
}
/*
**********************************************************************
*
* STATIC CLASSES
*
**********************************************************************
*/
private static final class JsclCalculatorMathEngine implements CalculatorMathEngine {
@Nonnull
private final MathEngine mathEngine;
private JsclCalculatorMathEngine(@Nonnull MathEngine mathEngine) {
this.mathEngine = mathEngine;
}
@Nonnull
@Override
public String evaluate(@Nonnull String expression) throws ParseException {
return this.mathEngine.evaluate(expression);
}
@Nonnull
@Override
public String simplify(@Nonnull String expression) throws ParseException {
return this.mathEngine.simplify(expression);
}
@Nonnull
@Override
public String elementary(@Nonnull String expression) throws ParseException {
return this.mathEngine.elementary(expression);
}
@Nonnull
@Override
public Generic evaluateGeneric(@Nonnull String expression) throws ParseException {
return this.mathEngine.evaluateGeneric(expression);
}
@Nonnull
@Override
public Generic simplifyGeneric(@Nonnull String expression) throws ParseException {
return this.mathEngine.simplifyGeneric(expression);
}
@Nonnull
@Override
public Generic elementaryGeneric(@Nonnull String expression) throws ParseException {
return this.mathEngine.elementaryGeneric(expression);
}
}
}

View File

@ -24,10 +24,17 @@ package org.solovyev.android.calculator;
import android.text.TextUtils;
import android.util.Log;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import jscl.AbstractJsclArithmeticException;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.NumeralBaseException;
import jscl.math.Generic;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.text.TextProcessor;
@ -40,24 +47,14 @@ import org.solovyev.common.text.Strings;
import org.solovyev.common.units.ConversionException;
import org.solovyev.common.units.Conversions;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.AbstractJsclArithmeticException;
import jscl.NumeralBase;
import jscl.NumeralBaseException;
import jscl.math.Generic;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException;
/**
* User: Solovyev_S
* Date: 20.09.12
@ -258,10 +255,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
try {
final CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
final MathEngine mathEngine = Locator.getInstance().getEngine().getEngine();
final MessageRegistry messageRegistry = new ListMessageRegistry();
Locator.getInstance().getEngine().getMathEngine0().setMessageRegistry(messageRegistry);
Locator.getInstance().getEngine().getEngine().setMessageRegistry(messageRegistry);
final Generic result = operation.evaluateGeneric(jsclExpression, mathEngine);

View File

@ -1,54 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator;
import javax.annotation.Nonnull;
import jscl.math.Generic;
import jscl.text.ParseException;
/**
* User: serso
* Date: 9/23/12
* Time: 6:05 PM
*/
public interface CalculatorMathEngine {
@Nonnull
String evaluate(@Nonnull String expression) throws ParseException;
@Nonnull
String simplify(@Nonnull String expression) throws ParseException;
@Nonnull
String elementary(@Nonnull String expression) throws ParseException;
@Nonnull
Generic evaluateGeneric(@Nonnull String expression) throws ParseException;
@Nonnull
Generic simplifyGeneric(@Nonnull String expression) throws ParseException;
@Nonnull
Generic elementaryGeneric(@Nonnull String expression) throws ParseException;
}

View File

@ -23,24 +23,17 @@
package org.solovyev.android.calculator;
import android.text.SpannableStringBuilder;
import org.solovyev.android.calculator.math.MathType;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.MathContext;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.math.numeric.Real;
import jscl.text.DoubleParser;
import jscl.text.JsclIntegerParser;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
import jscl.text.*;
import org.solovyev.android.calculator.math.MathType;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
@ -190,7 +183,7 @@ public class NumberBuilder extends BaseNumberBuilder {
}
// check if number still valid
toDouble(number, getNumeralBase(), engine.getMathEngine0());
toDouble(number, getNumeralBase(), engine.getEngine());
} catch (NumberFormatException e) {
// number is not valid => stop
@ -203,6 +196,6 @@ public class NumberBuilder extends BaseNumberBuilder {
nb = engine.getNumeralBase();
}
return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine0());
return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getEngine());
}
}

View File

@ -23,16 +23,15 @@
package org.solovyev.android.calculator.jscl;
import org.solovyev.android.calculator.CalculatorMathEngine;
import jscl.MathEngine;
import jscl.math.Generic;
import jscl.text.ParseException;
import org.solovyev.android.calculator.text.DummyTextProcessor;
import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor;
import org.solovyev.android.calculator.text.TextProcessor;
import javax.annotation.Nonnull;
import jscl.math.Generic;
import jscl.text.ParseException;
public enum JsclOperation {
simplify,
@ -58,7 +57,7 @@ public enum JsclOperation {
}
@Nonnull
public final String evaluate(@Nonnull String expression, @Nonnull CalculatorMathEngine engine) throws ParseException {
public final String evaluate(@Nonnull String expression, @Nonnull MathEngine engine) throws ParseException {
switch (this) {
case simplify:
return engine.simplify(expression);
@ -72,7 +71,7 @@ public enum JsclOperation {
}
@Nonnull
public final Generic evaluateGeneric(@Nonnull String expression, @Nonnull CalculatorMathEngine engine) throws ParseException {
public final Generic evaluateGeneric(@Nonnull String expression, @Nonnull MathEngine engine) throws ParseException {
switch (this) {
case simplify:
return engine.simplifyGeneric(expression);

View File

@ -24,9 +24,12 @@ package org.solovyev.android.calculator.math.edit;
import android.view.View;
import android.widget.EditText;
import org.solovyev.android.calculator.EntitiesRegistry;
import jscl.text.Identifier;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
import org.solovyev.android.calculator.CalculatorVarsRegistry;
import org.solovyev.android.calculator.EntitiesRegistry;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathType;
@ -38,16 +41,6 @@ import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.text.Identifier;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
/**
* User: serso
* Date: 12/22/11
* Time: 9:52 PM
*/
public class VarEditorSaver<T extends MathEntity> implements View.OnClickListener {
@Nonnull
@ -83,7 +76,7 @@ public class VarEditorSaver<T extends MathEntity> implements View.OnClickListene
if (!Strings.isEmpty(name)) {
try {
if (name == null) throw new AssertionError();
Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), Locator.getInstance().getEngine().getMathEngine0()), null);
Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), Locator.getInstance().getEngine().getEngine()), null);
result = true;
} catch (ParseException e) {
// not valid name;

View File

@ -133,14 +133,8 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen
@Override
@Nonnull
public CalculatorMathEngine getMathEngine() {
return calculatorEngine.getMathEngine();
}
@Nonnull
@Override
public MathEngine getMathEngine0() {
return calculatorEngine.getMathEngine0();
public MathEngine getEngine() {
return calculatorEngine.getEngine();
}
@Nonnull

View File

@ -27,7 +27,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.CalculatorApplication;
@ -35,16 +34,10 @@ import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.MathPersistenceEntity;
import java.io.StringWriter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.StringWriter;
/**
* User: serso
* Date: 10/7/12
* Time: 6:46 PM
*/
public class AndroidMathEntityDao<T extends MathPersistenceEntity> implements MathEntityDao<T> {
@Nonnull

View File

@ -22,28 +22,22 @@
package org.solovyev.android.calculator.model;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.solovyev.android.calculator.AbstractCalculatorTest;
import org.solovyev.android.calculator.CalculatorEvalException;
import org.solovyev.android.calculator.CalculatorParseException;
import org.solovyev.android.calculator.CalculatorTestUtils;
import org.solovyev.android.calculator.Locator;
import org.solovyev.common.Converter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import au.com.bytecode.opencsv.CSVReader;
import jscl.JsclMathEngine;
import jscl.MathEngine;
import jscl.math.Expression;
import jscl.text.ParseException;
import jscl.util.ExpressionGeneratorWithInput;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.solovyev.android.calculator.*;
import org.solovyev.common.Converter;
import javax.annotation.Nonnull;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
@ -64,11 +58,11 @@ public class NumeralBaseTest extends AbstractCalculatorTest {
final String bin = "0b:" + line[2].toUpperCase();
final String decExpression = converter.convert(dec);
final String decResult = Locator.getInstance().getEngine().getMathEngine().evaluate(decExpression);
final String decResult = Locator.getInstance().getEngine().getEngine().evaluate(decExpression);
final String hexExpression = converter.convert(hex);
final String hexResult = Locator.getInstance().getEngine().getMathEngine().evaluate(hexExpression);
final String hexResult = Locator.getInstance().getEngine().getEngine().evaluate(hexExpression);
final String binExpression = converter.convert(bin);
final String binResult = Locator.getInstance().getEngine().getMathEngine().evaluate(binExpression);
final String binResult = Locator.getInstance().getEngine().getEngine().evaluate(binExpression);
Assert.assertEquals("dec-hex: " + decExpression + " : " + hexExpression, decResult, hexResult);
Assert.assertEquals("dec-bin: " + decExpression + " : " + binExpression, decResult, binResult);