Conversion
This commit is contained in:
@@ -1,58 +1,68 @@
|
||||
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
|
||||
CalculatorEventDataId evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long sequenceId);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
||||
}
|
||||
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 Long sequenceId);
|
||||
}
|
||||
|
@@ -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,98 @@
|
||||
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 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;
|
||||
}
|
||||
}
|
@@ -1,148 +1,170 @@
|
||||
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 CalculatorEventData lastCalculatorEventData;
|
||||
|
||||
@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.lastCalculatorEventData = CalculatorEventDataImpl.newInstance(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 lastCalculatorEventData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(calculation_result, calculation_failed, calculation_cancelled)) {
|
||||
|
||||
if (calculatorEventData.isAfter(lastCalculatorEventData)) {
|
||||
lastCalculatorEventData = calculatorEventData;
|
||||
}
|
||||
|
||||
switch (calculatorEventType) {
|
||||
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 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());
|
||||
}
|
||||
}
|
||||
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 CalculatorEventData lastCalculatorEventData;
|
||||
|
||||
@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.lastCalculatorEventData = 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 lastCalculatorEventData;
|
||||
}
|
||||
|
||||
@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)) {
|
||||
|
||||
if (calculatorEventData.isAfter(lastCalculatorEventData)) {
|
||||
lastCalculatorEventData = calculatorEventData;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
@@ -1,67 +1,67 @@
|
||||
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 boolean isAfter(@NotNull CalculatorEventDataId that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
}
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,24 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:51
|
||||
*/
|
||||
public interface CalculatorEventData extends CalculatorEventDataId {
|
||||
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
boolean isAfter(@NotNull CalculatorEventData that);
|
||||
|
||||
boolean isSameSequence(@NotNull CalculatorEventData that);
|
||||
|
||||
boolean isAfterSequence(@NotNull CalculatorEventData that);
|
||||
}
|
||||
|
@@ -1,24 +0,0 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 18:18
|
||||
*/
|
||||
public interface CalculatorEventDataId {
|
||||
|
||||
// the higher id => the later event
|
||||
long getEventId();
|
||||
|
||||
// the higher id => the later event
|
||||
@NotNull
|
||||
Long getSequenceId();
|
||||
|
||||
boolean isAfter(@NotNull CalculatorEventDataId that);
|
||||
|
||||
boolean isSameSequence(@NotNull CalculatorEventDataId that);
|
||||
|
||||
boolean isAfterSequence(@NotNull CalculatorEventDataId that);
|
||||
}
|
@@ -1,75 +0,0 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 18:18
|
||||
*/
|
||||
class CalculatorEventDataIdImpl implements CalculatorEventDataId {
|
||||
|
||||
private static final long NO_SEQUENCE = -1L;
|
||||
|
||||
private final long eventId;
|
||||
|
||||
@NotNull
|
||||
private Long sequenceId = NO_SEQUENCE;
|
||||
|
||||
private CalculatorEventDataIdImpl(long id, @NotNull Long sequenceId) {
|
||||
this.eventId = id;
|
||||
this.sequenceId = sequenceId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventDataId newInstance(long id, @NotNull Long sequenceId) {
|
||||
return new CalculatorEventDataIdImpl(id, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return this.eventId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return this.sequenceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventDataId that) {
|
||||
return this.eventId > that.getEventId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventDataId that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId.equals(that.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventDataId 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 CalculatorEventDataIdImpl)) return false;
|
||||
|
||||
CalculatorEventDataIdImpl that = (CalculatorEventDataIdImpl) 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;
|
||||
}
|
||||
}
|
@@ -1,66 +1,75 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:54
|
||||
*/
|
||||
class CalculatorEventDataImpl implements CalculatorEventData {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventDataId calculatorEventDataId;
|
||||
|
||||
private CalculatorEventDataImpl(@NotNull CalculatorEventDataId calculatorEventDataId) {
|
||||
this.calculatorEventDataId = calculatorEventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEventData newInstance(@NotNull CalculatorEventDataId calculatorEventDataId) {
|
||||
return new CalculatorEventDataImpl(calculatorEventDataId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventDataId.getEventId();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventDataId.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventDataId.isAfter(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventDataId.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventDataId.isAfterSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CalculatorEventDataImpl)) return false;
|
||||
|
||||
CalculatorEventDataImpl that = (CalculatorEventDataImpl) o;
|
||||
|
||||
if (!calculatorEventDataId.equals(that.calculatorEventDataId)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return calculatorEventDataId.hashCode();
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 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 CalculatorEventDataImpl(long id, @NotNull Long sequenceId) {
|
||||
this.eventId = id;
|
||||
this.sequenceId = sequenceId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return this.eventId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return this.sequenceId;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
@@ -1,83 +1,90 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:40
|
||||
*/
|
||||
public enum CalculatorEventType {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* org.solovyev.android.calculator.CalculatorEvaluationEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
// @NotNull CalculatorEditorViewState
|
||||
manual_calculation_requested,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorInput
|
||||
calculation_started,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorOutput
|
||||
calculation_result,
|
||||
|
||||
calculation_cancelled,
|
||||
|
||||
calculation_finished,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorFailure
|
||||
calculation_failed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
conversion_started,
|
||||
|
||||
// @NotNull String conversion result
|
||||
conversion_finished,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData
|
||||
editor_state_changed,
|
||||
|
||||
// @NotNull CalculatorDisplayChangeEventData
|
||||
display_state_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
engine_preferences_changed;
|
||||
|
||||
public boolean isOfType(@NotNull CalculatorEventType... types) {
|
||||
for (CalculatorEventType type : types) {
|
||||
if ( this == type ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
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.CalculatorInput
|
||||
calculation_started,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorOutput
|
||||
calculation_result,
|
||||
|
||||
calculation_cancelled,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorFailure
|
||||
calculation_failed,
|
||||
|
||||
calculation_finished,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* 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,
|
||||
|
||||
// @NotNull CalculatorDisplayChangeEventData
|
||||
display_state_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
engine_preferences_changed;
|
||||
|
||||
public boolean isOfType(@NotNull CalculatorEventType... types) {
|
||||
for (CalculatorEventType type : types) {
|
||||
if ( this == type ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,405 +1,433 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.AbstractJsclArithmeticException;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.NumeralBaseException;
|
||||
import jscl.math.Generic;
|
||||
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.text.TextProcessor;
|
||||
import org.solovyev.common.history.HistoryAction;
|
||||
import org.solovyev.common.msg.MessageRegistry;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
import org.solovyev.math.units.UnitConverter;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.math.units.UnitType;
|
||||
|
||||
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 {
|
||||
|
||||
@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 threadPoolExecutor = Executors.newFixedThreadPool(10);
|
||||
|
||||
public CalculatorImpl() {
|
||||
this.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String doConversion(@NotNull UnitConverter<String> converter,
|
||||
@Nullable String from,
|
||||
@NotNull UnitType<String> fromUnitType,
|
||||
@NotNull UnitType<String> toUnitType) throws ConversionException {
|
||||
final String result;
|
||||
|
||||
if (StringUtils.isEmpty(from)) {
|
||||
result = "";
|
||||
} else {
|
||||
|
||||
String to = null;
|
||||
try {
|
||||
if (converter.isSupported(fromUnitType, toUnitType)) {
|
||||
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
|
||||
result = to;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventDataId nextEventDataId() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataIdImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventDataId nextEventDataId(@NotNull Long sequenceId) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataIdImpl.newInstance(eventId, sequenceId);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText());
|
||||
}
|
||||
|
||||
@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();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.simplify, viewState.getText());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId evaluate(@NotNull final JsclOperation operation,
|
||||
@NotNull final String expression) {
|
||||
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId();
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId(sequenceId);
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId convert(@NotNull final Generic generic,
|
||||
@NotNull final NumeralBase to) {
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId();
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Long sequenceId = eventDataId.getSequenceId();
|
||||
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId), CalculatorEventType.conversion_started, null);
|
||||
|
||||
final NumeralBase from = CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// todo serso: continue
|
||||
//doConversion(AndroidNumeralBase.getConverter(), fromString, AndroidNumeralBase.valueOf(fromString), AndroidNumeralBase.valueOf(to));
|
||||
} else {
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId), CalculatorEventType.conversion_finished, generic.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId();
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fireCalculatorEvent(CalculatorEventDataImpl.newInstance(eventDataId), calculatorEventType, data);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Long sequenceId) {
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId(sequenceId);
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fireCalculatorEvent(CalculatorEventDataImpl.newInstance(eventDataId), calculatorEventType, data);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
CalculatorLocatorImpl.getInstance().getEngine().init();
|
||||
CalculatorLocatorImpl.getInstance().getHistory().load();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData newConversionEventData(@NotNull Long sequenceId) {
|
||||
return CalculatorEventDataImpl.newInstance(nextEventDataId(sequenceId));
|
||||
}
|
||||
|
||||
private void evaluate(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr) {
|
||||
|
||||
PreparedExpression preparedExpression = null;
|
||||
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_started, new CalculatorInputImpl(expression, operation));
|
||||
|
||||
try {
|
||||
|
||||
expression = expression.trim();
|
||||
|
||||
if (StringUtils.isEmpty(expression)) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
||||
} else {
|
||||
preparedExpression = preprocessor.process(expression);
|
||||
|
||||
final String jsclExpression = preparedExpression.toString();
|
||||
|
||||
try {
|
||||
|
||||
final Generic result = operation.evaluateGeneric(jsclExpression, CalculatorLocatorImpl.getInstance().getEngine().getMathEngine());
|
||||
|
||||
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||
result.toString();
|
||||
|
||||
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);
|
||||
} finally {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_finished, null);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long calculationId) {
|
||||
return new CalculatorEvaluationEventDataImpl(CalculatorEventDataImpl.newInstance(nextEventDataId(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);
|
||||
}
|
||||
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(evalException));
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* 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 CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
calculatorEventContainer.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
|
||||
switch (calculatorEventType) {
|
||||
case editor_state_changed:
|
||||
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
|
||||
|
||||
final String newText = changeEventData.getNewState().getText();
|
||||
final String oldText = changeEventData.getOldState().getText();
|
||||
|
||||
if (!newText.equals(oldText)) {
|
||||
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
|
||||
}
|
||||
break;
|
||||
case engine_preferences_changed:
|
||||
evaluate(calculatorEventData.getSequenceId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = CalculatorLocatorImpl.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
|
||||
private CalculatorEditor getEditor() {
|
||||
return CalculatorLocatorImpl.getInstance().getEditor();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return CalculatorHistoryState.newInstance(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplay getDisplay() {
|
||||
return CalculatorLocatorImpl.getInstance().getDisplay();
|
||||
}
|
||||
|
||||
public static final class ConversionException extends Exception {
|
||||
private ConversionException() {
|
||||
}
|
||||
|
||||
private ConversionException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.AbstractJsclArithmeticException;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.NumeralBaseException;
|
||||
import jscl.math.Generic;
|
||||
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.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.common.history.HistoryAction;
|
||||
import org.solovyev.common.msg.MessageRegistry;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
import org.solovyev.math.units.*;
|
||||
|
||||
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 {
|
||||
|
||||
@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 threadPoolExecutor = Executors.newFixedThreadPool(10);
|
||||
|
||||
public CalculatorImpl() {
|
||||
this.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
|
||||
@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();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText());
|
||||
}
|
||||
|
||||
@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();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.simplify, viewState.getText());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation,
|
||||
@NotNull final String expression) {
|
||||
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
threadPoolExecutor.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);
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
CalculatorLocatorImpl.getInstance().getEngine().init();
|
||||
CalculatorLocatorImpl.getInstance().getHistory().load();
|
||||
}
|
||||
|
||||
@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) {
|
||||
|
||||
PreparedExpression preparedExpression = null;
|
||||
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_started, new CalculatorInputImpl(expression, operation));
|
||||
|
||||
try {
|
||||
|
||||
expression = expression.trim();
|
||||
|
||||
if (StringUtils.isEmpty(expression)) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
||||
} else {
|
||||
preparedExpression = preprocessor.process(expression);
|
||||
|
||||
final String jsclExpression = preparedExpression.toString();
|
||||
|
||||
try {
|
||||
|
||||
final Generic result = operation.evaluateGeneric(jsclExpression, CalculatorLocatorImpl.getInstance().getEngine().getMathEngine());
|
||||
|
||||
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||
result.toString();
|
||||
|
||||
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);
|
||||
} finally {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_finished, null);
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
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 = CalculatorLocatorImpl.getInstance().getDisplay().getViewState();
|
||||
final NumeralBase from = CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase();
|
||||
|
||||
threadPoolExecutor.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 CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
calculatorEventContainer.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
final CalculatorEventData eventData = nextEventData();
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
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);
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
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:
|
||||
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
|
||||
|
||||
final String newText = changeEventData.getNewState().getText();
|
||||
final String oldText = changeEventData.getOldState().getText();
|
||||
|
||||
if (!newText.equals(oldText)) {
|
||||
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
|
||||
}
|
||||
break;
|
||||
case engine_preferences_changed:
|
||||
evaluate(calculatorEventData.getSequenceId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = CalculatorLocatorImpl.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 CalculatorLocatorImpl.getInstance().getEditor();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplay getDisplay() {
|
||||
return CalculatorLocatorImpl.getInstance().getDisplay();
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +1,22 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 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 CalculatorEventDataId createFirstEventDataId() {
|
||||
return CalculatorEventDataIdImpl.newInstance(FIRST_ID, FIRST_ID);
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -1,216 +1,216 @@
|
||||
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.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.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed;
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.manual_calculation_requested;
|
||||
|
||||
/**
|
||||
* 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 volatile CalculatorEventDataId lastEventDataId = 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);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
synchronized (history) {
|
||||
return history.getStates();
|
||||
}
|
||||
}
|
||||
|
||||
@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)) {
|
||||
|
||||
if (calculatorEventData.isAfter(this.lastEventDataId)) {
|
||||
final boolean sameSequence = calculatorEventData.isSameSequence(this.lastEventDataId);
|
||||
|
||||
if (sameSequence || calculatorEventData.isAfterSequence(this.lastEventDataId)) {
|
||||
this.lastEventDataId = calculatorEventData;
|
||||
|
||||
switch (calculatorEventType) {
|
||||
case manual_calculation_requested:
|
||||
lastEditorViewState = (CalculatorEditorViewState) data;
|
||||
break;
|
||||
case editor_state_changed:
|
||||
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
|
||||
lastEditorViewState = editorChangeData.getNewState();
|
||||
break;
|
||||
case display_state_changed:
|
||||
if (sameSequence) {
|
||||
if (lastEditorViewState != null) {
|
||||
final CalculatorEditorViewState editorViewState = lastEditorViewState;
|
||||
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
|
||||
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewState();
|
||||
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
|
||||
}
|
||||
} else {
|
||||
lastEditorViewState = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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.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.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed;
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.manual_calculation_requested;
|
||||
|
||||
/**
|
||||
* 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 volatile CalculatorEventData lastEventDataId = 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);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
synchronized (history) {
|
||||
return history.getStates();
|
||||
}
|
||||
}
|
||||
|
||||
@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)) {
|
||||
|
||||
if (calculatorEventData.isAfter(this.lastEventDataId)) {
|
||||
final boolean sameSequence = calculatorEventData.isSameSequence(this.lastEventDataId);
|
||||
|
||||
if (sameSequence || calculatorEventData.isAfterSequence(this.lastEventDataId)) {
|
||||
this.lastEventDataId = calculatorEventData;
|
||||
|
||||
switch (calculatorEventType) {
|
||||
case manual_calculation_requested:
|
||||
lastEditorViewState = (CalculatorEditorViewState) data;
|
||||
break;
|
||||
case editor_state_changed:
|
||||
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
|
||||
lastEditorViewState = editorChangeData.getNewState();
|
||||
break;
|
||||
case display_state_changed:
|
||||
if (sameSequence) {
|
||||
if (lastEditorViewState != null) {
|
||||
final CalculatorEditorViewState editorViewState = lastEditorViewState;
|
||||
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
|
||||
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewState();
|
||||
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
|
||||
}
|
||||
} else {
|
||||
lastEditorViewState = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,99 @@
|
||||
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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package org.solovyev.math.units;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:01
|
||||
*/
|
||||
public final class ConversionException extends Exception {
|
||||
public ConversionException() {
|
||||
}
|
||||
|
||||
public ConversionException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package org.solovyev.math.units;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 24.09.12
|
||||
* Time: 16:01
|
||||
*/
|
||||
public class ConversionUtils {
|
||||
@NotNull
|
||||
public static String doConversion(@NotNull UnitConverter<String> converter,
|
||||
@Nullable String from,
|
||||
@NotNull UnitType<String> fromUnitType,
|
||||
@NotNull UnitType<String> toUnitType) throws ConversionException {
|
||||
final String result;
|
||||
|
||||
if (StringUtils.isEmpty(from)) {
|
||||
result = "";
|
||||
} else {
|
||||
|
||||
String to = null;
|
||||
try {
|
||||
if (converter.isSupported(fromUnitType, toUnitType)) {
|
||||
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
|
||||
result = to;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user