Calculator display changes
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
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.jscl.JsclOperation;
|
||||
@@ -22,4 +24,7 @@ public interface Calculator extends CalculatorEventContainer {
|
||||
CalculatorEventDataId evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
}
|
||||
|
@@ -47,8 +47,8 @@ public class CalculatorEvaluationEventDataImpl implements CalculatorEvaluationEv
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Long getCalculationId() {
|
||||
return calculatorEventData.getCalculationId();
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -13,8 +13,9 @@ public interface CalculatorEventDataId {
|
||||
// the higher id => the later event
|
||||
long getEventId();
|
||||
|
||||
// the higher id => the later event
|
||||
@Nullable
|
||||
Long getCalculationId();
|
||||
Long getSequenceId();
|
||||
|
||||
boolean isAfter(@NotNull CalculatorEventDataId calculatorEventDataId);
|
||||
}
|
||||
|
@@ -13,18 +13,16 @@ class CalculatorEventDataIdImpl implements CalculatorEventDataId {
|
||||
private final long eventId;
|
||||
|
||||
@Nullable
|
||||
private final Long calculationId;
|
||||
private final Long sequenceId;
|
||||
|
||||
private CalculatorEventDataIdImpl(long id,
|
||||
@Nullable Long calculationId) {
|
||||
private CalculatorEventDataIdImpl(long id, @Nullable Long sequenceId) {
|
||||
this.eventId = id;
|
||||
this.calculationId = calculationId;
|
||||
this.sequenceId = sequenceId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventDataId newInstance(long id,
|
||||
@Nullable Long calculationId) {
|
||||
return new CalculatorEventDataIdImpl(id, calculationId);
|
||||
static CalculatorEventDataId newInstance(long id, @Nullable Long sequenceId) {
|
||||
return new CalculatorEventDataIdImpl(id, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -34,8 +32,8 @@ class CalculatorEventDataIdImpl implements CalculatorEventDataId {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Long getCalculationId() {
|
||||
return this.calculationId;
|
||||
public Long getSequenceId() {
|
||||
return this.sequenceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,7 +49,7 @@ class CalculatorEventDataIdImpl implements CalculatorEventDataId {
|
||||
CalculatorEventDataIdImpl that = (CalculatorEventDataIdImpl) o;
|
||||
|
||||
if (eventId != that.eventId) return false;
|
||||
if (calculationId != null ? !calculationId.equals(that.calculationId) : that.calculationId != null)
|
||||
if (sequenceId != null ? !sequenceId.equals(that.sequenceId) : that.sequenceId != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -60,7 +58,7 @@ class CalculatorEventDataIdImpl implements CalculatorEventDataId {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (int) (eventId ^ (eventId >>> 32));
|
||||
result = 31 * result + (calculationId != null ? calculationId.hashCode() : 0);
|
||||
result = 31 * result + (sequenceId != null ? sequenceId.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -29,8 +29,8 @@ class CalculatorEventDataImpl implements CalculatorEventData {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Long getCalculationId() {
|
||||
return calculatorEventDataId.getCalculationId();
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventDataId.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -28,7 +28,19 @@ public enum CalculatorEventType {
|
||||
calculation_finished,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorFailure
|
||||
calculation_failed;
|
||||
calculation_failed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
conversion_started,
|
||||
|
||||
// @NotNull String conversion result
|
||||
conversion_finished;
|
||||
|
||||
public boolean isOfType(@NotNull CalculatorEventType... types) {
|
||||
for (CalculatorEventType type : types) {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.AbstractJsclArithmeticException;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.NumeralBaseException;
|
||||
import jscl.math.Generic;
|
||||
import jscl.text.ParseInterruptedException;
|
||||
@@ -10,6 +11,10 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
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;
|
||||
@@ -43,6 +48,32 @@ public class CalculatorImpl implements Calculator {
|
||||
public CalculatorImpl() {
|
||||
}
|
||||
|
||||
@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 nextCalculatorEventDataId() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
@@ -50,9 +81,9 @@ public class CalculatorImpl implements Calculator {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventDataId nextEventDataId(@NotNull Long calculationId) {
|
||||
private CalculatorEventDataId nextEventDataId(@NotNull Long sequenceId) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataIdImpl.newInstance(eventId, calculationId);
|
||||
return CalculatorEventDataIdImpl.newInstance(eventId, sequenceId);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -86,14 +117,56 @@ public class CalculatorImpl implements Calculator {
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getCalculationId(), operation, expression, mr);
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, mr);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
private void evaluate(@NotNull Long calculationId,
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId convert(@NotNull final Generic generic,
|
||||
@NotNull final NumeralBase to) {
|
||||
final CalculatorEventDataId eventDataId = nextCalculatorEventDataId();
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Long sequenceId = eventDataId.getSequenceId();
|
||||
assert sequenceId != null;
|
||||
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId), CalculatorEventType.conversion_started, null);
|
||||
|
||||
final NumeralBase from = CalculatorLocatorImpl.getInstance().getCalculatorEngine().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
|
||||
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) {
|
||||
@@ -101,7 +174,7 @@ public class CalculatorImpl implements Calculator {
|
||||
|
||||
PreparedExpression preparedExpression = null;
|
||||
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_started, new CalculatorInputImpl(expression, operation));
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_started, new CalculatorInputImpl(expression, operation));
|
||||
|
||||
try {
|
||||
preparedExpression = preprocessor.process(expression);
|
||||
@@ -116,27 +189,27 @@ public class CalculatorImpl implements Calculator {
|
||||
result.toString();
|
||||
|
||||
final CalculatorOutputImpl data = new CalculatorOutputImpl(operation.getFromProcessor().process(result), operation, result);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_result, data);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
|
||||
|
||||
} catch (AbstractJsclArithmeticException e) {
|
||||
handleException(calculationId, operation, expression, mr, new CalculatorEvalException(e, e, jsclExpression));
|
||||
handleException(sequenceId, operation, expression, mr, new CalculatorEvalException(e, e, jsclExpression));
|
||||
}
|
||||
|
||||
} catch (ArithmeticException e) {
|
||||
handleException(calculationId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
|
||||
} catch (StackOverflowError e) {
|
||||
handleException(calculationId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
|
||||
} catch (jscl.text.ParseException e) {
|
||||
handleException(calculationId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
|
||||
} catch (ParseInterruptedException e) {
|
||||
|
||||
// do nothing - we ourselves interrupt the calculations
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_cancelled, null);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
|
||||
|
||||
} catch (CalculatorParseException e) {
|
||||
handleException(calculationId, operation, expression, mr, preparedExpression, e);
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, e);
|
||||
} finally {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_finished, null);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_finished, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,4 +279,13 @@ public class CalculatorImpl implements Calculator {
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
|
||||
public static final class ConversionException extends Exception {
|
||||
private ConversionException() {
|
||||
}
|
||||
|
||||
private ConversionException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user