Application structure changed
This commit is contained in:
63
core/pom.xml
Normal file
63
core/pom.xml
Normal file
@@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-parent</artifactId>
|
||||
<version>1.5.3-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-core</artifactId>
|
||||
<version>1.5.3-SNAPSHOT</version>
|
||||
<name>Calculator++ Application Core</name>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev</groupId>
|
||||
<artifactId>common-text</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.opencsv</groupId>
|
||||
<artifactId>opencsv</artifactId>
|
||||
<version>2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>1.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.intellij</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev</groupId>
|
||||
<artifactId>jscl</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.simpleframework</groupId>
|
||||
<artifactId>simple-xml</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* 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.CustomFunctionCalculationException;
|
||||
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<T extends MathEntity, P extends MathPersistenceEntity> implements CalculatorMathRegistry<T> {
|
||||
|
||||
@NotNull
|
||||
private final MathRegistry<T> mathRegistry;
|
||||
|
||||
@NotNull
|
||||
private final String prefix;
|
||||
|
||||
@NotNull
|
||||
private final MathEntityDao<P> mathEntityDao;
|
||||
|
||||
protected AbstractCalculatorMathRegistry(@NotNull MathRegistry<T> mathRegistry,
|
||||
@NotNull String prefix,
|
||||
@NotNull MathEntityDao<P> mathEntityDao) {
|
||||
this.mathRegistry = mathRegistry;
|
||||
this.prefix = prefix;
|
||||
this.mathEntityDao = mathEntityDao;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@NotNull
|
||||
protected abstract Map<String, String> getSubstitutes();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDescription(@NotNull String mathEntityName) {
|
||||
final String stringName;
|
||||
|
||||
final Map<String, String> 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<P> persistenceContainer = mathEntityDao.load();
|
||||
|
||||
if (persistenceContainer != null) {
|
||||
for (P entity : persistenceContainer.getEntities()) {
|
||||
if (!contains(entity.getName())) {
|
||||
try {
|
||||
add(createBuilder(entity));
|
||||
} catch (CustomFunctionCalculationException e) {
|
||||
Locator.getInstance().getLogger().error(null, e.getMessage(), 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<? extends T> createBuilder(@NotNull P entity);
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized void save() {
|
||||
final MathEntityPersistenceContainer<P> 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<P> createPersistenceContainer();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<T> getEntities() {
|
||||
return mathRegistry.getEntities();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<T> getSystemEntities() {
|
||||
return mathRegistry.getSystemEntities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T add(@NotNull JBuilder<? extends T> JBuilder) {
|
||||
return mathRegistry.add(JBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(@NotNull T var) {
|
||||
mathRegistry.remove(var);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> 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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/15/11
|
||||
* Time: 9:01 PM
|
||||
*/
|
||||
public abstract class AbstractNumberBuilder {
|
||||
|
||||
@NotNull
|
||||
protected final CalculatorEngine engine;
|
||||
|
||||
@Nullable
|
||||
protected StringBuilder numberBuilder = null;
|
||||
|
||||
@Nullable
|
||||
protected NumeralBase nb;
|
||||
|
||||
protected AbstractNumberBuilder(@NotNull CalculatorEngine engine) {
|
||||
this.engine = engine;
|
||||
this.nb = engine.getNumeralBase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method determines if we can continue to process current number
|
||||
*
|
||||
* @param mathTypeResult current math type result
|
||||
* @return true if we can continue of processing of current number, if false - new number should be constructed
|
||||
*/
|
||||
protected boolean canContinue(@NotNull MathType.Result mathTypeResult) {
|
||||
boolean result = mathTypeResult.getMathType().getGroupType() == MathType.MathGroupType.number &&
|
||||
!spaceBefore(mathTypeResult) &&
|
||||
numeralBaseCheck(mathTypeResult) &&
|
||||
numeralBaseInTheStart(mathTypeResult.getMathType()) || isSignAfterE(mathTypeResult);
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean spaceBefore(@NotNull MathType.Result mathTypeResult) {
|
||||
return numberBuilder == null && StringUtils.isEmpty(mathTypeResult.getMatch().trim());
|
||||
}
|
||||
|
||||
private boolean numeralBaseInTheStart(@NotNull MathType mathType) {
|
||||
return mathType != MathType.numeral_base || numberBuilder == null;
|
||||
}
|
||||
|
||||
private boolean numeralBaseCheck(@NotNull MathType.Result mathType) {
|
||||
return mathType.getMathType() != MathType.digit || getNumeralBase().getAcceptableCharacters().contains(mathType.getMatch().charAt(0));
|
||||
}
|
||||
|
||||
private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) {
|
||||
if (!isHexMode()) {
|
||||
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
|
||||
final StringBuilder localNb = numberBuilder;
|
||||
if (localNb != null && localNb.length() > 0) {
|
||||
if (localNb.charAt(localNb.length() - 1) == MathType.POWER_10) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isHexMode() {
|
||||
return nb == NumeralBase.hex || (nb == null && engine.getNumeralBase() == NumeralBase.hex);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected NumeralBase getNumeralBase() {
|
||||
return nb == null ? engine.getNumeralBase() : nb;
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.history.HistoryControl;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:38
|
||||
*/
|
||||
public interface Calculator extends CalculatorEventContainer, HistoryControl<CalculatorHistoryState> {
|
||||
|
||||
void init();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
void evaluate();
|
||||
|
||||
void evaluate(@NotNull Long sequenceId);
|
||||
|
||||
void simplify();
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long sequenceId);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Object source);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
||||
|
||||
@NotNull
|
||||
PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException;
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 19.10.12
|
||||
* Time: 17:31
|
||||
*/
|
||||
public final class CalculatorButtonActions {
|
||||
|
||||
private CalculatorButtonActions() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 1:34 PM
|
||||
*/
|
||||
public interface CalculatorClipboard {
|
||||
|
||||
@Nullable
|
||||
String getText();
|
||||
|
||||
void setText(@NotNull String text);
|
||||
|
||||
void setText(@NotNull CharSequence text);
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:45
|
||||
*/
|
||||
public interface CalculatorConversionEventData extends CalculatorEventData {
|
||||
|
||||
// display state on the moment of conversion
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getDisplayState();
|
||||
|
||||
@NotNull
|
||||
NumeralBase getFromNumeralBase();
|
||||
|
||||
@NotNull
|
||||
NumeralBase getToNumeralBase();
|
||||
|
||||
@NotNull
|
||||
Generic getValue();
|
||||
}
|
@@ -0,0 +1,103 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:48
|
||||
*/
|
||||
public class CalculatorConversionEventDataImpl implements CalculatorConversionEventData {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
|
||||
@NotNull
|
||||
private NumeralBase fromNumeralBase;
|
||||
|
||||
@NotNull
|
||||
private NumeralBase toNumeralBase;
|
||||
|
||||
@NotNull
|
||||
private Generic value;
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState displayState;
|
||||
|
||||
private CalculatorConversionEventDataImpl() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorConversionEventData newInstance(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull Generic value,
|
||||
@NotNull NumeralBase from,
|
||||
@NotNull NumeralBase to,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
final CalculatorConversionEventDataImpl result = new CalculatorConversionEventDataImpl();
|
||||
|
||||
result.calculatorEventData = calculatorEventData;
|
||||
result.value = value;
|
||||
result.displayState = displayViewState;
|
||||
result.fromNumeralBase = from;
|
||||
result.toNumeralBase = to;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventData.getEventId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return calculatorEventData.getSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getDisplayState() {
|
||||
return this.displayState;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public NumeralBase getFromNumeralBase() {
|
||||
return fromNumeralBase;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public NumeralBase getToNumeralBase() {
|
||||
return toNumeralBase;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Generic getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/17/11
|
||||
* Time: 9:45 PM
|
||||
*/
|
||||
public interface CalculatorDisplay extends CalculatorEventListener {
|
||||
|
||||
void setView(@Nullable CalculatorDisplayView view);
|
||||
|
||||
@Nullable
|
||||
CalculatorDisplayView getView();
|
||||
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getViewState();
|
||||
|
||||
void setViewState(@NotNull CalculatorDisplayViewState viewState);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData getLastEventData();
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/21/12
|
||||
* Time: 9:49 PM
|
||||
*/
|
||||
public interface CalculatorDisplayChangeEventData extends Change<CalculatorDisplayViewState> {
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/21/12
|
||||
* Time: 9:50 PM
|
||||
*/
|
||||
public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayChangeEventData {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorDisplayViewState oldState;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorDisplayViewState newState;
|
||||
|
||||
public CalculatorDisplayChangeEventDataImpl(@NotNull CalculatorDisplayViewState oldState, @NotNull CalculatorDisplayViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
@@ -0,0 +1,169 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 8:24 PM
|
||||
*/
|
||||
public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEvent;
|
||||
|
||||
@Nullable
|
||||
private CalculatorDisplayView view;
|
||||
|
||||
@NotNull
|
||||
private final Object viewLock = new Object();
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState viewState = CalculatorDisplayViewStateImpl.newDefaultInstance();
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
public CalculatorDisplayImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setView(@Nullable CalculatorDisplayView view) {
|
||||
synchronized (viewLock) {
|
||||
this.view = view;
|
||||
|
||||
if (view != null) {
|
||||
this.view.setState(viewState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public CalculatorDisplayView getView() {
|
||||
return this.view;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getViewState() {
|
||||
return this.viewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewState(@NotNull CalculatorDisplayViewState newViewState) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
|
||||
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
|
||||
private void setViewStateForSequence(@NotNull CalculatorDisplayViewState newViewState, @NotNull Long sequenceId) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
|
||||
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState), sequenceId);
|
||||
}
|
||||
}
|
||||
|
||||
// must be synchronized with viewLock
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState setViewState0(@NotNull CalculatorDisplayViewState newViewState) {
|
||||
final CalculatorDisplayViewState oldViewState = this.viewState;
|
||||
|
||||
this.viewState = newViewState;
|
||||
if (this.view != null) {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
return oldViewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData getLastEventData() {
|
||||
return lastEvent.getLastEventData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(calculation_result, calculation_failed, calculation_cancelled, conversion_result, conversion_failed)) {
|
||||
|
||||
final CalculatorEventHolder.Result result = lastEvent.apply(calculatorEventData);
|
||||
|
||||
if (result.isNewAfter()) {
|
||||
switch (calculatorEventType) {
|
||||
case conversion_failed:
|
||||
processConversationFailed((CalculatorConversionEventData) calculatorEventData, (ConversionFailure) data);
|
||||
break;
|
||||
case conversion_result:
|
||||
processConversationResult((CalculatorConversionEventData)calculatorEventData, (String)data);
|
||||
break;
|
||||
case calculation_result:
|
||||
processCalculationResult((CalculatorEvaluationEventData) calculatorEventData, (CalculatorOutput) data);
|
||||
break;
|
||||
case calculation_cancelled:
|
||||
processCalculationCancelled((CalculatorEvaluationEventData)calculatorEventData);
|
||||
break;
|
||||
case calculation_failed:
|
||||
processCalculationFailed((CalculatorEvaluationEventData)calculatorEventData, (CalculatorFailure) data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processConversationFailed(@NotNull CalculatorConversionEventData calculatorEventData,
|
||||
@NotNull ConversionFailure data) {
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getDisplayState().getOperation(), CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error)), calculatorEventData.getSequenceId());
|
||||
|
||||
}
|
||||
|
||||
private void processCalculationFailed(@NotNull CalculatorEvaluationEventData calculatorEventData, @NotNull CalculatorFailure data) {
|
||||
|
||||
final CalculatorEvalException calculatorEvalException = data.getCalculationEvalException();
|
||||
|
||||
final String errorMessage;
|
||||
if (calculatorEvalException != null) {
|
||||
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
} else {
|
||||
final CalculatorParseException calculationParseException = data.getCalculationParseException();
|
||||
if (calculationParseException != null) {
|
||||
errorMessage = calculationParseException.getLocalizedMessage();
|
||||
} else {
|
||||
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
}
|
||||
}
|
||||
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
|
||||
}
|
||||
|
||||
private void processCalculationCancelled(@NotNull CalculatorEvaluationEventData calculatorEventData) {
|
||||
final String errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
|
||||
}
|
||||
|
||||
private void processCalculationResult(@NotNull CalculatorEvaluationEventData calculatorEventData, @NotNull CalculatorOutput data) {
|
||||
final String stringResult = data.getStringResult();
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId());
|
||||
}
|
||||
|
||||
private void processConversationResult(@NotNull CalculatorConversionEventData calculatorEventData, @NotNull String result) {
|
||||
// add prefix
|
||||
if (calculatorEventData.getFromNumeralBase() != calculatorEventData.getToNumeralBase()) {
|
||||
result = calculatorEventData.getToNumeralBase().getJsclPrefix() + result;
|
||||
}
|
||||
|
||||
final CalculatorDisplayViewState displayState = calculatorEventData.getDisplayState();
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId());
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 8:25 PM
|
||||
*/
|
||||
public interface CalculatorDisplayView {
|
||||
|
||||
void setState(@NotNull CalculatorDisplayViewState state);
|
||||
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getState();
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 9:50 PM
|
||||
*/
|
||||
public interface CalculatorDisplayViewState extends Serializable {
|
||||
|
||||
@NotNull
|
||||
String getText();
|
||||
|
||||
int getSelection();
|
||||
|
||||
@Nullable
|
||||
Generic getResult();
|
||||
|
||||
boolean isValid();
|
||||
|
||||
@Nullable
|
||||
String getErrorMessage();
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
||||
@Nullable
|
||||
String getStringResult();
|
||||
}
|
@@ -0,0 +1,128 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 9:50 PM
|
||||
*/
|
||||
public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewState {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private JsclOperation operation = JsclOperation.numeric;
|
||||
|
||||
@Nullable
|
||||
private transient Generic result;
|
||||
|
||||
@Nullable
|
||||
private String stringResult = "";
|
||||
|
||||
private boolean valid = true;
|
||||
|
||||
@Nullable
|
||||
private String errorMessage;
|
||||
|
||||
private int selection = 0;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private CalculatorDisplayViewStateImpl() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newDefaultInstance() {
|
||||
return new CalculatorDisplayViewStateImpl();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newErrorState(@NotNull JsclOperation operation,
|
||||
@NotNull String errorMessage) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
calculatorDisplayState.valid = false;
|
||||
calculatorDisplayState.errorMessage = errorMessage;
|
||||
calculatorDisplayState.operation = operation;
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newValidState(@NotNull JsclOperation operation,
|
||||
@Nullable Generic result,
|
||||
@NotNull String stringResult,
|
||||
int selection) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
calculatorDisplayState.valid = true;
|
||||
calculatorDisplayState.result = result;
|
||||
calculatorDisplayState.stringResult = stringResult;
|
||||
calculatorDisplayState.operation = operation;
|
||||
calculatorDisplayState.selection = selection;
|
||||
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return StringUtils.getNotEmpty(isValid() ? stringResult : errorMessage, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return selection;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Generic getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.gui.CursorControl;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 11:47
|
||||
*/
|
||||
public interface CalculatorEditor extends CalculatorEventListener {
|
||||
|
||||
void setView(@Nullable CalculatorEditorView view);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState getViewState();
|
||||
|
||||
// updates state of view (view.setState())
|
||||
void updateViewState();
|
||||
|
||||
void setViewState(@NotNull CalculatorEditorViewState viewState);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CURSOR CONTROL
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/**
|
||||
* Method sets the cursor to the beginning
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnStart();
|
||||
|
||||
/**
|
||||
* Method sets the cursor to the end
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnEnd();
|
||||
|
||||
/**
|
||||
* Method moves cursor to the left of current position
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorLeft();
|
||||
|
||||
/**
|
||||
* Method moves cursor to the right of current position
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorRight();
|
||||
|
||||
@NotNull
|
||||
CursorControl asCursorControl();
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR OPERATIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEditorViewState erase();
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState clear();
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState setText(@NotNull String text);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState setText(@NotNull String text, int selection);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState insert(@NotNull String text);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState insert(@NotNull String text, int selectionOffset);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState moveSelection(int offset);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState setSelection(int selection);
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 13:46
|
||||
*/
|
||||
public interface CalculatorEditorChangeEventData extends Change<CalculatorEditorViewState>{
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 13:46
|
||||
*/
|
||||
public class CalculatorEditorChangeEventDataImpl implements CalculatorEditorChangeEventData {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState oldState;
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState newState;
|
||||
|
||||
public CalculatorEditorChangeEventDataImpl(@NotNull CalculatorEditorViewState oldState,
|
||||
@NotNull CalculatorEditorViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
@@ -0,0 +1,311 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.history.EditorHistoryState;
|
||||
import org.solovyev.common.gui.CursorControl;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 11:53
|
||||
*/
|
||||
public class CalculatorEditorImpl implements CalculatorEditor {
|
||||
|
||||
@Nullable
|
||||
private CalculatorEditorView view;
|
||||
|
||||
@NotNull
|
||||
private final Object viewLock = new Object();
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance();
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEventHolder;
|
||||
|
||||
@NotNull
|
||||
private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this);
|
||||
|
||||
public CalculatorEditorImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setView(@Nullable CalculatorEditorView view) {
|
||||
synchronized (viewLock) {
|
||||
this.view = view;
|
||||
|
||||
if ( view != null ) {
|
||||
view.setState(lastViewState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getViewState() {
|
||||
return lastViewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateViewState() {
|
||||
setViewState(this.lastViewState, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewState(@NotNull CalculatorEditorViewState newViewState) {
|
||||
setViewState(newViewState, true);
|
||||
}
|
||||
|
||||
private void setViewState(@NotNull CalculatorEditorViewState newViewState, boolean majorChanges) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState oldViewState = this.lastViewState;
|
||||
|
||||
this.lastViewState = newViewState;
|
||||
if (this.view != null) {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
|
||||
if (majorChanges) {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
} else {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed_light, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
final CalculatorEventHolder.Result result = lastEventHolder.apply(calculatorEventData);
|
||||
|
||||
if (result.isNewAfter()) {
|
||||
switch (calculatorEventType) {
|
||||
case use_history_state:
|
||||
final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState)data;
|
||||
final EditorHistoryState editorState = calculatorHistoryState.getEditorState();
|
||||
this.setText(StringUtils.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* SELECTION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState newSelectionViewState(int newSelection) {
|
||||
if (this.lastViewState.getSelection() != newSelection) {
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection);
|
||||
setViewState(result, false);
|
||||
return result;
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnStart() {
|
||||
synchronized (viewLock) {
|
||||
return newSelectionViewState(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnEnd() {
|
||||
synchronized (viewLock) {
|
||||
return newSelectionViewState(this.lastViewState.getText().length());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorLeft() {
|
||||
synchronized (viewLock) {
|
||||
if (this.lastViewState.getSelection() > 0) {
|
||||
return newSelectionViewState(this.lastViewState.getSelection() - 1);
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorRight() {
|
||||
synchronized (viewLock) {
|
||||
if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) {
|
||||
return newSelectionViewState(this.lastViewState.getSelection() + 1);
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CursorControl asCursorControl() {
|
||||
return cursorControlAdapter;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR ACTIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState erase() {
|
||||
synchronized (viewLock) {
|
||||
int selection = this.lastViewState.getSelection();
|
||||
final String text = this.lastViewState.getText();
|
||||
if (selection > 0 && text.length() > 0 && selection <= text.length()) {
|
||||
final StringBuilder newText = new StringBuilder(text.length() - 1);
|
||||
newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length()));
|
||||
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1);
|
||||
setViewState(result);
|
||||
return result;
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState clear() {
|
||||
synchronized (viewLock) {
|
||||
return setText("");
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setText(@NotNull String text) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length());
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setText(@NotNull String text, int selection) {
|
||||
synchronized (viewLock) {
|
||||
selection = correctSelection(selection, text);
|
||||
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection);
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState insert(@NotNull String text) {
|
||||
synchronized (viewLock) {
|
||||
return insert(text, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState insert(@NotNull String text, int selectionOffset) {
|
||||
synchronized (viewLock) {
|
||||
final int selection = this.lastViewState.getSelection();
|
||||
final String oldText = this.lastViewState.getText();
|
||||
|
||||
int newTextLength = text.length() + oldText.length();
|
||||
final StringBuilder newText = new StringBuilder(newTextLength);
|
||||
|
||||
newText.append(oldText.substring(0, selection));
|
||||
newText.append(text);
|
||||
newText.append(oldText.substring(selection));
|
||||
|
||||
int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength);
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection);
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState moveSelection(int offset) {
|
||||
synchronized (viewLock) {
|
||||
int selection = this.lastViewState.getSelection() + offset;
|
||||
|
||||
return setSelection(selection);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setSelection(int selection) {
|
||||
synchronized (viewLock) {
|
||||
selection = correctSelection(selection, this.lastViewState.getText());
|
||||
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection);
|
||||
setViewState(result, false);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private int correctSelection(int selection, @NotNull String text) {
|
||||
return correctSelection(selection, text.length());
|
||||
}
|
||||
|
||||
private int correctSelection(int selection, int textLength) {
|
||||
int result = Math.max(selection, 0);
|
||||
result = Math.min(result, textLength);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static final class CursorControlAdapter implements CursorControl {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEditor calculatorEditor;
|
||||
|
||||
private CursorControlAdapter(@NotNull CalculatorEditor calculatorEditor) {
|
||||
this.calculatorEditor = calculatorEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCursorOnStart() {
|
||||
this.calculatorEditor.setCursorOnStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCursorOnEnd() {
|
||||
this.calculatorEditor.setCursorOnEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorLeft() {
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorRight() {
|
||||
this.calculatorEditor.moveCursorRight();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 11:48
|
||||
*/
|
||||
public interface CalculatorEditorView {
|
||||
|
||||
void setState(@NotNull CalculatorEditorViewState viewState);
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 11:48
|
||||
*/
|
||||
public interface CalculatorEditorViewState extends Serializable {
|
||||
|
||||
@NotNull
|
||||
String getText();
|
||||
|
||||
int getSelection();
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 12:02
|
||||
*/
|
||||
public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState {
|
||||
|
||||
@NotNull
|
||||
private String text = "";
|
||||
|
||||
private int selection = 0;
|
||||
|
||||
private CalculatorEditorViewStateImpl() {
|
||||
}
|
||||
|
||||
public CalculatorEditorViewStateImpl(@NotNull CalculatorEditorViewState viewState) {
|
||||
this.text = viewState.getText();
|
||||
this.selection = viewState.getSelection();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return this.selection;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newDefaultInstance() {
|
||||
return new CalculatorEditorViewStateImpl();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newSelection(@NotNull CalculatorEditorViewState viewState, int newSelection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl(viewState);
|
||||
|
||||
result.selection = newSelection;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newInstance(@NotNull String text, int selection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl();
|
||||
result.text = text;
|
||||
result.selection = selection;
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.operator.Operator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.text.DecimalFormatSymbols;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 12:43
|
||||
*/
|
||||
public interface CalculatorEngine {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* INIT
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
void init();
|
||||
|
||||
void reset();
|
||||
|
||||
void softReset();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<IConstant> getVarsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Function> getFunctionsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Operator> getOperatorsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathEngine getMathEngine();
|
||||
|
||||
@Deprecated
|
||||
@NotNull
|
||||
MathEngine getMathEngine0();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
String getMultiplicationSign();
|
||||
|
||||
void setUseGroupingSeparator(boolean useGroupingSeparator);
|
||||
|
||||
void setGroupingSeparator(char groupingSeparator);
|
||||
|
||||
void setPrecision(@NotNull Integer precision);
|
||||
|
||||
void setRoundResult(@NotNull Boolean round);
|
||||
|
||||
@NotNull
|
||||
AngleUnit getAngleUnits();
|
||||
|
||||
void setAngleUnits(@NotNull AngleUnit angleUnits);
|
||||
|
||||
@NotNull
|
||||
NumeralBase getNumeralBase();
|
||||
|
||||
void setNumeralBase(@NotNull NumeralBase numeralBase);
|
||||
|
||||
void setMultiplicationSign(@NotNull String multiplicationSign);
|
||||
|
||||
void setScienceNotation(@NotNull Boolean scienceNotation);
|
||||
|
||||
void setTimeout(@NotNull Integer timeout);
|
||||
|
||||
void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols);
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/24/11
|
||||
* Time: 9:55 PM
|
||||
*/
|
||||
public interface CalculatorEngineControl {
|
||||
|
||||
void evaluate();
|
||||
|
||||
void simplify();
|
||||
}
|
@@ -0,0 +1,320 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.text.DecimalFormatSymbols;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/23/12
|
||||
* Time: 5:34 PM
|
||||
*/
|
||||
public class CalculatorEngineImpl implements CalculatorEngine {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final String MULTIPLICATION_SIGN_DEFAULT = "×";
|
||||
|
||||
private static final String MAX_CALCULATION_TIME_DEFAULT = "5";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE/REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
private final MathEngine engine;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathEngine mathEngine;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<IConstant> varsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Function> functionsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Operator> operatorsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Operator> postfixFunctionsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final Object lock;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
private int timeout = Integer.valueOf(MAX_CALCULATION_TIME_DEFAULT);
|
||||
|
||||
@NotNull
|
||||
private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT;
|
||||
|
||||
public CalculatorEngineImpl(@NotNull JsclMathEngine engine,
|
||||
@NotNull CalculatorMathRegistry<IConstant> varsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Function> functionsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Operator> operatorsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Operator> postfixFunctionsRegistry,
|
||||
@Nullable Object lock) {
|
||||
|
||||
this.engine = engine;
|
||||
this.mathEngine = new JsclCalculatorMathEngine(engine);
|
||||
|
||||
this.engine.setRoundResult(true);
|
||||
this.engine.setUseGroupingSeparator(true);
|
||||
|
||||
this.varsRegistry = varsRegistry;
|
||||
this.functionsRegistry = functionsRegistry;
|
||||
this.operatorsRegistry = operatorsRegistry;
|
||||
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
|
||||
this.lock = lock == null ? new Object() : lock;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<IConstant> getVarsRegistry() {
|
||||
return this.varsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Function> getFunctionsRegistry() {
|
||||
return this.functionsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Operator> getOperatorsRegistry() {
|
||||
return this.operatorsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry() {
|
||||
return this.postfixFunctionsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathEngine getMathEngine() {
|
||||
return this.mathEngine;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEngine getMathEngine0() {
|
||||
return this.engine;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* INIT
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
synchronized (lock) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
synchronized (lock) {
|
||||
varsRegistry.load();
|
||||
functionsRegistry.load();
|
||||
operatorsRegistry.load();
|
||||
postfixFunctionsRegistry.load();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void softReset() {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMultiplicationSign() {
|
||||
return this.multiplicationSign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUseGroupingSeparator(boolean useGroupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.engine.setUseGroupingSeparator(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGroupingSeparator(char groupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.engine.setGroupingSeparator(groupingSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrecision(@NotNull Integer precision) {
|
||||
synchronized (lock) {
|
||||
this.engine.setPrecision(precision);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRoundResult(@NotNull Boolean round) {
|
||||
synchronized (lock) {
|
||||
this.engine.setRoundResult(round);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public AngleUnit getAngleUnits() {
|
||||
synchronized (lock) {
|
||||
return this.engine.getAngleUnits();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAngleUnits(@NotNull AngleUnit angleUnits) {
|
||||
synchronized (lock) {
|
||||
this.engine.setAngleUnits(angleUnits);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public NumeralBase getNumeralBase() {
|
||||
synchronized (lock) {
|
||||
return this.engine.getNumeralBase();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
synchronized (lock) {
|
||||
this.engine.setNumeralBase(numeralBase);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMultiplicationSign(@NotNull String multiplicationSign) {
|
||||
this.multiplicationSign = multiplicationSign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScienceNotation(@NotNull Boolean scienceNotation) {
|
||||
synchronized (lock) {
|
||||
this.engine.setScienceNotation(scienceNotation);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeout(@NotNull Integer timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) {
|
||||
synchronized (lock) {
|
||||
this.engine.setDecimalGroupSymbols(decimalGroupSymbols);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC CLASSES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final class JsclCalculatorMathEngine implements CalculatorMathEngine {
|
||||
|
||||
@NotNull
|
||||
private final MathEngine mathEngine;
|
||||
|
||||
private JsclCalculatorMathEngine(@NotNull MathEngine mathEngine) {
|
||||
this.mathEngine = mathEngine;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String evaluate(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.evaluate(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String simplify(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.simplify(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String elementary(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.elementary(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic evaluateGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.evaluateGeneric(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic simplifyGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.simplifyGeneric(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic elementaryGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.elementaryGeneric(expression);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.solovyev.common.exceptions.SersoException;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/8/11
|
||||
* Time: 1:27 AM
|
||||
*/
|
||||
public class CalculatorEvalException extends SersoException implements Message {
|
||||
|
||||
@NotNull
|
||||
private final Message message;
|
||||
|
||||
@NotNull
|
||||
private final String expression;
|
||||
|
||||
public CalculatorEvalException(@NotNull Message message, @NotNull Throwable cause, String expression) {
|
||||
super(cause);
|
||||
this.message = message;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMessageCode() {
|
||||
return this.message.getMessageCode();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Object> getParameters() {
|
||||
return this.message.getParameters();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return this.message.getMessageType();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getLocalizedMessage() {
|
||||
return this.message.getLocalizedMessage(Locale.getDefault());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getLocalizedMessage(@NotNull Locale locale) {
|
||||
return this.message.getLocalizedMessage(locale);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 10:00 PM
|
||||
*/
|
||||
public interface CalculatorEvaluationEventData extends CalculatorEventData {
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
||||
@NotNull
|
||||
String getExpression();
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 10:01 PM
|
||||
*/
|
||||
public class CalculatorEvaluationEventDataImpl implements CalculatorEvaluationEventData {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventData calculatorEventData;
|
||||
|
||||
@NotNull
|
||||
private final JsclOperation operation;
|
||||
|
||||
@NotNull
|
||||
private final String expression;
|
||||
|
||||
public CalculatorEvaluationEventDataImpl(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.operation = operation;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return this.expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventData.getEventId();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return calculatorEventData.getSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:39
|
||||
*/
|
||||
public interface CalculatorEventContainer {
|
||||
|
||||
void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener);
|
||||
|
||||
void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener);
|
||||
|
||||
void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents);
|
||||
|
||||
public static class CalculatorEvent {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventType calculatorEventType;
|
||||
|
||||
@Nullable
|
||||
private Object data;
|
||||
|
||||
public CalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.calculatorEventType = calculatorEventType;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEventData getCalculatorEventData() {
|
||||
return calculatorEventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEventType getCalculatorEventType() {
|
||||
return calculatorEventType;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 18:18
|
||||
*/
|
||||
public interface CalculatorEventData {
|
||||
|
||||
// the higher id => the later event
|
||||
long getEventId();
|
||||
|
||||
// the higher id => the later event
|
||||
@NotNull
|
||||
Long getSequenceId();
|
||||
|
||||
@Nullable
|
||||
Object getSource();
|
||||
|
||||
boolean isAfter(@NotNull CalculatorEventData that);
|
||||
|
||||
boolean isSameSequence(@NotNull CalculatorEventData that);
|
||||
|
||||
boolean isAfterSequence(@NotNull CalculatorEventData that);
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 18:18
|
||||
*/
|
||||
class CalculatorEventDataImpl implements CalculatorEventData {
|
||||
|
||||
private static final long NO_SEQUENCE = -1L;
|
||||
|
||||
private final long eventId;
|
||||
|
||||
@NotNull
|
||||
private Long sequenceId = NO_SEQUENCE;
|
||||
|
||||
private final Object source;
|
||||
|
||||
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId, @Nullable Object source) {
|
||||
this.eventId = id;
|
||||
this.sequenceId = sequenceId;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId, @NotNull Object source) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return this.eventId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return this.sequenceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return this.eventId > that.getEventId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId.equals(that.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId > that.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CalculatorEventDataImpl)) return false;
|
||||
|
||||
CalculatorEventDataImpl that = (CalculatorEventDataImpl) o;
|
||||
|
||||
if (eventId != that.eventId) return false;
|
||||
if (!sequenceId.equals(that.sequenceId))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (int) (eventId ^ (eventId >>> 32));
|
||||
result = 31 * result + (sequenceId.hashCode());
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/9/12
|
||||
* Time: 9:59 PM
|
||||
*/
|
||||
public class CalculatorEventHolder {
|
||||
|
||||
@NotNull
|
||||
private volatile CalculatorEventData lastEventData;
|
||||
|
||||
public CalculatorEventHolder(@NotNull CalculatorEventData lastEventData) {
|
||||
this.lastEventData = lastEventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public synchronized CalculatorEventData getLastEventData() {
|
||||
return lastEventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public synchronized Result apply(@NotNull CalculatorEventData newEventData) {
|
||||
final Result result = new Result(lastEventData, newEventData);
|
||||
|
||||
if (result.isNewAfter()) {
|
||||
this.lastEventData = newEventData;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventData lastEventData;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventData newEventData;
|
||||
|
||||
@Nullable
|
||||
private Boolean after = null;
|
||||
|
||||
@Nullable
|
||||
private Boolean sameSequence = null;
|
||||
|
||||
public Result(@NotNull CalculatorEventData lastEventData,
|
||||
@NotNull CalculatorEventData newEventData) {
|
||||
this.lastEventData = lastEventData;
|
||||
this.newEventData = newEventData;
|
||||
}
|
||||
|
||||
public boolean isNewAfter() {
|
||||
if (after == null) {
|
||||
after = newEventData.isAfter(lastEventData);
|
||||
}
|
||||
return after;
|
||||
}
|
||||
|
||||
public boolean isSameSequence() {
|
||||
if (sameSequence == null) {
|
||||
sameSequence = newEventData.isSameSequence(lastEventData);
|
||||
}
|
||||
return sameSequence;
|
||||
}
|
||||
|
||||
public boolean isNewAfterSequence() {
|
||||
return newEventData.isAfterSequence(lastEventData);
|
||||
}
|
||||
|
||||
public boolean isNewSameOrAfterSequence() {
|
||||
return isSameSequence() || isNewAfterSequence();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:39
|
||||
*/
|
||||
public interface CalculatorEventListener extends EventListener {
|
||||
|
||||
void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
}
|
@@ -0,0 +1,168 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:40
|
||||
*/
|
||||
public enum CalculatorEventType {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
* org.solovyev.android.calculator.CalculatorEvaluationEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
// @NotNull CalculatorEditorViewState
|
||||
manual_calculation_requested,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorOutput
|
||||
calculation_result,
|
||||
|
||||
calculation_cancelled,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorFailure
|
||||
calculation_failed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
* CalculatorConversionEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
conversion_started,
|
||||
|
||||
// @NotNull String conversion result
|
||||
conversion_result,
|
||||
|
||||
// @NotNull ConversionFailure
|
||||
conversion_failed,
|
||||
|
||||
conversion_finished,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData
|
||||
editor_state_changed,
|
||||
editor_state_changed_light,
|
||||
|
||||
// @NotNull CalculatorDisplayChangeEventData
|
||||
display_state_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
engine_preferences_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull CalculatorHistoryState
|
||||
history_state_added,
|
||||
|
||||
// @NotNull CalculatorHistoryState
|
||||
use_history_state,
|
||||
|
||||
clear_history_requested,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* MATH ENTITIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull IConstant
|
||||
use_constant,
|
||||
|
||||
// @NotNull Function
|
||||
use_function,
|
||||
|
||||
// @NotNull Operator
|
||||
use_operator,
|
||||
|
||||
// @NotNull IConstant
|
||||
constant_added,
|
||||
|
||||
// @NotNull Change<IConstant>
|
||||
constant_changed,
|
||||
|
||||
// @NotNull IConstant
|
||||
constant_removed,
|
||||
|
||||
|
||||
// @NotNull Function
|
||||
function_removed,
|
||||
|
||||
// @NotNull Function
|
||||
function_added,
|
||||
|
||||
// @NotNull Change<IFunction>
|
||||
function_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* OTHER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// List<Message>
|
||||
calculation_messages,
|
||||
|
||||
show_history,
|
||||
show_history_detached,
|
||||
|
||||
show_functions,
|
||||
show_functions_detached,
|
||||
|
||||
show_vars,
|
||||
show_vars_detached,
|
||||
|
||||
open_app,
|
||||
|
||||
show_operators,
|
||||
show_operators_detached,
|
||||
|
||||
show_settings,
|
||||
show_settings_detached,
|
||||
|
||||
show_like_dialog;
|
||||
|
||||
public boolean isOfType(@NotNull CalculatorEventType... types) {
|
||||
for (CalculatorEventType type : types) {
|
||||
if ( this == type ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:33 PM
|
||||
*/
|
||||
public interface CalculatorFailure {
|
||||
|
||||
@NotNull
|
||||
Exception getException();
|
||||
|
||||
@Nullable
|
||||
CalculatorParseException getCalculationParseException();
|
||||
|
||||
@Nullable
|
||||
CalculatorEvalException getCalculationEvalException();
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:34 PM
|
||||
*/
|
||||
public class CalculatorFailureImpl implements CalculatorFailure {
|
||||
|
||||
@NotNull
|
||||
private Exception exception;
|
||||
|
||||
public CalculatorFailureImpl(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Exception getException() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorParseException getCalculationParseException() {
|
||||
return exception instanceof CalculatorParseException ? (CalculatorParseException)exception : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorEvalException getCalculationEvalException() {
|
||||
return exception instanceof CalculatorEvalException ? (CalculatorEvalException)exception : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorFailureImpl{" +
|
||||
"exception=" + exception +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.text.msg.Messages;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/17/12
|
||||
* Time: 7:30 PM
|
||||
*/
|
||||
public enum CalculatorFixableError {
|
||||
|
||||
must_be_rad(Messages.msg_23, Messages.msg_24, Messages.msg_25) {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setAngleUnits(AngleUnit.rad);
|
||||
}
|
||||
},
|
||||
|
||||
preferred_numeral_base() {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setPreferredNumeralBase();
|
||||
}
|
||||
},
|
||||
|
||||
preferred_angle_units() {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setPreferredAngleUnits();
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private final List<String> messageCodes;
|
||||
|
||||
CalculatorFixableError(@Nullable String... messageCodes) {
|
||||
this.messageCodes = CollectionsUtils.asList(messageCodes);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CalculatorFixableError getErrorByMessageCode(@NotNull String messageCode) {
|
||||
for (CalculatorFixableError fixableError : values()) {
|
||||
if (fixableError.messageCodes.contains(messageCode)) {
|
||||
return fixableError;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract void fix();
|
||||
}
|
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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.CustomFunctionCalculationException;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.model.AFunction;
|
||||
import org.solovyev.android.calculator.model.Functions;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/17/11
|
||||
* Time: 11:28 PM
|
||||
*/
|
||||
public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegistry<Function, AFunction> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
static {
|
||||
substitutes.put("√", "sqrt");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
|
||||
|
||||
public CalculatorFunctionsMathRegistry(@NotNull MathRegistry<Function> functionsRegistry,
|
||||
@NotNull MathEntityDao<AFunction> mathEntityDao) {
|
||||
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
super.load();
|
||||
|
||||
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
|
||||
add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
|
||||
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
add(new CustomFunction.Builder(true, "re", Arrays.asList("x"), "(x+conjugate(x))/2"));
|
||||
add(new CustomFunction.Builder(true, "im", Arrays.asList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
}
|
||||
|
||||
public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry,
|
||||
@NotNull MathEntityBuilder<? extends Function> builder,
|
||||
@Nullable IFunction editedInstance,
|
||||
@NotNull Object source, boolean save) throws CustomFunctionCalculationException {
|
||||
final Function addedFunction = registry.add(builder);
|
||||
|
||||
if (save) {
|
||||
registry.save();
|
||||
}
|
||||
|
||||
if (editedInstance == null) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.function_added, addedFunction, source);
|
||||
} else {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.function_changed, ChangeImpl.newInstance(editedInstance, addedFunction), source);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Function function) {
|
||||
for (FunctionCategory category : FunctionCategory.values()) {
|
||||
if ( category.isInCategory(function) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDescription(@NotNull String functionName) {
|
||||
final Function function = get(functionName);
|
||||
|
||||
String result = null;
|
||||
if ( function instanceof CustomFunction ) {
|
||||
result = ((CustomFunction) function).getDescription();
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(result) ) {
|
||||
result = super.getDescription(functionName);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction function) {
|
||||
return new CustomFunction.Builder(function);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AFunction transform(@NotNull Function function) {
|
||||
if (function instanceof CustomFunction) {
|
||||
return AFunction.fromIFunction((CustomFunction) function);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<AFunction> createPersistenceContainer() {
|
||||
return new Functions();
|
||||
}
|
||||
}
|
@@ -0,0 +1,601 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.android.calculator.model.Var;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.common.history.HistoryAction;
|
||||
import org.solovyev.common.msg.ListMessageRegistry;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageRegistry;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
import org.solovyev.math.units.ConversionException;
|
||||
import org.solovyev.math.units.ConversionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:42
|
||||
*/
|
||||
public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// one minute
|
||||
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||
|
||||
@NotNull
|
||||
private final AtomicLong counter = new AtomicLong(CalculatorUtils.FIRST_ID);
|
||||
|
||||
@NotNull
|
||||
private final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance();
|
||||
|
||||
@NotNull
|
||||
private final Executor calculationsExecutor = Executors.newFixedThreadPool(10);
|
||||
|
||||
// NOTE: only one thread is responsible for events as all events must be done in order of their creating
|
||||
@NotNull
|
||||
private final Executor eventExecutor = Executors.newFixedThreadPool(1);
|
||||
|
||||
private volatile boolean calculateOnFly = true;
|
||||
|
||||
private volatile long lastPreferenceCheck = 0L;
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public CalculatorImpl() {
|
||||
this.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData(@NotNull Object source) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId, source);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData(@NotNull Long sequenceId) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, sequenceId);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate(@NotNull Long sequenceId) {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simplify() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation,
|
||||
@NotNull final String expression) {
|
||||
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventDataId = nextEventData(sequenceId);
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
Locator.getInstance().getEngine().init();
|
||||
Locator.getInstance().getHistory().load();
|
||||
}
|
||||
|
||||
public void setCalculateOnFly(boolean calculateOnFly) {
|
||||
this.calculateOnFly = calculateOnFly;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorConversionEventData newConversionEventData(@NotNull Long sequenceId,
|
||||
@NotNull Generic value,
|
||||
@NotNull NumeralBase from,
|
||||
@NotNull NumeralBase to,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState);
|
||||
}
|
||||
|
||||
private void evaluate(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr) {
|
||||
|
||||
checkPreferredPreferences();
|
||||
|
||||
PreparedExpression preparedExpression = null;
|
||||
|
||||
try {
|
||||
|
||||
expression = expression.trim();
|
||||
|
||||
if (StringUtils.isEmpty(expression)) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
||||
} else {
|
||||
preparedExpression = prepareExpression(expression);
|
||||
|
||||
final String jsclExpression = preparedExpression.toString();
|
||||
|
||||
try {
|
||||
|
||||
final CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
|
||||
|
||||
final MessageRegistry messageRegistry = new ListMessageRegistry();
|
||||
Locator.getInstance().getEngine().getMathEngine0().setMessageRegistry(messageRegistry);
|
||||
|
||||
final Generic result = operation.evaluateGeneric(jsclExpression, mathEngine);
|
||||
|
||||
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||
result.toString();
|
||||
|
||||
if (messageRegistry.hasMessage()) {
|
||||
final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
try {
|
||||
final List<Message> messages = new ArrayList<Message>();
|
||||
while (messageRegistry.hasMessage()) {
|
||||
messages.add(messageRegistry.getMessage());
|
||||
}
|
||||
if (!messages.isEmpty()) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// todo serso: not good but we need proper synchronization
|
||||
logger.error("Calculator", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
|
||||
|
||||
} catch (AbstractJsclArithmeticException e) {
|
||||
handleException(sequenceId, operation, expression, mr, new CalculatorEvalException(e, e, jsclExpression));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ArithmeticException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
|
||||
} catch (StackOverflowError e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
|
||||
} catch (jscl.text.ParseException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
|
||||
} catch (ParseInterruptedException e) {
|
||||
|
||||
// do nothing - we ourselves interrupt the calculations
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
|
||||
|
||||
} catch (CalculatorParseException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkPreferredPreferences() {
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
|
||||
if ( currentTime - lastPreferenceCheck > PREFERENCE_CHECK_INTERVAL ) {
|
||||
lastPreferenceCheck = currentTime;
|
||||
Locator.getInstance().getPreferenceService().checkPreferredPreferences(false);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
||||
return preprocessor.process(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long calculationId) {
|
||||
return new CalculatorEvaluationEventDataImpl(nextEventData(calculationId), operation, expression);
|
||||
}
|
||||
|
||||
private void handleException(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr,
|
||||
@Nullable PreparedExpression preparedExpression,
|
||||
@NotNull CalculatorParseException parseException) {
|
||||
|
||||
if (operation == JsclOperation.numeric
|
||||
&& preparedExpression != null
|
||||
&& preparedExpression.isExistsUndefinedVar()) {
|
||||
|
||||
evaluate(sequenceId, JsclOperation.simplify, expression, mr);
|
||||
} else {
|
||||
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(parseException));
|
||||
}
|
||||
}
|
||||
|
||||
private void handleException(@NotNull Long calculationId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr,
|
||||
@NotNull CalculatorEvalException evalException) {
|
||||
|
||||
if (operation == JsclOperation.numeric && evalException.getCause() instanceof NumeralBaseException) {
|
||||
evaluate(calculationId, JsclOperation.simplify, expression, mr);
|
||||
} else {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(evalException));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData convert(@NotNull final Generic value,
|
||||
@NotNull final NumeralBase to) {
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase();
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Long sequenceId = eventDataId.getSequenceId();
|
||||
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_started, null);
|
||||
try {
|
||||
|
||||
final String result = doConversion(value, from, to);
|
||||
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_result, result);
|
||||
|
||||
} catch (ConversionException e) {
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_failed, new ConversionFailureImpl(e));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String doConversion(@NotNull Generic generic,
|
||||
@NotNull NumeralBase from,
|
||||
@NotNull NumeralBase to) throws ConversionException {
|
||||
final String result;
|
||||
|
||||
if (from != to) {
|
||||
String fromString = generic.toString();
|
||||
if (!StringUtils.isEmpty(fromString)) {
|
||||
try {
|
||||
fromString = ToJsclTextProcessor.getInstance().process(fromString).getExpression();
|
||||
} catch (CalculatorParseException e) {
|
||||
// ok, problems while processing occurred
|
||||
}
|
||||
}
|
||||
|
||||
result = ConversionUtils.doConversion(CalculatorNumeralBase.getConverter(), fromString, CalculatorNumeralBase.valueOf(from), CalculatorNumeralBase.valueOf(to));
|
||||
} else {
|
||||
result = generic.toString();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConversionPossible(@NotNull Generic generic, NumeralBase from, @NotNull NumeralBase to) {
|
||||
try {
|
||||
doConversion(generic, from, to);
|
||||
return true;
|
||||
} catch (ConversionException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculatorEventContainer.addCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculatorEventContainer.removeCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull final CalculatorEventData calculatorEventData, @NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
eventExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull final List<CalculatorEvent> calculatorEvents) {
|
||||
eventExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
final CalculatorEventData eventData = nextEventData();
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Object source) {
|
||||
final CalculatorEventData eventData = nextEventData(source);
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventData = nextEventData(sequenceId);
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS HANDLER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
|
||||
switch (calculatorEventType) {
|
||||
case editor_state_changed:
|
||||
if (calculateOnFly) {
|
||||
final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data;
|
||||
|
||||
final String newText = editorChangeEventData.getNewValue().getText();
|
||||
final String oldText = editorChangeEventData.getOldValue().getText();
|
||||
|
||||
if (!newText.equals(oldText)) {
|
||||
evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case display_state_changed:
|
||||
onDisplayStateChanged((CalculatorDisplayChangeEventData) data);
|
||||
break;
|
||||
|
||||
case constant_changed:
|
||||
final IConstant newConstant = ((Change<IConstant>)data) .getNewValue();
|
||||
if (!newConstant.getName().equals(CalculatorVarsRegistry.ANS)) {
|
||||
evaluate();
|
||||
}
|
||||
break;
|
||||
|
||||
case constant_added:
|
||||
case constant_removed:
|
||||
case function_added:
|
||||
case function_changed:
|
||||
case function_removed:
|
||||
evaluate();
|
||||
break;
|
||||
|
||||
case engine_preferences_changed:
|
||||
evaluate(calculatorEventData.getSequenceId());
|
||||
break;
|
||||
|
||||
case use_constant:
|
||||
final IConstant constant = (IConstant)data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(constant.getName());
|
||||
break;
|
||||
|
||||
case use_operator:
|
||||
final Operator operator = (Operator)data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(operator.getName());
|
||||
break;
|
||||
|
||||
case use_function:
|
||||
final Function function = (Function)data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(function.getName());
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void onDisplayStateChanged(@NotNull CalculatorDisplayChangeEventData displayChangeEventData) {
|
||||
final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue();
|
||||
if (newState.isValid()) {
|
||||
final String result = newState.getStringResult();
|
||||
if ( !StringUtils.isEmpty(result) ) {
|
||||
final CalculatorMathRegistry<IConstant> varsRegistry = Locator.getInstance().getEngine().getVarsRegistry();
|
||||
final IConstant ansVar = varsRegistry.get(CalculatorVarsRegistry.ANS);
|
||||
|
||||
final Var.Builder varBuilder;
|
||||
if (ansVar != null) {
|
||||
varBuilder = new Var.Builder(ansVar);
|
||||
} else {
|
||||
varBuilder = new Var.Builder();
|
||||
}
|
||||
|
||||
varBuilder.setName(CalculatorVarsRegistry.ANS);
|
||||
varBuilder.setValue(result);
|
||||
varBuilder.setDescription(CalculatorMessages.getBundle().getString("ans_description"));
|
||||
|
||||
CalculatorVarsRegistry.saveVariable(varsRegistry, varBuilder, ansVar, this, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = Locator.getInstance().getHistory();
|
||||
if (history.isActionAvailable(historyAction)) {
|
||||
final CalculatorHistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
|
||||
if (newState != null) {
|
||||
setCurrentHistoryState(newState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) {
|
||||
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return CalculatorHistoryState.newInstance(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* OTHER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor getEditor() {
|
||||
return Locator.getInstance().getEditor();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplay getDisplay() {
|
||||
return Locator.getInstance().getDisplay();
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:25 PM
|
||||
*/
|
||||
public interface CalculatorInput {
|
||||
|
||||
@NotNull
|
||||
String getExpression();
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:26 PM
|
||||
*/
|
||||
public class CalculatorInputImpl implements CalculatorInput {
|
||||
|
||||
@NotNull
|
||||
private String expression;
|
||||
|
||||
@NotNull
|
||||
private JsclOperation operation;
|
||||
|
||||
public CalculatorInputImpl(@NotNull String expression, @NotNull JsclOperation operation) {
|
||||
this.expression = expression;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JsclOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 1:08 PM
|
||||
*/
|
||||
public interface CalculatorKeyboard {
|
||||
|
||||
void buttonPressed(@Nullable String text);
|
||||
|
||||
void roundBracketsButtonPressed();
|
||||
|
||||
void pasteButtonPressed();
|
||||
|
||||
void clearButtonPressed();
|
||||
|
||||
void copyButtonPressed();
|
||||
|
||||
void moveCursorLeft();
|
||||
|
||||
void moveCursorRight();
|
||||
}
|
@@ -0,0 +1,124 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 1:08 PM
|
||||
*/
|
||||
public class CalculatorKeyboardImpl implements CalculatorKeyboard {
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
public CalculatorKeyboardImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buttonPressed(@Nullable final String text) {
|
||||
|
||||
if (!StringUtils.isEmpty(text)) {
|
||||
assert text != null;
|
||||
|
||||
// process special buttons
|
||||
boolean processed = processSpecialButtons(text);
|
||||
|
||||
if (!processed) {
|
||||
int cursorPositionOffset = 0;
|
||||
final StringBuilder textToBeInserted = new StringBuilder(text);
|
||||
|
||||
final MathType.Result mathType = MathType.getType(text, 0, false);
|
||||
switch (mathType.getMathType()) {
|
||||
case function:
|
||||
textToBeInserted.append("()");
|
||||
cursorPositionOffset = -1;
|
||||
break;
|
||||
case operator:
|
||||
textToBeInserted.append("()");
|
||||
cursorPositionOffset = -1;
|
||||
break;
|
||||
case comma:
|
||||
textToBeInserted.append(" ");
|
||||
break;
|
||||
}
|
||||
|
||||
if (cursorPositionOffset == 0) {
|
||||
if (MathType.openGroupSymbols.contains(text)) {
|
||||
cursorPositionOffset = -1;
|
||||
}
|
||||
}
|
||||
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
editor.insert(textToBeInserted.toString(), cursorPositionOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean processSpecialButtons(@NotNull String text) {
|
||||
boolean result = false;
|
||||
|
||||
final CalculatorSpecialButton button = CalculatorSpecialButton.getByActionCode(text);
|
||||
if ( button != null ) {
|
||||
button.onClick(this);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void roundBracketsButtonPressed() {
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
CalculatorEditorViewState viewState = editor.getViewState();
|
||||
|
||||
final int cursorPosition = viewState.getSelection();
|
||||
final String oldText = viewState.getText();
|
||||
|
||||
final StringBuilder newText = new StringBuilder(oldText.length() + 2);
|
||||
newText.append("(");
|
||||
newText.append(oldText.substring(0, cursorPosition));
|
||||
newText.append(")");
|
||||
newText.append(oldText.substring(cursorPosition));
|
||||
editor.setText(newText.toString(), cursorPosition + 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pasteButtonPressed() {
|
||||
final String text = Locator.getInstance().getClipboard().getText();
|
||||
if (text != null) {
|
||||
Locator.getInstance().getEditor().insert(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearButtonPressed() {
|
||||
Locator.getInstance().getEditor().clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copyButtonPressed() {
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
if (displayViewState.isValid()) {
|
||||
final CharSequence text = displayViewState.getText();
|
||||
if (!StringUtils.isEmpty(text)) {
|
||||
Locator.getInstance().getClipboard().setText(text);
|
||||
Locator.getInstance().getNotifier().showMessage(CalculatorMessage.newInfoMessage(CalculatorMessages.result_copied));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorLeft() {
|
||||
Locator.getInstance().getEditor().moveCursorLeft();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorRight() {
|
||||
Locator.getInstance().getEditor().moveCursorRight();
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 12:45
|
||||
*/
|
||||
public interface CalculatorLocator {
|
||||
|
||||
void init(@NotNull Calculator calculator,
|
||||
@NotNull CalculatorEngine engine,
|
||||
@NotNull CalculatorClipboard clipboard,
|
||||
@NotNull CalculatorNotifier notifier,
|
||||
@NotNull CalculatorHistory history,
|
||||
@NotNull CalculatorLogger logger,
|
||||
@NotNull CalculatorPreferenceService preferenceService,
|
||||
@NotNull CalculatorKeyboard keyboard,
|
||||
@NotNull CalculatorExternalListenersContainer externalListenersContainer);
|
||||
|
||||
@NotNull
|
||||
Calculator getCalculator();
|
||||
|
||||
@NotNull
|
||||
CalculatorEngine getEngine();
|
||||
|
||||
@NotNull
|
||||
CalculatorDisplay getDisplay();
|
||||
|
||||
@NotNull
|
||||
CalculatorEditor getEditor();
|
||||
|
||||
@NotNull
|
||||
CalculatorKeyboard getKeyboard();
|
||||
|
||||
@NotNull
|
||||
CalculatorClipboard getClipboard();
|
||||
|
||||
@NotNull
|
||||
CalculatorNotifier getNotifier();
|
||||
|
||||
@NotNull
|
||||
CalculatorHistory getHistory();
|
||||
|
||||
@NotNull
|
||||
CalculatorLogger getLogger();
|
||||
|
||||
@NotNull
|
||||
CalculatorPreferenceService getPreferenceService();
|
||||
|
||||
@NotNull
|
||||
CalculatorExternalListenersContainer getExternalListenersContainer();
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/11/12
|
||||
* Time: 12:11 AM
|
||||
*/
|
||||
public interface CalculatorLogger {
|
||||
|
||||
void debug(@Nullable String tag, @NotNull String message);
|
||||
|
||||
void debug(@Nullable String tag, @NotNull String message, @NotNull Throwable e);
|
||||
|
||||
void error(@Nullable String tag, @NotNull String message, @NotNull Throwable e);
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.text.ParseException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/23/12
|
||||
* Time: 6:05 PM
|
||||
*/
|
||||
public interface CalculatorMathEngine {
|
||||
|
||||
@NotNull
|
||||
String evaluate(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
String simplify(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
String elementary(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
Generic evaluateGeneric(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
Generic simplifyGeneric(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
Generic elementaryGeneric(@NotNull String expression) throws ParseException;
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.math.MathEntity;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/30/11
|
||||
* Time: 1:02 AM
|
||||
*/
|
||||
public interface CalculatorMathRegistry<T extends MathEntity> extends MathRegistry<T> {
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull String mathEntityName);
|
||||
|
||||
@Nullable
|
||||
String getCategory(@NotNull T mathEntity);
|
||||
|
||||
void load();
|
||||
|
||||
void save();
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.AbstractMessage;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 8:06 PM
|
||||
*/
|
||||
public class CalculatorMessage extends AbstractMessage {
|
||||
|
||||
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
|
||||
super(messageCode, messageType, parameters);
|
||||
}
|
||||
|
||||
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @NotNull List<?> parameters) {
|
||||
super(messageCode, messageType, parameters);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message newInfoMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.info, parameters);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message newWarningMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.warning, parameters);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.error, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMessagePattern(@NotNull Locale locale) {
|
||||
final ResourceBundle rb = CalculatorMessages.getBundle(locale);
|
||||
return rb.getString(getMessageCode());
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 8:10 PM
|
||||
*/
|
||||
public final class CalculatorMessages {
|
||||
|
||||
@NotNull
|
||||
private static final Map<Locale, ResourceBundle> bundlesByLocale = new HashMap<Locale, ResourceBundle>();
|
||||
|
||||
|
||||
private CalculatorMessages() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ResourceBundle getBundle() {
|
||||
return getBundle(Locale.getDefault());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ResourceBundle getBundle(@NotNull Locale locale) {
|
||||
synchronized (bundlesByLocale) {
|
||||
ResourceBundle result = bundlesByLocale.get(locale);
|
||||
if (result == null) {
|
||||
result = ResourceBundle.getBundle("org/solovyev/android/calculator/messages", locale);
|
||||
bundlesByLocale.put(locale, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/* Arithmetic error occurred: {0} */
|
||||
public static final String msg_001 = "msg_1";
|
||||
|
||||
/* Too complex expression*/
|
||||
public static final String msg_002 = "msg_2";
|
||||
|
||||
/* Too long execution time - check the expression*/
|
||||
public static final String msg_003 = "msg_3";
|
||||
|
||||
/* Evaluation was cancelled*/
|
||||
public static final String msg_004 = "msg_4";
|
||||
|
||||
/* No parameters are specified for function: {0}*/
|
||||
public static final String msg_005 = "msg_5";
|
||||
|
||||
/* Infinite loop is detected in expression*/
|
||||
public static final String msg_006 = "msg_6";
|
||||
|
||||
/* Error */
|
||||
public static final String syntax_error = "syntax_error";
|
||||
|
||||
/* Result copied to clipboard! */
|
||||
public static final String result_copied = "result_copied";
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 1:52 PM
|
||||
*/
|
||||
public interface CalculatorNotifier {
|
||||
|
||||
void showMessage(@NotNull Message message);
|
||||
|
||||
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters);
|
||||
|
||||
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters);
|
||||
|
||||
void showDebugMessage(@Nullable String tag, @NotNull String message);
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.operator.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/17/11
|
||||
* Time: 11:29 PM
|
||||
*/
|
||||
public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
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_";
|
||||
|
||||
public CalculatorOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : OperatorCategory.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Operator> 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<MathPersistenceEntity> createPersistenceContainer() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:29 PM
|
||||
*/
|
||||
public interface CalculatorOutput {
|
||||
|
||||
@NotNull
|
||||
String getStringResult();
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
||||
|
||||
// null in case of empty expression
|
||||
@Nullable
|
||||
Generic getResult();
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:28 PM
|
||||
*/
|
||||
public class CalculatorOutputImpl implements CalculatorOutput {
|
||||
|
||||
@Nullable
|
||||
private Generic result;
|
||||
|
||||
@NotNull
|
||||
private String stringResult;
|
||||
|
||||
@NotNull
|
||||
private JsclOperation operation;
|
||||
|
||||
private CalculatorOutputImpl(@NotNull String stringResult,
|
||||
@NotNull JsclOperation operation,
|
||||
@Nullable Generic result) {
|
||||
this.stringResult = stringResult;
|
||||
this.operation = operation;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOutput newOutput(@NotNull String stringResult,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull Generic result) {
|
||||
return new CalculatorOutputImpl(stringResult, operation, result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOutput newEmptyOutput(@NotNull JsclOperation operation) {
|
||||
return new CalculatorOutputImpl("", operation, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JsclOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Generic getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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.exceptions.SersoException;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/6/11
|
||||
* Time: 9:25 PM
|
||||
*/
|
||||
public class CalculatorParseException extends SersoException implements Message {
|
||||
|
||||
@NotNull
|
||||
private final Message message;
|
||||
|
||||
@NotNull
|
||||
private final String expression;
|
||||
|
||||
@Nullable
|
||||
private final Integer position;
|
||||
|
||||
public CalculatorParseException(@NotNull jscl.text.ParseException jsclParseException) {
|
||||
this.message = jsclParseException;
|
||||
this.expression = jsclParseException.getExpression();
|
||||
this.position = jsclParseException.getPosition();
|
||||
}
|
||||
|
||||
public CalculatorParseException(@Nullable Integer position,
|
||||
@NotNull String expression,
|
||||
@NotNull Message message) {
|
||||
this.message = message;
|
||||
this.expression = expression;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public CalculatorParseException(@NotNull String expression,
|
||||
@NotNull Message message) {
|
||||
this(null, expression, message);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMessageCode() {
|
||||
return this.message.getMessageCode();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Object> getParameters() {
|
||||
return this.message.getParameters();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return this.message.getMessageType();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getLocalizedMessage() {
|
||||
return this.message.getLocalizedMessage(Locale.getDefault());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getLocalizedMessage(@NotNull Locale locale) {
|
||||
return this.message.getLocalizedMessage(locale);
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.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 CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
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_";
|
||||
|
||||
public CalculatorPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : OperatorCategory.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Operator> 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<MathPersistenceEntity> createPersistenceContainer() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/17/12
|
||||
* Time: 7:45 PM
|
||||
*/
|
||||
public interface CalculatorPreferenceService {
|
||||
|
||||
void setPreferredAngleUnits();
|
||||
void setAngleUnits(@NotNull AngleUnit angleUnit);
|
||||
|
||||
void setPreferredNumeralBase();
|
||||
void setNumeralBase(@NotNull NumeralBase numeralBase);
|
||||
|
||||
void checkPreferredPreferences(boolean force);
|
||||
}
|
@@ -0,0 +1,171 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/20/12
|
||||
* Time: 2:05 PM
|
||||
*/
|
||||
public enum CalculatorSpecialButton {
|
||||
|
||||
history("history") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history, null);
|
||||
}
|
||||
},
|
||||
history_detached("history_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history_detached, null);
|
||||
}
|
||||
},
|
||||
cursor_right("▶") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.moveCursorRight();
|
||||
}
|
||||
},
|
||||
cursor_left("◀") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.moveCursorLeft();
|
||||
}
|
||||
},
|
||||
settings("settings") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null);
|
||||
}
|
||||
},
|
||||
|
||||
settings_detached("settings_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null);
|
||||
}
|
||||
},
|
||||
|
||||
like("like") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_like_dialog, null);
|
||||
}
|
||||
},
|
||||
erase("erase") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getEditor().erase();
|
||||
}
|
||||
},
|
||||
paste("paste") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.pasteButtonPressed();
|
||||
}
|
||||
},
|
||||
copy("copy") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.copyButtonPressed();
|
||||
}
|
||||
},
|
||||
equals("=") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().evaluate();
|
||||
}
|
||||
},
|
||||
clear("clear") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.clearButtonPressed();
|
||||
}
|
||||
},
|
||||
functions("functions") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions, null);
|
||||
}
|
||||
},
|
||||
functions_detached("functions_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions_detached, null);
|
||||
}
|
||||
},
|
||||
open_app("open_app") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.open_app, null);
|
||||
}
|
||||
},
|
||||
vars("vars") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars, null);
|
||||
}
|
||||
},
|
||||
vars_detached("vars_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars_detached, null);
|
||||
}
|
||||
},
|
||||
operators("operators") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null);
|
||||
}
|
||||
},
|
||||
|
||||
operators_detached("operators_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators_detached, null);
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<String, CalculatorSpecialButton>();
|
||||
|
||||
@NotNull
|
||||
private final String actionCode;
|
||||
|
||||
CalculatorSpecialButton(@NotNull String actionCode) {
|
||||
this.actionCode = actionCode;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getActionCode() {
|
||||
return actionCode;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CalculatorSpecialButton getByActionCode(@NotNull String actionCode) {
|
||||
initButtonsByActionCodesMap();
|
||||
return buttonsByActionCodes.get(actionCode);
|
||||
}
|
||||
|
||||
public abstract void onClick(@NotNull CalculatorKeyboard keyboard);
|
||||
|
||||
private static void initButtonsByActionCodesMap() {
|
||||
if ( buttonsByActionCodes.isEmpty() ) {
|
||||
// if not initialized
|
||||
|
||||
final CalculatorSpecialButton[] specialButtons = values();
|
||||
|
||||
final Map<String, CalculatorSpecialButton> localButtonsByActionCodes = new HashMap<String, CalculatorSpecialButton>(specialButtons.length);
|
||||
for (CalculatorSpecialButton specialButton : specialButtons) {
|
||||
localButtonsByActionCodes.put(specialButton.getActionCode(), specialButton);
|
||||
}
|
||||
|
||||
buttonsByActionCodes = localButtonsByActionCodes;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 7:13 PM
|
||||
*/
|
||||
public final class CalculatorUtils {
|
||||
|
||||
static final long FIRST_ID = 0;
|
||||
|
||||
private CalculatorUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEventData createFirstEventDataId() {
|
||||
return CalculatorEventDataImpl.newInstance(FIRST_ID, FIRST_ID);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Set<Constant> getNotSystemConstants(@NotNull Generic expression) {
|
||||
final Set<Constant> notSystemConstants = new HashSet<Constant>();
|
||||
|
||||
for (Constant constant : expression.getConstants()) {
|
||||
IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(constant.getName());
|
||||
if (var != null && !var.isSystem() && !var.isDefined()) {
|
||||
notSystemConstants.add(constant);
|
||||
}
|
||||
}
|
||||
|
||||
return notSystemConstants;
|
||||
}
|
||||
|
||||
public static boolean isPlotPossible(@NotNull Generic expression, @NotNull JsclOperation operation) {
|
||||
boolean result = false;
|
||||
|
||||
if (operation == JsclOperation.simplify) {
|
||||
if (getNotSystemConstants(expression).size() == 1) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
import org.solovyev.android.calculator.model.Var;
|
||||
import org.solovyev.android.calculator.model.Vars;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/29/11
|
||||
* Time: 4:57 PM
|
||||
*/
|
||||
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
|
||||
|
||||
@NotNull
|
||||
public static final String ANS = "ans";
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
static {
|
||||
substitutes.put("π", "pi");
|
||||
substitutes.put("Π", "PI");
|
||||
substitutes.put("∞", "inf");
|
||||
substitutes.put("h", "h_reduced");
|
||||
substitutes.put("NaN", "nan");
|
||||
}
|
||||
|
||||
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
|
||||
@NotNull MathEntityDao<Var> mathEntityDao) {
|
||||
super(mathRegistry, "c_var_description_", mathEntityDao);
|
||||
}
|
||||
|
||||
public static <T extends MathEntity> void saveVariable(@NotNull CalculatorMathRegistry<T> registry,
|
||||
@NotNull MathEntityBuilder<? extends T> builder,
|
||||
@Nullable T editedInstance,
|
||||
@NotNull Object source, boolean save) {
|
||||
final T addedVar = registry.add(builder);
|
||||
|
||||
if (save) {
|
||||
registry.save();
|
||||
}
|
||||
|
||||
if (editedInstance == null) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source);
|
||||
} else {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
public synchronized void load() {
|
||||
super.load();
|
||||
|
||||
tryToAddAuxVar("x");
|
||||
tryToAddAuxVar("y");
|
||||
tryToAddAuxVar("t");
|
||||
tryToAddAuxVar("j");
|
||||
|
||||
|
||||
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
|
||||
for (Var var : vars) {
|
||||
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends IConstant> createBuilder(@NotNull Var entity) {
|
||||
return new Var.Builder(entity);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
|
||||
return new Vars();
|
||||
}
|
||||
|
||||
private void tryToAddAuxVar(@NotNull String name) {
|
||||
if ( !contains(name) ) {
|
||||
add(new Var.Builder(name, (String)null));
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Var transform(@NotNull IConstant entity) {
|
||||
if (entity instanceof Var) {
|
||||
return (Var) entity;
|
||||
} else {
|
||||
return new Var.Builder(entity).create();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription(@NotNull String mathEntityName) {
|
||||
final IConstant var = get(mathEntityName);
|
||||
if (var != null && !var.isSystem()) {
|
||||
return var.getDescription();
|
||||
} else {
|
||||
return super.getDescription(mathEntityName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull IConstant var) {
|
||||
for (VarCategory category : VarCategory.values()) {
|
||||
if ( category.isInCategory(var) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/1/12
|
||||
* Time: 11:16 PM
|
||||
*/
|
||||
public interface Change<T> {
|
||||
|
||||
@NotNull
|
||||
T getOldValue();
|
||||
|
||||
@NotNull
|
||||
T getNewValue();
|
||||
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/1/12
|
||||
* Time: 11:18 PM
|
||||
*/
|
||||
public class ChangeImpl<T> implements Change<T> {
|
||||
|
||||
@NotNull
|
||||
private T oldValue;
|
||||
|
||||
@NotNull
|
||||
private T newValue;
|
||||
|
||||
private ChangeImpl() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <T> Change<T> newInstance(@NotNull T oldValue, @NotNull T newValue) {
|
||||
final ChangeImpl<T> result = new ChangeImpl<T>();
|
||||
|
||||
result.oldValue = oldValue;
|
||||
result.newValue = newValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public T getOldValue() {
|
||||
return this.oldValue;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public T getNewValue() {
|
||||
return this.newValue;
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:12
|
||||
*/
|
||||
public interface ConversionFailure {
|
||||
|
||||
@NotNull
|
||||
Exception getException();
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:12
|
||||
*/
|
||||
public class ConversionFailureImpl implements ConversionFailure {
|
||||
|
||||
@NotNull
|
||||
private Exception exception;
|
||||
|
||||
public ConversionFailureImpl(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Exception getException() {
|
||||
return this.exception;
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 2:00 PM
|
||||
*/
|
||||
public class DummyCalculatorClipboard implements CalculatorClipboard {
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(@NotNull String text) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(@NotNull CharSequence text) {
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 2:00 PM
|
||||
*/
|
||||
public class DummyCalculatorNotifier implements CalculatorNotifier {
|
||||
|
||||
@Override
|
||||
public void showMessage(@NotNull Message message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDebugMessage(@Nullable String tag, @NotNull String message) {
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/17/11
|
||||
* Time: 9:37 PM
|
||||
*/
|
||||
public interface Editor {
|
||||
|
||||
@Nullable
|
||||
CharSequence getText();
|
||||
|
||||
void setText(@Nullable CharSequence text);
|
||||
|
||||
int getSelection();
|
||||
|
||||
void setSelection(int selection);
|
||||
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
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<String> 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<FunctionCategory> getCategoriesByTabOrder() {
|
||||
final List<FunctionCategory> result = CollectionsUtils.asList(FunctionCategory.values());
|
||||
|
||||
Collections.sort(result, new Comparator<FunctionCategory>() {
|
||||
@Override
|
||||
public int compare(FunctionCategory category, FunctionCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.utils.ListListenersContainer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:42
|
||||
*/
|
||||
public class ListCalculatorEventContainer implements CalculatorEventContainer {
|
||||
|
||||
@NotNull
|
||||
private static final String TAG = "CalculatorEventData";
|
||||
|
||||
@NotNull
|
||||
private final ListListenersContainer<CalculatorEventListener> listeners = new ListListenersContainer<CalculatorEventListener>();
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.addListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.removeListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
fireCalculatorEvents(Arrays.asList(new CalculatorEvent(calculatorEventData, calculatorEventType, data)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
final List<CalculatorEventListener> listeners = this.listeners.getListeners();
|
||||
|
||||
//final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
|
||||
for (CalculatorEvent e : calculatorEvents) {
|
||||
//Locator.getInstance().getLogger().debug(TAG, "Event fired: " + e.getCalculatorEventType());
|
||||
for (CalculatorEventListener listener : listeners) {
|
||||
/*long startTime = System.currentTimeMillis();*/
|
||||
listener.onCalculatorEvent(e.getCalculatorEventData(), e.getCalculatorEventType(), e.getData());
|
||||
/* long endTime = System.currentTimeMillis();
|
||||
long totalTime = (endTime - startTime);
|
||||
if ( totalTime > 300 ) {
|
||||
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Started event: " + e.getCalculatorEventType() + " with data: " + e.getData() + " for: " + listener.getClass().getSimpleName());
|
||||
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Total time, ms: " + totalTime);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/15/11
|
||||
* Time: 8:33 PM
|
||||
*/
|
||||
|
||||
public class LiteNumberBuilder extends AbstractNumberBuilder {
|
||||
|
||||
public LiteNumberBuilder(@NotNull CalculatorEngine engine) {
|
||||
super(engine);
|
||||
this.nb = engine.getNumeralBase();
|
||||
}
|
||||
|
||||
public void process(@NotNull MathType.Result mathTypeResult) {
|
||||
if (canContinue(mathTypeResult)) {
|
||||
// let's continue building number
|
||||
if (numberBuilder == null) {
|
||||
// if new number => create new builder
|
||||
numberBuilder = new StringBuilder();
|
||||
}
|
||||
|
||||
if (mathTypeResult.getMathType() != MathType.numeral_base) {
|
||||
// just add matching string
|
||||
numberBuilder.append(mathTypeResult.getMatch());
|
||||
} else {
|
||||
// set explicitly numeral base (do not include it into number)
|
||||
nb = NumeralBase.getByPrefix(mathTypeResult.getMatch());
|
||||
}
|
||||
|
||||
} else {
|
||||
// process current number (and go to the next one)
|
||||
if (numberBuilder != null) {
|
||||
numberBuilder = null;
|
||||
|
||||
// must set default numeral base (exit numeral base mode)
|
||||
nb = engine.getNumeralBase();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
148
core/src/main/java/org/solovyev/android/calculator/Locator.java
Normal file
148
core/src/main/java/org/solovyev/android/calculator/Locator.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 12:45
|
||||
*/
|
||||
public class Locator implements CalculatorLocator {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEngine calculatorEngine;
|
||||
|
||||
@NotNull
|
||||
private Calculator calculator;
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplay calculatorDisplay;
|
||||
|
||||
@NotNull
|
||||
private CalculatorKeyboard calculatorKeyboard;
|
||||
|
||||
@NotNull
|
||||
private CalculatorHistory calculatorHistory;
|
||||
|
||||
@NotNull
|
||||
private CalculatorNotifier calculatorNotifier = new DummyCalculatorNotifier();
|
||||
|
||||
@NotNull
|
||||
private CalculatorLogger calculatorLogger = new SystemOutCalculatorLogger();
|
||||
|
||||
@NotNull
|
||||
private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard();
|
||||
|
||||
@NotNull
|
||||
private static final CalculatorLocator instance = new Locator();
|
||||
|
||||
@NotNull
|
||||
private CalculatorPreferenceService calculatorPreferenceService;
|
||||
|
||||
@NotNull
|
||||
private CalculatorExternalListenersContainer calculatorExternalListenersContainer;
|
||||
|
||||
public Locator() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(@NotNull Calculator calculator,
|
||||
@NotNull CalculatorEngine engine,
|
||||
@NotNull CalculatorClipboard clipboard,
|
||||
@NotNull CalculatorNotifier notifier,
|
||||
@NotNull CalculatorHistory history,
|
||||
@NotNull CalculatorLogger logger,
|
||||
@NotNull CalculatorPreferenceService preferenceService,
|
||||
@NotNull CalculatorKeyboard keyboard,
|
||||
@NotNull CalculatorExternalListenersContainer externalListenersContainer) {
|
||||
|
||||
this.calculator = calculator;
|
||||
this.calculatorEngine = engine;
|
||||
this.calculatorClipboard = clipboard;
|
||||
this.calculatorNotifier = notifier;
|
||||
this.calculatorHistory = history;
|
||||
this.calculatorLogger = logger;
|
||||
this.calculatorPreferenceService = preferenceService;
|
||||
this.calculatorExternalListenersContainer = externalListenersContainer;
|
||||
|
||||
calculatorEditor = new CalculatorEditorImpl(this.calculator);
|
||||
calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
|
||||
calculatorKeyboard = keyboard;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorLocator getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEngine getEngine() {
|
||||
return calculatorEngine;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Calculator getCalculator() {
|
||||
return this.calculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorDisplay getDisplay() {
|
||||
return calculatorDisplay;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditor getEditor() {
|
||||
return calculatorEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorKeyboard getKeyboard() {
|
||||
return calculatorKeyboard;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorClipboard getClipboard() {
|
||||
return calculatorClipboard;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorNotifier getNotifier() {
|
||||
return calculatorNotifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistory getHistory() {
|
||||
return calculatorHistory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorLogger getLogger() {
|
||||
return calculatorLogger;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorPreferenceService getPreferenceService() {
|
||||
return this.calculatorPreferenceService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorExternalListenersContainer getExternalListenersContainer() {
|
||||
return calculatorExternalListenersContainer;
|
||||
}
|
||||
}
|
@@ -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<T extends MathPersistenceEntity> {
|
||||
|
||||
void save(@NotNull MathEntityPersistenceContainer<T> container);
|
||||
|
||||
@Nullable
|
||||
MathEntityPersistenceContainer<T> load();
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull String descriptionId);
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:03 PM
|
||||
*/
|
||||
public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> {
|
||||
|
||||
public List<T> getEntities();
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:27 PM
|
||||
*/
|
||||
public interface MathPersistenceEntity {
|
||||
|
||||
@NotNull
|
||||
String getName();
|
||||
}
|
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* 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.MathContext;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.numeric.Real;
|
||||
import jscl.text.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.common.MutableObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/23/11
|
||||
* Time: 2:57 PM
|
||||
*/
|
||||
public class NumberBuilder extends AbstractNumberBuilder {
|
||||
|
||||
public NumberBuilder(@NotNull CalculatorEngine engine) {
|
||||
super(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method replaces number in text according to some rules (e.g. formatting)
|
||||
*
|
||||
* @param text text where number can be replaced
|
||||
* @param mathTypeResult math type result of current token
|
||||
* @param offset offset between new number length and old number length (newNumberLength - oldNumberLength)
|
||||
*
|
||||
*
|
||||
* @return new math type result (as one can be changed due to substituting of number with constant)
|
||||
*/
|
||||
@NotNull
|
||||
public MathType.Result process(@NotNull StringBuilder text, @NotNull MathType.Result mathTypeResult, @Nullable MutableObject<Integer> offset) {
|
||||
final MathType.Result possibleResult;
|
||||
if (canContinue(mathTypeResult)) {
|
||||
// let's continue building number
|
||||
if (numberBuilder == null) {
|
||||
// if new number => create new builder
|
||||
numberBuilder = new StringBuilder();
|
||||
}
|
||||
|
||||
if (mathTypeResult.getMathType() != MathType.numeral_base) {
|
||||
// just add matching string
|
||||
numberBuilder.append(mathTypeResult.getMatch());
|
||||
} else {
|
||||
// set explicitly numeral base (do not include it into number)
|
||||
nb = NumeralBase.getByPrefix(mathTypeResult.getMatch());
|
||||
}
|
||||
|
||||
possibleResult = null;
|
||||
} else {
|
||||
// process current number (and go to the next one)
|
||||
possibleResult = processNumber(text, offset);
|
||||
}
|
||||
|
||||
return possibleResult == null ? mathTypeResult : possibleResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method replaces number in text according to some rules (e.g. formatting)
|
||||
*
|
||||
* @param text text where number can be replaced
|
||||
* @param offset offset between new number length and old number length (newNumberLength - oldNumberLength)
|
||||
*
|
||||
* @return new math type result (as one can be changed due to substituting of number with constant)
|
||||
*/
|
||||
@Nullable
|
||||
public MathType.Result processNumber(@NotNull StringBuilder text, @Nullable MutableObject<Integer> offset) {
|
||||
// total number of trimmed chars
|
||||
int trimmedChars = 0;
|
||||
|
||||
String number = null;
|
||||
|
||||
// toXml numeral base (as later it might be replaced)
|
||||
final NumeralBase localNb = getNumeralBase();
|
||||
|
||||
if (numberBuilder != null) {
|
||||
try {
|
||||
number = numberBuilder.toString();
|
||||
|
||||
// let's get rid of unnecessary characters (grouping separators, + after E)
|
||||
final List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(MathType.grouping_separator.getTokens());
|
||||
// + after E can be omitted: 10+E = 10E (NOTE: - cannot be omitted )
|
||||
tokens.add("+");
|
||||
for (String groupingSeparator : tokens) {
|
||||
final String trimmedNumber = number.replace(groupingSeparator, "");
|
||||
trimmedChars += number.length() - trimmedNumber.length();
|
||||
number = trimmedNumber;
|
||||
}
|
||||
|
||||
// check if number still valid
|
||||
toDouble(number, getNumeralBase(), engine.getMathEngine0());
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
// number is not valid => stop
|
||||
number = null;
|
||||
}
|
||||
|
||||
numberBuilder = null;
|
||||
|
||||
// must set default numeral base (exit numeral base mode)
|
||||
nb = engine.getNumeralBase();
|
||||
}
|
||||
|
||||
return replaceNumberInText(text, number, trimmedChars, offset, localNb, engine.getMathEngine0());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static MathType.Result replaceNumberInText(@NotNull StringBuilder text,
|
||||
@Nullable String number,
|
||||
int trimmedChars,
|
||||
@Nullable MutableObject<Integer> offset,
|
||||
@NotNull NumeralBase nb,
|
||||
@NotNull final MathEngine engine) {
|
||||
MathType.Result result = null;
|
||||
|
||||
if (number != null) {
|
||||
// in any case remove old number from text
|
||||
final int oldNumberLength = number.length() + trimmedChars;
|
||||
text.delete(text.length() - oldNumberLength, text.length());
|
||||
|
||||
final String newNumber = formatNumber(number, nb, engine);
|
||||
if (offset != null) {
|
||||
// register offset between old number and new number
|
||||
offset.setObject(newNumber.length() - oldNumberLength);
|
||||
}
|
||||
text.append(newNumber);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String formatNumber(@NotNull String number, @NotNull NumeralBase nb, @NotNull MathEngine engine) {
|
||||
String result;
|
||||
|
||||
int indexOfDot = number.indexOf('.');
|
||||
|
||||
if (indexOfDot < 0) {
|
||||
int indexOfE;
|
||||
if (nb == NumeralBase.hex) {
|
||||
indexOfE = -1;
|
||||
} else {
|
||||
indexOfE = number.indexOf(MathType.POWER_10);
|
||||
}
|
||||
if (indexOfE < 0) {
|
||||
result = engine.addGroupingSeparators(nb, number);
|
||||
} else {
|
||||
final String partBeforeE;
|
||||
if (indexOfE != 0) {
|
||||
partBeforeE = engine.addGroupingSeparators(nb, number.substring(0, indexOfE));
|
||||
} else {
|
||||
partBeforeE = "";
|
||||
}
|
||||
result = partBeforeE + number.substring(indexOfE);
|
||||
}
|
||||
} else {
|
||||
final String integerPart;
|
||||
if (indexOfDot != 0) {
|
||||
integerPart = engine.addGroupingSeparators(nb, number.substring(0, indexOfDot));
|
||||
} else {
|
||||
integerPart = "";
|
||||
}
|
||||
result = integerPart + number.substring(indexOfDot);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Double toDouble(@NotNull String s, @NotNull NumeralBase nb, @NotNull final MathContext mc) throws NumberFormatException {
|
||||
final NumeralBase defaultNb = mc.getNumeralBase();
|
||||
try {
|
||||
mc.setNumeralBase(nb);
|
||||
|
||||
try {
|
||||
return JsclIntegerParser.parser.parse(Parser.Parameters.newInstance(s, new MutableInt(0), mc), null).content().doubleValue();
|
||||
} catch (ParseException e) {
|
||||
try {
|
||||
return ((Real) DoubleParser.parser.parse(Parser.Parameters.newInstance(s, new MutableInt(0), mc), null).content()).doubleValue();
|
||||
} catch (ParseException e1) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
mc.setNumeralBase(defaultNb);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.operator.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 7:40 PM
|
||||
*/
|
||||
public enum OperatorCategory {
|
||||
|
||||
derivatives(100){
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
|
||||
}
|
||||
},
|
||||
|
||||
other(200) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Sum || operator instanceof Product;
|
||||
}
|
||||
},
|
||||
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return !operator.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
common(50) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : values()) {
|
||||
if ( category != this ) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private final int tabOrder;
|
||||
|
||||
OperatorCategory(int tabOrder) {
|
||||
this.tabOrder = tabOrder;
|
||||
}
|
||||
|
||||
public abstract boolean isInCategory(@NotNull Operator operator);
|
||||
|
||||
@NotNull
|
||||
public static List<OperatorCategory> getCategoriesByTabOrder() {
|
||||
final List<OperatorCategory> result = CollectionsUtils.asList(OperatorCategory.values());
|
||||
|
||||
Collections.sort(result, new Comparator<OperatorCategory>() {
|
||||
@Override
|
||||
public int compare(OperatorCategory category, OperatorCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
|
||||
// todo serso: current solution (as creating operators is not implemented yet)
|
||||
result.remove(my);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/18/11
|
||||
* Time: 10:07 PM
|
||||
*/
|
||||
public class PreparedExpression implements CharSequence{
|
||||
|
||||
@NotNull
|
||||
private String expression;
|
||||
|
||||
@NotNull
|
||||
private List<IConstant> undefinedVars;
|
||||
|
||||
public PreparedExpression(@NotNull String expression, @NotNull List<IConstant> undefinedVars) {
|
||||
this.expression = expression;
|
||||
this.undefinedVars = undefinedVars;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
public boolean isExistsUndefinedVar() {
|
||||
return !this.undefinedVars.isEmpty();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<IConstant> getUndefinedVars() {
|
||||
return undefinedVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return expression.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public char charAt(int i) {
|
||||
return expression.charAt(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence subSequence(int i, int i1) {
|
||||
return expression.subSequence(i, i1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.expression;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/11/12
|
||||
* Time: 12:12 AM
|
||||
*/
|
||||
public class SystemOutCalculatorLogger implements CalculatorLogger {
|
||||
|
||||
@NotNull
|
||||
private static final String TAG = SystemOutCalculatorLogger.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
public void debug(@Nullable String tag, @NotNull String message) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getTag(@Nullable String tag) {
|
||||
return tag != null ? tag : TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(@Nullable String tag, @NotNull String message, @NotNull Throwable e) {
|
||||
debug(tag, message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(@Nullable String tag, @NotNull String message, @NotNull Throwable e) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
}
|
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.common.StartsWithFinder;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
|
||||
|
||||
@NotNull
|
||||
private static final Integer MAX_DEPTH = 20;
|
||||
|
||||
@NotNull
|
||||
private static final TextProcessor<PreparedExpression, String> instance = new ToJsclTextProcessor();
|
||||
|
||||
private ToJsclTextProcessor() {
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public static TextProcessor<PreparedExpression, String> getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public PreparedExpression process(@NotNull String s) throws CalculatorParseException {
|
||||
return processWithDepth(s, 0, new ArrayList<IConstant>());
|
||||
}
|
||||
|
||||
private static PreparedExpression processWithDepth(@NotNull String s, int depth, @NotNull List<IConstant> undefinedVars) throws CalculatorParseException {
|
||||
return replaceVariables(processExpression(s).toString(), depth, undefinedVars);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static StringBuilder processExpression(@NotNull String s) throws CalculatorParseException {
|
||||
final StartsWithFinder startsWithFinder = new StartsWithFinder(s, 0);
|
||||
final StringBuilder result = new StringBuilder();
|
||||
|
||||
MathType.Result mathTypeResult = null;
|
||||
MathType.Result mathTypeBefore;
|
||||
|
||||
final LiteNumberBuilder nb = new LiteNumberBuilder(Locator.getInstance().getEngine());
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (s.charAt(i) == ' ') continue;
|
||||
startsWithFinder.setI(i);
|
||||
|
||||
mathTypeBefore = mathTypeResult == null ? null : mathTypeResult;
|
||||
|
||||
mathTypeResult = MathType.getType(s, i, nb.isHexMode());
|
||||
|
||||
nb.process(mathTypeResult);
|
||||
|
||||
if (mathTypeBefore != null) {
|
||||
|
||||
final MathType current = mathTypeResult.getMathType();
|
||||
|
||||
if (current.isNeedMultiplicationSignBefore(mathTypeBefore.getMathType())) {
|
||||
result.append("*");
|
||||
}
|
||||
}
|
||||
|
||||
if (mathTypeBefore != null &&
|
||||
(mathTypeBefore.getMathType() == MathType.function || mathTypeBefore.getMathType() == MathType.operator) &&
|
||||
CollectionsUtils.find(MathType.openGroupSymbols, startsWithFinder) != null) {
|
||||
final String functionName = mathTypeBefore.getMatch();
|
||||
final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
|
||||
if ( function == null || function.getMinParameters() > 0 ) {
|
||||
throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.getMatch()));
|
||||
}
|
||||
}
|
||||
|
||||
i = mathTypeResult.processToJscl(result, i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PreparedExpression replaceVariables(@NotNull final String s, int depth, @NotNull List<IConstant> undefinedVars) throws CalculatorParseException {
|
||||
if (depth >= MAX_DEPTH) {
|
||||
throw new CalculatorParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error));
|
||||
} else {
|
||||
depth++;
|
||||
}
|
||||
|
||||
final StartsWithFinder startsWithFinder = new StartsWithFinder(s, 0);
|
||||
|
||||
final StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
startsWithFinder.setI(i);
|
||||
|
||||
int offset = 0;
|
||||
String functionName = CollectionsUtils.find(MathType.function.getTokens(), startsWithFinder);
|
||||
if (functionName == null) {
|
||||
String operatorName = CollectionsUtils.find(MathType.operator.getTokens(), startsWithFinder);
|
||||
if (operatorName == null) {
|
||||
String varName = CollectionsUtils.find(Locator.getInstance().getEngine().getVarsRegistry().getNames(), startsWithFinder);
|
||||
if (varName != null) {
|
||||
final IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(varName);
|
||||
if (var != null) {
|
||||
if (!var.isDefined()) {
|
||||
undefinedVars.add(var);
|
||||
result.append(varName);
|
||||
offset = varName.length();
|
||||
} else {
|
||||
final String value = var.getValue();
|
||||
assert value != null;
|
||||
|
||||
if ( var.getDoubleValue() != null ) {
|
||||
//result.append(value);
|
||||
// NOTE: append varName as JSCL engine will convert it to double if needed
|
||||
result.append(varName);
|
||||
} else {
|
||||
result.append("(").append(processWithDepth(value, depth, undefinedVars)).append(")");
|
||||
}
|
||||
offset = varName.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result.append(operatorName);
|
||||
offset = operatorName.length();
|
||||
}
|
||||
} else {
|
||||
result.append(functionName);
|
||||
offset = functionName.length();
|
||||
}
|
||||
|
||||
|
||||
if (offset == 0) {
|
||||
result.append(s.charAt(i));
|
||||
} else {
|
||||
i += offset - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return new PreparedExpression(result.toString(), undefinedVars);
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 4:25 PM
|
||||
*/
|
||||
public enum VarCategory {
|
||||
|
||||
system(100){
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull IConstant var) {
|
||||
return var.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull IConstant var) {
|
||||
return !var.isSystem();
|
||||
}
|
||||
};
|
||||
|
||||
private final int tabOrder;
|
||||
|
||||
VarCategory(int tabOrder) {
|
||||
this.tabOrder = tabOrder;
|
||||
}
|
||||
|
||||
public abstract boolean isInCategory(@NotNull IConstant var);
|
||||
|
||||
@NotNull
|
||||
public static List<VarCategory> getCategoriesByTabOrder() {
|
||||
final List<VarCategory> result = CollectionsUtils.asList(VarCategory.values());
|
||||
|
||||
Collections.sort(result, new Comparator<VarCategory>() {
|
||||
@Override
|
||||
public int compare(VarCategory category, VarCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package org.solovyev.android.calculator.external;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface CalculatorExternalListenersContainer {
|
||||
|
||||
void addExternalListener(@NotNull Class<?> externalCalculatorClass);
|
||||
|
||||
boolean removeExternalListener(@NotNull Class<?> externalCalculatorClass);
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.history;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Transient;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/15/11
|
||||
* Time: 1:45 PM
|
||||
*/
|
||||
public class AbstractHistoryState implements Cloneable{
|
||||
|
||||
@Element
|
||||
private long time = new Date().getTime();
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String comment;
|
||||
|
||||
@Transient
|
||||
private boolean saved;
|
||||
|
||||
@Transient
|
||||
private int id = 0;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(@Nullable String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public boolean isSaved() {
|
||||
return saved;
|
||||
}
|
||||
|
||||
public void setSaved(boolean saved) {
|
||||
this.saved = saved;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractHistoryState clone() {
|
||||
AbstractHistoryState clone;
|
||||
|
||||
try {
|
||||
clone = (AbstractHistoryState)super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
}
|
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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.CalculatorDisplay;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/17/11
|
||||
* Time: 11:05 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
|
||||
@Transient
|
||||
private boolean valid = true;
|
||||
|
||||
@Transient
|
||||
@Nullable
|
||||
private String errorMessage = null;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private EditorHistoryState editorState;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private JsclOperation jsclOperation;
|
||||
|
||||
@Transient
|
||||
@Nullable
|
||||
private Generic genericResult;
|
||||
|
||||
private CalculatorDisplayHistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayHistoryState newInstance(@NotNull CalculatorDisplayViewState viewState) {
|
||||
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
|
||||
|
||||
result.editorState = EditorHistoryState.newInstance(viewState);
|
||||
|
||||
result.valid = viewState.isValid();
|
||||
result.jsclOperation = viewState.getOperation();
|
||||
result.genericResult = viewState.getResult();
|
||||
result.errorMessage = viewState.getErrorMessage();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setValuesFromHistory(@NotNull CalculatorDisplay display) {
|
||||
if ( this.isValid() ) {
|
||||
display.setViewState(CalculatorDisplayViewStateImpl.newValidState(this.getJsclOperation(), this.getGenericResult(), StringUtils.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition()));
|
||||
} else {
|
||||
display.setViewState(CalculatorDisplayViewStateImpl.newErrorState(this.getJsclOperation(), StringUtils.getNotEmpty(this.getErrorMessage(), "")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public EditorHistoryState getEditorState() {
|
||||
return editorState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsclOperation getJsclOperation() {
|
||||
return jsclOperation;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Generic getGenericResult() {
|
||||
return genericResult;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o;
|
||||
|
||||
if (!editorState.equals(that.editorState)) return false;
|
||||
if (jsclOperation != that.jsclOperation) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = editorState.hashCode();
|
||||
result = 31 * result + jsclOperation.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorDisplayHistoryState{" +
|
||||
"valid=" + valid +
|
||||
", errorMessage='" + errorMessage + '\'' +
|
||||
", editorHistoryState=" + editorState +
|
||||
", jsclOperation=" + jsclOperation +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CalculatorDisplayHistoryState clone() {
|
||||
try {
|
||||
final CalculatorDisplayHistoryState clone = (CalculatorDisplayHistoryState) super.clone();
|
||||
|
||||
clone.editorState = this.editorState.clone();
|
||||
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorEventListener;
|
||||
import org.solovyev.common.history.HistoryHelper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:11
|
||||
*/
|
||||
public interface CalculatorHistory extends HistoryHelper<CalculatorHistoryState>, CalculatorEventListener {
|
||||
|
||||
void load();
|
||||
|
||||
void save();
|
||||
|
||||
void fromXml(@NotNull String xml);
|
||||
|
||||
String toXml();
|
||||
|
||||
void clearSavedHistory();
|
||||
|
||||
void removeSavedHistory(@NotNull CalculatorHistoryState historyState);
|
||||
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getSavedHistory();
|
||||
|
||||
@NotNull
|
||||
CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState);
|
||||
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getStates();
|
||||
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getStates(boolean includeIntermediateStates);
|
||||
|
||||
}
|
@@ -0,0 +1,257 @@
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
import org.solovyev.common.history.HistoryAction;
|
||||
import org.solovyev.common.history.HistoryHelper;
|
||||
import org.solovyev.common.history.SimpleHistoryHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.*;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:12
|
||||
*/
|
||||
public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
@NotNull
|
||||
private final HistoryHelper<CalculatorHistoryState> history = new SimpleHistoryHelper<CalculatorHistoryState>();
|
||||
|
||||
@NotNull
|
||||
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
|
||||
@Nullable
|
||||
private volatile CalculatorEditorViewState lastEditorViewState;
|
||||
|
||||
public CalculatorHistoryImpl(@NotNull Calculator calculator) {
|
||||
calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
synchronized (history) {
|
||||
return this.history.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState getLastHistoryState() {
|
||||
synchronized (history) {
|
||||
return this.history.getLastHistoryState();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUndoAvailable() {
|
||||
synchronized (history) {
|
||||
return history.isUndoAvailable();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.undo(currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedoAvailable() {
|
||||
return history.isRedoAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.redo(currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionAvailable(@NotNull HistoryAction historyAction) {
|
||||
synchronized (history) {
|
||||
return history.isActionAvailable(historyAction);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState doAction(@NotNull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.doAction(historyAction, currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addState(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
history.addState(currentState);
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
synchronized (history) {
|
||||
return history.getStates();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) {
|
||||
if (includeIntermediateStates) {
|
||||
return getStates();
|
||||
} else {
|
||||
final List<CalculatorHistoryState> states = getStates();
|
||||
|
||||
final List<CalculatorHistoryState> result = new LinkedList<CalculatorHistoryState>();
|
||||
|
||||
CalculatorHistoryState laterState = null;
|
||||
for (CalculatorHistoryState state : CollectionsUtils.reversed(states)) {
|
||||
if ( laterState != null ) {
|
||||
final String laterEditorText = laterState.getEditorState().getText();
|
||||
final String editorText = state.getEditorState().getText();
|
||||
if ( laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
|
||||
// intermediate result => skip from add
|
||||
} else {
|
||||
result.add(0, state);
|
||||
}
|
||||
} else {
|
||||
result.add(0, state);
|
||||
}
|
||||
|
||||
laterState = state;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isIntermediate(@NotNull String laterEditorText,
|
||||
@NotNull String editorText) {
|
||||
if ( Math.abs(laterEditorText.length() - editorText.length()) <= 1 ) {
|
||||
if ( laterEditorText.length() > editorText.length() ) {
|
||||
return laterEditorText.startsWith(editorText);
|
||||
} else {
|
||||
return editorText.startsWith(laterEditorText);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
synchronized (history) {
|
||||
this.history.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<CalculatorHistoryState> getSavedHistory() {
|
||||
return Collections.unmodifiableList(savedHistory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState) {
|
||||
if (historyState.isSaved()) {
|
||||
return historyState;
|
||||
} else {
|
||||
final CalculatorHistoryState savedState = historyState.clone();
|
||||
|
||||
savedState.setId(counter.incrementAndGet());
|
||||
savedState.setSaved(true);
|
||||
|
||||
savedHistory.add(savedState);
|
||||
|
||||
return savedState;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// todo serso: create saved/loader class
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// todo serso: create saved/loader class
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromXml(@NotNull String xml) {
|
||||
clearSavedHistory();
|
||||
|
||||
HistoryUtils.fromXml(xml, this.savedHistory);
|
||||
for (CalculatorHistoryState historyState : savedHistory) {
|
||||
historyState.setSaved(true);
|
||||
historyState.setId(counter.incrementAndGet());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toXml() {
|
||||
return HistoryUtils.toXml(this.savedHistory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSavedHistory() {
|
||||
this.savedHistory.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState) {
|
||||
this.savedHistory.remove(historyState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(editor_state_changed, display_state_changed, manual_calculation_requested)) {
|
||||
|
||||
final CalculatorEventHolder.Result result = lastEventData.apply(calculatorEventData);
|
||||
|
||||
if (result.isNewAfter() && result.isNewSameOrAfterSequence() ) {
|
||||
switch (calculatorEventType) {
|
||||
case manual_calculation_requested:
|
||||
lastEditorViewState = (CalculatorEditorViewState) data;
|
||||
break;
|
||||
case editor_state_changed:
|
||||
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
|
||||
lastEditorViewState = editorChangeData.getNewValue();
|
||||
break;
|
||||
case display_state_changed:
|
||||
if (result.isSameSequence()) {
|
||||
if (lastEditorViewState != null) {
|
||||
final CalculatorEditorViewState editorViewState = lastEditorViewState;
|
||||
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
|
||||
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue();
|
||||
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
|
||||
}
|
||||
} else {
|
||||
lastEditorViewState = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/11/11
|
||||
* Time: 12:16 AM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private EditorHistoryState editorState;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private CalculatorDisplayHistoryState displayState;
|
||||
|
||||
private CalculatorHistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
private CalculatorHistoryState(@NotNull EditorHistoryState editorState,
|
||||
@NotNull CalculatorDisplayHistoryState displayState) {
|
||||
this.editorState = editorState;
|
||||
this.displayState = displayState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorHistoryState newInstance(@NotNull CalculatorEditor editor,
|
||||
@NotNull CalculatorDisplay display) {
|
||||
final CalculatorEditorViewState editorViewState = editor.getViewState();
|
||||
final CalculatorDisplayViewState displayViewState = display.getViewState();
|
||||
|
||||
return newInstance(editorViewState, displayViewState);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorHistoryState newInstance(@NotNull CalculatorEditorViewState editorViewState,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState);
|
||||
|
||||
final CalculatorDisplayHistoryState displayHistoryState = CalculatorDisplayHistoryState.newInstance(displayViewState);
|
||||
|
||||
return new CalculatorHistoryState(editorHistoryState, displayHistoryState);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public EditorHistoryState getEditorState() {
|
||||
return editorState;
|
||||
}
|
||||
|
||||
public void setEditorState(@NotNull EditorHistoryState editorState) {
|
||||
this.editorState = editorState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorDisplayHistoryState getDisplayState() {
|
||||
return displayState;
|
||||
}
|
||||
|
||||
public void setDisplayState(@NotNull CalculatorDisplayHistoryState displayState) {
|
||||
this.displayState = displayState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorHistoryState{" +
|
||||
"editorState=" + editorState +
|
||||
", displayState=" + displayState +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
CalculatorHistoryState that = (CalculatorHistoryState) o;
|
||||
|
||||
if (this.isSaved() != that.isSaved()) return false;
|
||||
if (this.getId() != that.getId()) return false;
|
||||
if (!displayState.equals(that.displayState)) return false;
|
||||
if (!editorState.equals(that.editorState)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = Boolean.valueOf(isSaved()).hashCode();
|
||||
result = 31 * result + getId();
|
||||
result = 31 * result + editorState.hashCode();
|
||||
result = 31 * result + displayState.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setValuesFromHistory(@NotNull CalculatorEditor editor, @NotNull CalculatorDisplay display) {
|
||||
this.getEditorState().setValuesFromHistory(editor);
|
||||
this.getDisplayState().setValuesFromHistory(display);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CalculatorHistoryState clone() {
|
||||
final CalculatorHistoryState clone = (CalculatorHistoryState)super.clone();
|
||||
|
||||
clone.editorState = this.editorState.clone();
|
||||
clone.displayState = this.displayState.clone();
|
||||
|
||||
return clone;
|
||||
}
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
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.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditor;
|
||||
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
@Root
|
||||
public class EditorHistoryState implements Cloneable{
|
||||
|
||||
@Element
|
||||
private int cursorPosition;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String text = "";
|
||||
|
||||
private EditorHistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static EditorHistoryState newInstance(@NotNull CalculatorEditorViewState viewState) {
|
||||
final EditorHistoryState result = new EditorHistoryState();
|
||||
|
||||
result.text = String.valueOf(viewState.getText());
|
||||
result.cursorPosition = viewState.getSelection();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static EditorHistoryState newInstance(@NotNull CalculatorDisplayViewState viewState) {
|
||||
final EditorHistoryState result = new EditorHistoryState();
|
||||
|
||||
result.text = viewState.getText();
|
||||
result.cursorPosition = viewState.getSelection();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setValuesFromHistory(@NotNull CalculatorEditor editor) {
|
||||
editor.setText(StringUtils.getNotEmpty(this.getText(), ""));
|
||||
editor.setSelection(this.getCursorPosition());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public int getCursorPosition() {
|
||||
return cursorPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof EditorHistoryState)) return false;
|
||||
|
||||
EditorHistoryState that = (EditorHistoryState) o;
|
||||
|
||||
if (cursorPosition != that.cursorPosition) return false;
|
||||
if (text != null ? !text.equals(that.text) : that.text != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = cursorPosition;
|
||||
result = 31 * result + (text != null ? text.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EditorHistoryState{" +
|
||||
"cursorPosition=" + cursorPosition +
|
||||
", text='" + text + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected EditorHistoryState clone() {
|
||||
try {
|
||||
return (EditorHistoryState)super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.history;
|
||||
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/17/11
|
||||
* Time: 9:30 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class History {
|
||||
|
||||
@ElementList(type = CalculatorHistoryState.class)
|
||||
private List<CalculatorHistoryState> historyItems = new ArrayList<CalculatorHistoryState>();
|
||||
|
||||
public History() {
|
||||
}
|
||||
|
||||
public List<CalculatorHistoryState> getHistoryItems() {
|
||||
return historyItems;
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.history;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/17/11
|
||||
* Time: 9:59 PM
|
||||
*/
|
||||
class HistoryUtils {
|
||||
|
||||
// not intended for instantiation
|
||||
private HistoryUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static void fromXml(@Nullable String xml, @NotNull List<CalculatorHistoryState> historyItems) {
|
||||
if (xml != null) {
|
||||
final Serializer serializer = new Persister();
|
||||
try {
|
||||
final History history = serializer.read(History.class, xml);
|
||||
for (CalculatorHistoryState historyItem : history.getHistoryItems()) {
|
||||
historyItems.add(historyItem);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String toXml(@NotNull List<CalculatorHistoryState> historyItems) {
|
||||
final History history = new History();
|
||||
for (CalculatorHistoryState historyState : historyItems) {
|
||||
if (historyState.isSaved()) {
|
||||
history.getHistoryItems().add(historyState);
|
||||
}
|
||||
}
|
||||
|
||||
final StringWriter xml = new StringWriter();
|
||||
final Serializer serializer = new Persister();
|
||||
try {
|
||||
serializer.write(history, xml);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return xml.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.jscl;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/6/11
|
||||
* Time: 9:48 PM
|
||||
*/
|
||||
class FromJsclNumericTextProcessor implements TextProcessor<String, Generic> {
|
||||
|
||||
public static final FromJsclNumericTextProcessor instance = new FromJsclNumericTextProcessor();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String process(@NotNull Generic numeric) throws CalculatorParseException {
|
||||
return numeric.toString().replace("*", "");
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.jscl;
|
||||
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.text.ParseException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorMathEngine;
|
||||
import org.solovyev.android.calculator.text.DummyTextProcessor;
|
||||
import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
|
||||
public enum JsclOperation {
|
||||
|
||||
simplify,
|
||||
elementary,
|
||||
numeric;
|
||||
|
||||
JsclOperation() {
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public TextProcessor<String, Generic> getFromProcessor() {
|
||||
switch (this) {
|
||||
case simplify:
|
||||
return FromJsclSimplifyTextProcessor.instance;
|
||||
case elementary:
|
||||
return DummyTextProcessor.instance;
|
||||
case numeric:
|
||||
return FromJsclNumericTextProcessor.instance;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final String evaluate(@NotNull String expression, @NotNull CalculatorMathEngine engine) throws ParseException {
|
||||
switch (this) {
|
||||
case simplify:
|
||||
return engine.simplify(expression);
|
||||
case elementary:
|
||||
return engine.elementary(expression);
|
||||
case numeric:
|
||||
return engine.evaluate(expression);
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final Generic evaluateGeneric(@NotNull String expression, @NotNull CalculatorMathEngine engine) throws ParseException {
|
||||
switch (this) {
|
||||
case simplify:
|
||||
return engine.simplifyGeneric(expression);
|
||||
case elementary:
|
||||
return engine.elementaryGeneric(expression);
|
||||
case numeric:
|
||||
return engine.evaluateGeneric(expression);
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,445 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.function.Constants;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.common.JPredicate;
|
||||
import org.solovyev.common.StartsWithFinder;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public enum MathType {
|
||||
|
||||
numeral_base(50, true, false, MathGroupType.number) {
|
||||
|
||||
private final List<String> tokens = new ArrayList<String>(10);
|
||||
{
|
||||
for (NumeralBase numeralBase : NumeralBase.values()) {
|
||||
tokens.add(numeralBase.getJsclPrefix());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
},
|
||||
|
||||
dot(200, true, true, MathGroupType.number, ".") {
|
||||
@Override
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit;
|
||||
}
|
||||
},
|
||||
|
||||
grouping_separator(250, false, false, MathGroupType.number, "'", " "){
|
||||
@Override
|
||||
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) throws CalculatorParseException {
|
||||
return i;
|
||||
}
|
||||
},
|
||||
|
||||
power_10(300, false, false, MathGroupType.number, "E"),
|
||||
|
||||
postfix_function(400, false, true, MathGroupType.function) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getPostfixFunctionsRegistry().getNames();
|
||||
}
|
||||
},
|
||||
|
||||
unary_operation(500, false, false, MathGroupType.operation, "-", "="),
|
||||
binary_operation(600, false, false, MathGroupType.operation, "-", "+", "*", "×", "∙", "/", "^") {
|
||||
@Override
|
||||
protected String getSubstituteToJscl(@NotNull String match) {
|
||||
if (match.equals("×") || match.equals("∙")) {
|
||||
return "*";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
open_group_symbol(800, true, false, MathGroupType.other, "[", "(", "{") {
|
||||
@Override
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != function && mathTypeBefore != operator;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSubstituteToJscl(@NotNull String match) {
|
||||
return "(";
|
||||
}
|
||||
},
|
||||
|
||||
close_group_symbol(900, false, true, MathGroupType.other, "]", ")", "}") {
|
||||
@Override
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSubstituteToJscl(@NotNull String match) {
|
||||
return ")";
|
||||
}
|
||||
},
|
||||
|
||||
function(1000, true, true, MathGroupType.function) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getFunctionsRegistry().getNames();
|
||||
}
|
||||
},
|
||||
|
||||
operator(1050, true, true, MathGroupType.function) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getOperatorsRegistry().getNames();
|
||||
}
|
||||
},
|
||||
|
||||
constant(1100, true, true, MathGroupType.other) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getVarsRegistry().getNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSubstituteFromJscl(@NotNull String match) {
|
||||
return Constants.INF_2.getName().equals(match) ? MathType.INFINITY : super.getSubstituteFromJscl(match);
|
||||
}
|
||||
},
|
||||
|
||||
digit(1125, true, true, MathGroupType.number) {
|
||||
|
||||
private final List<String> tokens = new ArrayList<String>(16);
|
||||
{
|
||||
for (Character character : NumeralBase.hex.getAcceptableCharacters()) {
|
||||
tokens.add(character.toString());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot /*&& mathTypeBefore != numeral_base*/;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
},
|
||||
|
||||
comma(1150, false, false, MathGroupType.other, ","),
|
||||
|
||||
text(1200, false, false, MathGroupType.other) {
|
||||
@Override
|
||||
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
|
||||
if (match.length() > 0) {
|
||||
result.append(match.charAt(0));
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
|
||||
if (match.length() > 0) {
|
||||
result.append(match.charAt(0));
|
||||
}
|
||||
return i;
|
||||
}
|
||||
};
|
||||
|
||||
public static enum MathGroupType {
|
||||
function,
|
||||
number,
|
||||
operation,
|
||||
other
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private final List<String> tokens;
|
||||
|
||||
@NotNull
|
||||
private final Integer priority;
|
||||
|
||||
private final boolean needMultiplicationSignBefore;
|
||||
|
||||
private final boolean needMultiplicationSignAfter;
|
||||
|
||||
@NotNull
|
||||
private final MathGroupType groupType;
|
||||
|
||||
MathType(@NotNull Integer priority,
|
||||
boolean needMultiplicationSignBefore,
|
||||
boolean needMultiplicationSignAfter,
|
||||
@NotNull MathGroupType groupType,
|
||||
@NotNull String... tokens) {
|
||||
this(priority, needMultiplicationSignBefore, needMultiplicationSignAfter, groupType, CollectionsUtils.asList(tokens));
|
||||
}
|
||||
|
||||
MathType(@NotNull Integer priority,
|
||||
boolean needMultiplicationSignBefore,
|
||||
boolean needMultiplicationSignAfter,
|
||||
@NotNull MathGroupType groupType,
|
||||
@NotNull List<String> tokens) {
|
||||
this.priority = priority;
|
||||
this.needMultiplicationSignBefore = needMultiplicationSignBefore;
|
||||
this.needMultiplicationSignAfter = needMultiplicationSignAfter;
|
||||
this.groupType = groupType;
|
||||
this.tokens = Collections.unmodifiableList(tokens);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MathGroupType getGroupType() {
|
||||
return groupType;
|
||||
}
|
||||
|
||||
/* public static int getPostfixFunctionStart(@NotNull CharSequence s, int position) throws ParseException {
|
||||
assert s.length() > position;
|
||||
|
||||
int numberOfOpenGroups = 0;
|
||||
int result = position;
|
||||
for (; result >= 0; result--) {
|
||||
|
||||
final MathType mathType = getType(s.toString(), result).getMathType();
|
||||
|
||||
if (CollectionsUtils.contains(mathType, digit, dot, grouping_separator, power_10)) {
|
||||
// continue
|
||||
} else if (mathType == close_group_symbol) {
|
||||
numberOfOpenGroups++;
|
||||
} else if (mathType == open_group_symbol) {
|
||||
if (numberOfOpenGroups > 0) {
|
||||
numberOfOpenGroups--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (stop(s, numberOfOpenGroups, result)) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (numberOfOpenGroups != 0){
|
||||
throw new ParseException("Could not find start of prefix function!");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean stop(CharSequence s, int numberOfOpenGroups, int i) {
|
||||
if (numberOfOpenGroups == 0) {
|
||||
if (i > 0) {
|
||||
final EndsWithFinder endsWithFinder = new EndsWithFinder(s);
|
||||
endsWithFinder.setI(i + 1);
|
||||
if (!CollectionsUtils.contains(function.getTokens(), FilterType.included, endsWithFinder)) {
|
||||
MathType type = getType(s.toString(), i).getMathType();
|
||||
if (type != constant) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}*/
|
||||
|
||||
@NotNull
|
||||
public List<String> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private boolean isNeedMultiplicationSignBefore() {
|
||||
return needMultiplicationSignBefore;
|
||||
}
|
||||
|
||||
private boolean isNeedMultiplicationSignAfter() {
|
||||
return needMultiplicationSignAfter;
|
||||
}
|
||||
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return needMultiplicationSignBefore && mathTypeBefore.isNeedMultiplicationSignAfter();
|
||||
}
|
||||
|
||||
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) throws CalculatorParseException {
|
||||
final String substitute = getSubstituteToJscl(match);
|
||||
result.append(substitute == null ? match : substitute);
|
||||
return returnI(i, match);
|
||||
}
|
||||
|
||||
protected int returnI(int i, @NotNull String match) {
|
||||
if (match.length() > 1) {
|
||||
return i + match.length() - 1;
|
||||
} else {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
|
||||
final String substitute = getSubstituteFromJscl(match);
|
||||
result.append(substitute == null ? match : substitute);
|
||||
return returnI(i, match);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected String getSubstituteFromJscl(@NotNull String match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected String getSubstituteToJscl(@NotNull String match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final List<String> openGroupSymbols = Arrays.asList("[]", "()", "{}");
|
||||
|
||||
public final static Character POWER_10 = 'E';
|
||||
|
||||
public static final String IMAGINARY_NUMBER = "i";
|
||||
public static final String IMAGINARY_NUMBER_JSCL = "√(-1)";
|
||||
|
||||
public static final String PI = "π";
|
||||
public static final String E = "e";
|
||||
public static final String C = "c";
|
||||
public static final Double C_VALUE = 299792458d;
|
||||
public static final String G = "G";
|
||||
public static final Double G_VALUE = 6.6738480E-11;
|
||||
public static final String H_REDUCED = "h";
|
||||
public static final Double H_REDUCED_VALUE = 6.6260695729E-34 / ( 2 * Math.PI );
|
||||
public final static String NAN = "NaN";
|
||||
|
||||
public final static String INFINITY = "∞";
|
||||
public final static String INFINITY_JSCL = "Infinity";
|
||||
|
||||
|
||||
/**
|
||||
* Method determines mathematical entity type for text substring starting from ith index
|
||||
*
|
||||
*
|
||||
* @param text analyzed text
|
||||
* @param i index which points to start of substring
|
||||
* @param hexMode
|
||||
* @return math entity type of substring starting from ith index of specified text
|
||||
*/
|
||||
@NotNull
|
||||
public static Result getType(@NotNull String text, int i, boolean hexMode) {
|
||||
if (i < 0) {
|
||||
throw new IllegalArgumentException("I must be more or equals to 0.");
|
||||
} else if (i >= text.length() && i != 0) {
|
||||
throw new IllegalArgumentException("I must be less than size of text.");
|
||||
} else if (i == 0 && text.length() == 0) {
|
||||
return new Result(MathType.text, text);
|
||||
}
|
||||
|
||||
final StartsWithFinder startsWithFinder = new StartsWithFinder(text, i);
|
||||
|
||||
for (MathType mathType : getMathTypesByPriority()) {
|
||||
final String s = CollectionsUtils.find(mathType.getTokens(), startsWithFinder);
|
||||
if (s != null) {
|
||||
if ( s.length() == 1 ) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Result(mathType, s);
|
||||
}
|
||||
}
|
||||
|
||||
return new Result(MathType.text, text.substring(i));
|
||||
}
|
||||
|
||||
|
||||
private static List<MathType> mathTypesByPriority;
|
||||
|
||||
@NotNull
|
||||
private static List<MathType> getMathTypesByPriority() {
|
||||
if (mathTypesByPriority == null) {
|
||||
final List<MathType> result = CollectionsUtils.asList(MathType.values());
|
||||
|
||||
Collections.sort(result, new Comparator<MathType>() {
|
||||
@Override
|
||||
public int compare(MathType l, MathType r) {
|
||||
return l.priority.compareTo(r.priority);
|
||||
}
|
||||
});
|
||||
|
||||
mathTypesByPriority = result;
|
||||
}
|
||||
|
||||
return mathTypesByPriority;
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
|
||||
@NotNull
|
||||
private final MathType mathType;
|
||||
|
||||
@NotNull
|
||||
private final String match;
|
||||
|
||||
public Result(@NotNull MathType mathType, @NotNull String match) {
|
||||
this.mathType = mathType;
|
||||
|
||||
this.match = match;
|
||||
}
|
||||
|
||||
public int processToJscl(@NotNull StringBuilder result, int i) throws CalculatorParseException {
|
||||
return mathType.processToJscl(result, i, match);
|
||||
}
|
||||
|
||||
public int processFromJscl(@NotNull StringBuilder result, int i) {
|
||||
return mathType.processFromJscl(result, i, match);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getMatch() {
|
||||
return match;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MathType getMathType() {
|
||||
return mathType;
|
||||
}
|
||||
}
|
||||
|
||||
private static class EndsWithFinder implements JPredicate<String> {
|
||||
|
||||
private int i;
|
||||
|
||||
@NotNull
|
||||
private final CharSequence targetString;
|
||||
|
||||
private EndsWithFinder(@NotNull CharSequence targetString) {
|
||||
this.targetString = targetString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(@Nullable String s) {
|
||||
return targetString.subSequence(0, i).toString().endsWith(s);
|
||||
}
|
||||
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,323 @@
|
||||
/*
|
||||
* 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.function.IFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
import org.solovyev.android.calculator.MathPersistenceEntity;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:25 PM
|
||||
*/
|
||||
|
||||
@Root(name = "function")
|
||||
public class AFunction implements IFunction, MathPersistenceEntity, Serializable {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@Transient
|
||||
private Integer id;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Element(name = "body")
|
||||
@NotNull
|
||||
private String content;
|
||||
|
||||
@ElementList(type = String.class)
|
||||
@NotNull
|
||||
private List<String> parameterNames = new ArrayList<String>();
|
||||
|
||||
@Element
|
||||
private boolean system;
|
||||
|
||||
@Element(required = false)
|
||||
@NotNull
|
||||
private String description = "";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public AFunction() {
|
||||
}
|
||||
|
||||
public AFunction(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static AFunction fromIFunction(@NotNull IFunction function) {
|
||||
final AFunction result = new AFunction();
|
||||
|
||||
copy(result, function);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void copy(@NotNull MathEntity mathEntity) {
|
||||
if (mathEntity instanceof IFunction) {
|
||||
copy(this, (IFunction) mathEntity);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + mathEntity.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
private static void copy(@NotNull AFunction target,
|
||||
@NotNull IFunction source) {
|
||||
target.name = source.getName();
|
||||
target.content = source.getContent();
|
||||
target.description = StringUtils.getNotEmpty(source.getDescription(), "");
|
||||
target.system = source.isSystem();
|
||||
if (source.isIdDefined()) {
|
||||
target.id = source.getId();
|
||||
}
|
||||
target.parameterNames = new ArrayList<String>(source.getParameterNames());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJava() {
|
||||
return String.valueOf(this.content);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* GETTERS/SETTERS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdDefined() {
|
||||
return this.id != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(@NotNull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setContent(@NotNull String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<String> getParameterNames() {
|
||||
return parameterNames;
|
||||
}
|
||||
|
||||
public void setParameterNames(@NotNull List<String> parameterNames) {
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public static class Builder implements MathEntityBuilder<AFunction> {
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
private boolean system = false;
|
||||
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Nullable
|
||||
private Integer id;
|
||||
|
||||
@NotNull
|
||||
private List<String> parameterNames = Collections.emptyList();
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
public Builder(@NotNull IFunction function) {
|
||||
this.name = function.getName();
|
||||
this.value = function.getContent();
|
||||
this.system = function.isSystem();
|
||||
this.description = function.getDescription();
|
||||
this.id = function.getId();
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name,
|
||||
@NotNull String value,
|
||||
@NotNull List<String> parameterNames) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder setValue(@Nullable String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected Builder setSystem(boolean system) {
|
||||
this.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setParameterNames(@NotNull List<String> parameterNames) {
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder setDescription(@Nullable String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AFunction create() {
|
||||
final AFunction result;
|
||||
if (id != null) {
|
||||
result = new AFunction(id);
|
||||
} else {
|
||||
result = new AFunction();
|
||||
}
|
||||
|
||||
result.name = name;
|
||||
try {
|
||||
result.content = Locator.getInstance().getCalculator().prepareExpression(value).toString();
|
||||
} catch (CalculatorParseException e) {
|
||||
throw new CreationException(e);
|
||||
}
|
||||
result.system = system;
|
||||
result.description = StringUtils.getNotEmpty(description, "");
|
||||
result.parameterNames = new ArrayList<String>(parameterNames);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class CreationException extends RuntimeException implements Message {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorParseException message;
|
||||
|
||||
public CreationException(@NotNull CalculatorParseException cause) {
|
||||
super(cause);
|
||||
message = cause;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMessageCode() {
|
||||
return message.getMessageCode();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Object> getParameters() {
|
||||
return message.getParameters();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return message.getMessageType();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getLocalizedMessage() {
|
||||
return message.getLocalizedMessage();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getLocalizedMessage(@NotNull Locale locale) {
|
||||
return message.getLocalizedMessage(locale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:15 PM
|
||||
*/
|
||||
@Root
|
||||
public class Functions implements MathEntityPersistenceContainer<AFunction> {
|
||||
|
||||
@ElementList(type = AFunction.class)
|
||||
private List<AFunction> functions = new ArrayList<AFunction>();
|
||||
|
||||
public Functions() {
|
||||
}
|
||||
|
||||
public List<AFunction> getEntities() {
|
||||
return functions;
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 9:21 PM
|
||||
*/
|
||||
public interface MathEntityBuilder<T extends MathEntity> extends JBuilder<T> {
|
||||
|
||||
@NotNull
|
||||
public MathEntityBuilder<T> setName(@NotNull String name);
|
||||
|
||||
@NotNull
|
||||
public MathEntityBuilder<T> setDescription(@Nullable String description);
|
||||
|
||||
@NotNull
|
||||
public MathEntityBuilder<T> setValue(@Nullable String value);
|
||||
}
|
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
* 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.function.Constant;
|
||||
import jscl.math.function.ExtendedConstant;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/28/11
|
||||
* Time: 11:22 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class Var implements IConstant, MathPersistenceEntity {
|
||||
|
||||
@Transient
|
||||
private Integer id;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
@Element
|
||||
private boolean system;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Transient
|
||||
private Constant constant;
|
||||
|
||||
public static class Builder implements JBuilder<Var>, MathEntityBuilder<Var> {
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
private boolean system = false;
|
||||
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Nullable
|
||||
private Integer id;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
public Builder(@NotNull Var var) {
|
||||
this.name = var.name;
|
||||
this.value = var.value;
|
||||
this.system = var.system;
|
||||
this.description = var.description;
|
||||
this.id = var.id;
|
||||
}
|
||||
|
||||
public Builder(@NotNull IConstant iConstant) {
|
||||
this.name = iConstant.getName();
|
||||
|
||||
this.value = iConstant.getValue();
|
||||
|
||||
this.system = iConstant.isSystem();
|
||||
this.description = iConstant.getDescription();
|
||||
if (iConstant.isIdDefined()) {
|
||||
this.id = iConstant.getId();
|
||||
}
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name, @NotNull Double value) {
|
||||
this(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name, @Nullable String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public Builder setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder setValue(@Nullable String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected Builder setSystem(boolean system) {
|
||||
this.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder setDescription(@Nullable String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Var create() {
|
||||
final Var result;
|
||||
if (id != null) {
|
||||
result = new Var(id);
|
||||
} else {
|
||||
result = new Var();
|
||||
}
|
||||
|
||||
result.name = name;
|
||||
result.value = value;
|
||||
result.system = system;
|
||||
result.description = description;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private Var() {
|
||||
}
|
||||
|
||||
private Var(@NotNull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void copy(@NotNull MathEntity o) {
|
||||
if (o instanceof IConstant) {
|
||||
final IConstant that = ((IConstant) o);
|
||||
this.name = that.getName();
|
||||
this.value = that.getValue();
|
||||
this.description = that.getDescription();
|
||||
this.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Double getDoubleValue() {
|
||||
Double result = null;
|
||||
if (value != null) {
|
||||
try {
|
||||
result = Double.valueOf(value);
|
||||
} catch (NumberFormatException e) {
|
||||
// do nothing - string is not a double
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toJava() {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public boolean isSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdDefined() {
|
||||
return this.id != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(@NotNull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Constant getConstant() {
|
||||
if (constant == null) {
|
||||
constant = new Constant(this.name);
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefined() {
|
||||
return !StringUtils.isEmpty(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Var var = (Var) o;
|
||||
|
||||
if (!name.equals(var.name)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/29/11
|
||||
* Time: 5:19 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class Vars implements MathEntityPersistenceContainer<Var> {
|
||||
|
||||
@ElementList(type = Var.class)
|
||||
private List<Var> vars = new ArrayList<Var>();
|
||||
|
||||
public Vars() {
|
||||
}
|
||||
|
||||
public List<Var> getEntities() {
|
||||
return vars;
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.text;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/18/11
|
||||
* Time: 10:39 PM
|
||||
*/
|
||||
public enum DummyTextProcessor implements TextProcessor<String, Generic> {
|
||||
|
||||
instance;
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String process(@NotNull Generic s) throws CalculatorParseException {
|
||||
return s.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
package org.solovyev.android.calculator.text;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/20/11
|
||||
* Time: 2:59 PM
|
||||
*/
|
||||
public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Generic> {
|
||||
|
||||
public static final FromJsclSimplifyTextProcessor instance = new FromJsclSimplifyTextProcessor();
|
||||
|
||||
public FromJsclSimplifyTextProcessor() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String process(@NotNull Generic from) throws CalculatorParseException {
|
||||
return removeMultiplicationSigns(from.toString());
|
||||
}
|
||||
|
||||
public String process(@NotNull String s) {
|
||||
return removeMultiplicationSigns(s);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String removeMultiplicationSigns(String s) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
MathType.Result mathTypeBefore;
|
||||
MathType.Result mathType = null;
|
||||
MathType.Result mathTypeAfter = null;
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
mathTypeBefore = mathType;
|
||||
if (mathTypeAfter == null) {
|
||||
mathType = MathType.getType(s, i, false);
|
||||
} else {
|
||||
mathType = mathTypeAfter;
|
||||
}
|
||||
|
||||
char ch = s.charAt(i);
|
||||
if (ch == '*') {
|
||||
if (i + 1 < s.length()) {
|
||||
mathTypeAfter = MathType.getType(s, i + 1, false);
|
||||
} else {
|
||||
mathTypeAfter = null;
|
||||
}
|
||||
|
||||
if (needMultiplicationSign(mathTypeBefore == null ? null : mathTypeBefore.getMathType(), mathTypeAfter == null ? null : mathTypeAfter.getMathType())) {
|
||||
sb.append(Locator.getInstance().getEngine().getMultiplicationSign());
|
||||
}
|
||||
|
||||
} else {
|
||||
if (mathType.getMathType() == MathType.constant || mathType.getMathType() == MathType.function || mathType.getMathType() == MathType.operator) {
|
||||
sb.append(mathType.getMatch());
|
||||
i += mathType.getMatch().length() - 1;
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
mathTypeAfter = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private final List<MathType> mathTypes = Arrays.asList(MathType.function, MathType.constant);
|
||||
|
||||
private boolean needMultiplicationSign(@Nullable MathType mathTypeBefore, @Nullable MathType mathTypeAfter) {
|
||||
if (mathTypeBefore == null || mathTypeAfter == null) {
|
||||
return true;
|
||||
} else if (mathTypes.contains(mathTypeBefore) || mathTypes.contains(mathTypeAfter)) {
|
||||
return false;
|
||||
} else if ( mathTypeBefore == MathType.close_group_symbol ) {
|
||||
return false;
|
||||
} else if ( mathTypeAfter == MathType.open_group_symbol ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package org.solovyev.android.calculator.text;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/11
|
||||
* Time: 12:12 PM
|
||||
*/
|
||||
public interface TextProcessor<TO extends CharSequence, FROM> {
|
||||
|
||||
@NotNull
|
||||
TO process(@NotNull FROM from) throws CalculatorParseException;
|
||||
}
|
@@ -0,0 +1,96 @@
|
||||
package org.solovyev.android.calculator.units;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitConverter;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.math.units.UnitType;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:05
|
||||
*/
|
||||
public enum CalculatorNumeralBase implements UnitType<String> {
|
||||
|
||||
|
||||
bin(NumeralBase.bin),
|
||||
|
||||
oct(NumeralBase.oct),
|
||||
|
||||
dec(NumeralBase.dec),
|
||||
|
||||
hex(NumeralBase.hex);
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase numeralBase;
|
||||
|
||||
private CalculatorNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
this.numeralBase = numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public NumeralBase getNumeralBase() {
|
||||
return numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final CalculatorNumeralBase.Converter converter = new CalculatorNumeralBase.Converter();
|
||||
|
||||
@NotNull
|
||||
public static CalculatorNumeralBase.Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<String> getUnitValueClass() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Unit<String> createUnit(@NotNull String value) {
|
||||
return UnitImpl.newInstance(value, this);
|
||||
}
|
||||
|
||||
public static class Converter implements UnitConverter<String> {
|
||||
|
||||
private Converter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
||||
return CalculatorNumeralBase.class.isAssignableFrom(from.getClass()) && CalculatorNumeralBase.class.isAssignableFrom(to.getClass());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
|
||||
if (!isSupported(from.getUnitType(), toType)) {
|
||||
throw new IllegalArgumentException("Types are not supported!");
|
||||
}
|
||||
|
||||
final CalculatorNumeralBase fromTypeAndroid = (CalculatorNumeralBase) from.getUnitType();
|
||||
final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase;
|
||||
final NumeralBase toNumeralBase = ((CalculatorNumeralBase) toType).numeralBase;
|
||||
final String fromValue = (String) from.getValue();
|
||||
|
||||
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
||||
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), toType);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorNumeralBase valueOf(@NotNull NumeralBase nb) {
|
||||
for (CalculatorNumeralBase calculatorNumeralBase : values()) {
|
||||
if (calculatorNumeralBase.numeralBase == nb) {
|
||||
return calculatorNumeralBase;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(nb + " is not supported numeral base!");
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user