Conversion
This commit is contained in:
parent
b2fe1d43c9
commit
4b08fa133e
@ -32,16 +32,26 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
|
||||
void simplify();
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId evaluate(@NotNull JsclOperation operation,
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId evaluate(@NotNull JsclOperation operation,
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long sequenceId);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
@ -51,8 +61,8 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
||||
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;
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
|
||||
public CalculatorDisplayImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.lastCalculatorEventData = CalculatorEventDataImpl.newInstance(CalculatorUtils.createFirstEventDataId());
|
||||
this.lastCalculatorEventData = CalculatorUtils.createFirstEventDataId();
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@ -95,15 +95,21 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(calculation_result, calculation_failed, calculation_cancelled)) {
|
||||
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);
|
||||
processCalculationResult((CalculatorEvaluationEventData) calculatorEventData, (CalculatorOutput) data);
|
||||
break;
|
||||
case calculation_cancelled:
|
||||
processCalculationCancelled((CalculatorEvaluationEventData)calculatorEventData);
|
||||
@ -116,6 +122,12 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@ -145,4 +157,14 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
* Date: 9/20/12
|
||||
* Time: 10:00 PM
|
||||
*/
|
||||
public interface CalculatorEvaluationEventData extends CalculatorEventData{
|
||||
public interface CalculatorEvaluationEventData extends CalculatorEventData {
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
@ -51,17 +51,17 @@ public class CalculatorEvaluationEventDataImpl implements CalculatorEvaluationEv
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventDataId that) {
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventDataId that) {
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventDataId that) {
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,24 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:51
|
||||
* Time: 18:18
|
||||
*/
|
||||
public interface CalculatorEventData extends CalculatorEventDataId {
|
||||
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;
|
||||
}
|
||||
}
|
@ -5,46 +5,51 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:54
|
||||
* Time: 18:18
|
||||
*/
|
||||
class CalculatorEventDataImpl implements CalculatorEventData {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventDataId calculatorEventDataId;
|
||||
private static final long NO_SEQUENCE = -1L;
|
||||
|
||||
private CalculatorEventDataImpl(@NotNull CalculatorEventDataId calculatorEventDataId) {
|
||||
this.calculatorEventDataId = calculatorEventDataId;
|
||||
private final long eventId;
|
||||
|
||||
@NotNull
|
||||
private Long sequenceId = NO_SEQUENCE;
|
||||
|
||||
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId) {
|
||||
this.eventId = id;
|
||||
this.sequenceId = sequenceId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEventData newInstance(@NotNull CalculatorEventDataId calculatorEventDataId) {
|
||||
return new CalculatorEventDataImpl(calculatorEventDataId);
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventDataId.getEventId();
|
||||
return this.eventId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventDataId.getSequenceId();
|
||||
return this.sequenceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventDataId.isAfter(that);
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return this.eventId > that.getEventId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventDataId.isSameSequence(that);
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId.equals(that.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventDataId that) {
|
||||
return this.calculatorEventDataId.isAfterSequence(that);
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId > that.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,13 +59,17 @@ class CalculatorEventDataImpl implements CalculatorEventData {
|
||||
|
||||
CalculatorEventDataImpl that = (CalculatorEventDataImpl) o;
|
||||
|
||||
if (!calculatorEventDataId.equals(that.calculatorEventDataId)) return false;
|
||||
if (eventId != that.eventId) return false;
|
||||
if (!sequenceId.equals(that.sequenceId))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return calculatorEventDataId.hashCode();
|
||||
int result = (int) (eventId ^ (eventId >>> 32));
|
||||
result = 31 * result + (sequenceId.hashCode());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ public enum CalculatorEventType {
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
* org.solovyev.android.calculator.CalculatorEvaluationEventData
|
||||
*
|
||||
**********************************************************************
|
||||
@ -29,21 +30,27 @@ public enum CalculatorEventType {
|
||||
|
||||
calculation_cancelled,
|
||||
|
||||
calculation_finished,
|
||||
|
||||
// @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,
|
||||
|
||||
/*
|
||||
|
@ -11,13 +11,12 @@ 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.UnitConverter;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.math.units.UnitType;
|
||||
import org.solovyev.math.units.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
@ -48,41 +47,15 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
}
|
||||
|
||||
@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;
|
||||
private CalculatorEventData nextEventData() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventDataId nextEventDataId() {
|
||||
private CalculatorEventData nextEventData(@NotNull Long sequenceId) {
|
||||
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);
|
||||
return CalculatorEventDataImpl.newInstance(eventId, sequenceId);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -116,10 +89,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId evaluate(@NotNull final JsclOperation operation,
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation,
|
||||
@NotNull final String expression) {
|
||||
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId();
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
@ -133,8 +106,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventDataId evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
|
||||
final CalculatorEventDataId eventDataId = nextEventDataId(sequenceId);
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventDataId = nextEventData(sequenceId);
|
||||
|
||||
threadPoolExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
@ -146,72 +119,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
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();
|
||||
@ -219,8 +126,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData newConversionEventData(@NotNull Long sequenceId) {
|
||||
return CalculatorEventDataImpl.newInstance(nextEventDataId(sequenceId));
|
||||
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,
|
||||
@ -280,7 +191,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long calculationId) {
|
||||
return new CalculatorEvaluationEventDataImpl(CalculatorEventDataImpl.newInstance(nextEventDataId(calculationId)), operation, expression);
|
||||
return new CalculatorEvaluationEventDataImpl(nextEventData(calculationId), operation, expression);
|
||||
}
|
||||
|
||||
private void handleException(@NotNull Long sequenceId,
|
||||
@ -314,6 +225,78 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
@ -342,6 +325,44 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
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) {
|
||||
|
||||
@ -362,6 +383,14 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = CalculatorLocatorImpl.getInstance().getHistory();
|
||||
@ -378,28 +407,27 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor getEditor() {
|
||||
return CalculatorLocatorImpl.getInstance().getEditor();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
public static final class ConversionException extends Exception {
|
||||
private ConversionException() {
|
||||
}
|
||||
|
||||
private ConversionException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public final class CalculatorUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEventDataId createFirstEventDataId() {
|
||||
return CalculatorEventDataIdImpl.newInstance(FIRST_ID, FIRST_ID);
|
||||
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;
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
|
||||
|
||||
@NotNull
|
||||
private volatile CalculatorEventDataId lastEventDataId = CalculatorUtils.createFirstEventDataId();
|
||||
private volatile CalculatorEventData lastEventDataId = CalculatorUtils.createFirstEventDataId();
|
||||
|
||||
@Nullable
|
||||
private volatile CalculatorEditorViewState lastEditorViewState;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -44,31 +44,36 @@ public class AndroidCalculator implements Calculator {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId evaluate(@NotNull JsclOperation operation, @NotNull String expression) {
|
||||
public CalculatorEventData evaluate(@NotNull JsclOperation operation, @NotNull String expression) {
|
||||
return calculator.evaluate(operation, expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId evaluate(@NotNull JsclOperation operation, @NotNull String expression, @NotNull Long sequenceId) {
|
||||
public CalculatorEventData evaluate(@NotNull JsclOperation operation, @NotNull String expression, @NotNull Long sequenceId) {
|
||||
return calculator.evaluate(operation, expression, sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to) {
|
||||
return calculator.isConversionPossible(generic, from, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to) {
|
||||
public CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to) {
|
||||
return calculator.convert(generic, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
return calculator.fireCalculatorEvent(calculatorEventType, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) {
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) {
|
||||
return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId);
|
||||
}
|
||||
|
||||
|
@ -3,14 +3,10 @@ package org.solovyev.android.calculator;
|
||||
import android.app.Activity;
|
||||
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 org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
import org.solovyev.android.view.drag.DragDirection;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -20,9 +16,9 @@ import java.util.List;
|
||||
* Date: 4/21/12
|
||||
* Time: 8:00 PM
|
||||
*/
|
||||
public enum AndroidNumeralBase implements UnitType<String> {
|
||||
public enum AndroidNumeralBase {
|
||||
|
||||
bin(NumeralBase.bin) {
|
||||
bin(CalculatorNumeralBase.bin) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
@ -30,7 +26,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
}
|
||||
},
|
||||
|
||||
oct(NumeralBase.oct) {
|
||||
oct(CalculatorNumeralBase.oct) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
@ -40,7 +36,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
}
|
||||
},
|
||||
|
||||
dec(NumeralBase.dec) {
|
||||
dec(CalculatorNumeralBase.dec) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
@ -50,7 +46,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
}
|
||||
},
|
||||
|
||||
hex(NumeralBase.hex) {
|
||||
hex(CalculatorNumeralBase.hex) {
|
||||
|
||||
@NotNull
|
||||
private List<Integer> specialHexButtonIds = Arrays.asList(R.id.oneDigitButton, R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton);
|
||||
@ -72,15 +68,10 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase numeralBase;
|
||||
private final CalculatorNumeralBase calculatorNumeralBase;
|
||||
|
||||
private AndroidNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
this.numeralBase = numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Unit<String> createUnit(@NotNull String value) {
|
||||
return UnitImpl.newInstance(value, this);
|
||||
private AndroidNumeralBase(@NotNull CalculatorNumeralBase calculatorNumeralBase) {
|
||||
this.calculatorNumeralBase = calculatorNumeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@ -101,54 +92,13 @@ public enum AndroidNumeralBase implements UnitType<String> {
|
||||
|
||||
@NotNull
|
||||
public NumeralBase getNumeralBase() {
|
||||
return numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<String> getUnitValueClass() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final Converter converter = new Converter();
|
||||
|
||||
@NotNull
|
||||
public static Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public static class Converter implements UnitConverter<String> {
|
||||
|
||||
private Converter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
||||
return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.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 AndroidNumeralBase fromTypeAndroid = (AndroidNumeralBase) from.getUnitType();
|
||||
final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase;
|
||||
final NumeralBase toNumeralBase = ((AndroidNumeralBase) toType).numeralBase;
|
||||
final String fromValue = (String) from.getValue();
|
||||
|
||||
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
||||
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (AndroidNumeralBase) toType);
|
||||
}
|
||||
return calculatorNumeralBase.getNumeralBase();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) {
|
||||
for (AndroidNumeralBase androidNumeralBase : values()) {
|
||||
if (androidNumeralBase.numeralBase == nb) {
|
||||
if (androidNumeralBase.calculatorNumeralBase.getNumeralBase() == nb) {
|
||||
return androidNumeralBase;
|
||||
}
|
||||
}
|
||||
|
@ -30,10 +30,11 @@ enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
|
||||
|
||||
if (operation == JsclOperation.numeric) {
|
||||
if (generic.getConstants().isEmpty()) {
|
||||
convert(generic);
|
||||
|
||||
// conversion possible => return true
|
||||
result = true;
|
||||
final NumeralBase fromNumeralBase = CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase();
|
||||
if (fromNumeralBase != toNumeralBase) {
|
||||
result = CalculatorLocatorImpl.getInstance().getCalculator().isConversionPossible(generic, fromNumeralBase, this.toNumeralBase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,16 +43,10 @@ enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
|
||||
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) {
|
||||
final NumeralBase fromNumeralBase = CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase();
|
||||
final Generic result = data.getResult();
|
||||
|
||||
final Generic lastResult = data.getResult();
|
||||
|
||||
if (lastResult != null) {
|
||||
convert(lastResult);
|
||||
if (result != null) {
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().convert(result, this.toNumeralBase);
|
||||
}
|
||||
}
|
||||
|
||||
private void convert(@NotNull Generic generic) {
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().convert(generic, this.toNumeralBase);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import android.view.WindowManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitImpl;
|
||||
import org.solovyev.common.MutableObject;
|
||||
@ -30,22 +31,22 @@ public class NumeralBaseConverterDialog {
|
||||
|
||||
public void show(@NotNull Context context) {
|
||||
final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
|
||||
b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||
b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||
b.setFromUnitTypes(Arrays.asList(CalculatorNumeralBase.values()));
|
||||
b.setToUnitTypes(Arrays.asList(CalculatorNumeralBase.values()));
|
||||
|
||||
if (!StringUtils.isEmpty(initialFromValue)) {
|
||||
String value = initialFromValue;
|
||||
try {
|
||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
||||
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
} catch (CalculatorParseException e) {
|
||||
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
}
|
||||
} else {
|
||||
b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance("", CalculatorNumeralBase.valueOf(CalculatorLocatorImpl.getInstance().getEngine().getNumeralBase())));
|
||||
}
|
||||
|
||||
b.setConverter(AndroidNumeralBase.getConverter());
|
||||
b.setConverter(CalculatorNumeralBase.getConverter());
|
||||
|
||||
final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
|
||||
b.setOkButtonOnClickListener(new View.OnClickListener() {
|
||||
|
@ -11,10 +11,7 @@ import android.widget.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.CalculatorImpl;
|
||||
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 org.solovyev.math.units.*;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.view.ViewBuilder;
|
||||
import org.solovyev.android.view.ViewFromLayoutBuilder;
|
||||
@ -173,8 +170,8 @@ public class UnitConverterViewBuilder implements ViewBuilder<View> {
|
||||
|
||||
final String from = fromEditText.getText().toString();
|
||||
try {
|
||||
toEditText.setText(CalculatorImpl.doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
|
||||
} catch (CalculatorImpl.ConversionException e) {
|
||||
toEditText.setText(ConversionUtils.doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
|
||||
} catch (ConversionException e) {
|
||||
toEditText.setText(context.getString(R.string.c_error));
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package org.solovyev.android;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.AndroidNumeralBase;
|
||||
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitConverter;
|
||||
|
||||
@ -18,32 +18,32 @@ import java.util.Random;
|
||||
public class AndroidNumeralBaseTest {
|
||||
|
||||
@NotNull
|
||||
private final UnitConverter c = AndroidNumeralBase.getConverter();
|
||||
private final UnitConverter c = CalculatorNumeralBase.getConverter();
|
||||
|
||||
@Test
|
||||
public void testIsSupported() throws Exception {
|
||||
Assert.assertTrue(c.isSupported(AndroidNumeralBase.bin, AndroidNumeralBase.dec));
|
||||
Assert.assertTrue(c.isSupported(CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromDec() throws Exception {
|
||||
|
||||
Assert.assertEquals("101", c.convert(AndroidNumeralBase.dec.createUnit("5"), AndroidNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("1", c.convert(AndroidNumeralBase.dec.createUnit("1"), AndroidNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("1111100111", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("101", c.convert(CalculatorNumeralBase.dec.createUnit("5"), CalculatorNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("1", c.convert(CalculatorNumeralBase.dec.createUnit("1"), CalculatorNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("1111100111", c.convert(CalculatorNumeralBase.dec.createUnit("999"), CalculatorNumeralBase.bin).getValue());
|
||||
|
||||
Assert.assertEquals("A23", c.convert(AndroidNumeralBase.dec.createUnit("2595"), AndroidNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("AEE", c.convert(AndroidNumeralBase.dec.createUnit("2798"), AndroidNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("15", c.convert(AndroidNumeralBase.dec.createUnit("21"), AndroidNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("3E7", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("A23", c.convert(CalculatorNumeralBase.dec.createUnit("2595"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("AEE", c.convert(CalculatorNumeralBase.dec.createUnit("2798"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("21"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("3E7", c.convert(CalculatorNumeralBase.dec.createUnit("999"), CalculatorNumeralBase.hex).getValue());
|
||||
|
||||
Assert.assertEquals("76", c.convert(AndroidNumeralBase.dec.createUnit("62"), AndroidNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("12", c.convert(AndroidNumeralBase.dec.createUnit("10"), AndroidNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("15", c.convert(AndroidNumeralBase.dec.createUnit("13"), AndroidNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("10445", c.convert(AndroidNumeralBase.dec.createUnit("4389"), AndroidNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("76", c.convert(CalculatorNumeralBase.dec.createUnit("62"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("12", c.convert(CalculatorNumeralBase.dec.createUnit("10"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("13"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("10445", c.convert(CalculatorNumeralBase.dec.createUnit("4389"), CalculatorNumeralBase.oct).getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -51,20 +51,20 @@ public class AndroidNumeralBaseTest {
|
||||
final Random random = new Random(new Date().getTime());
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
final String value = String.valueOf(random.nextInt());
|
||||
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.oct, AndroidNumeralBase.oct, AndroidNumeralBase.bin, AndroidNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.bin, AndroidNumeralBase.hex, AndroidNumeralBase.dec, AndroidNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.oct, AndroidNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.bin, AndroidNumeralBase.oct, AndroidNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.oct, CalculatorNumeralBase.oct, CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.bin, CalculatorNumeralBase.hex, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.bin, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String convertChain(@NotNull String value, @NotNull AndroidNumeralBase baseAndroid, @NotNull AndroidNumeralBase... typeAndroids) {
|
||||
private String convertChain(@NotNull String value, @NotNull CalculatorNumeralBase baseAndroid, @NotNull CalculatorNumeralBase... typeAndroids) {
|
||||
Unit<String> unit = baseAndroid.createUnit(value);
|
||||
|
||||
for (AndroidNumeralBase typeAndroid : typeAndroids) {
|
||||
unit = AndroidNumeralBase.getConverter().convert(unit, typeAndroid);
|
||||
for (CalculatorNumeralBase typeAndroid : typeAndroids) {
|
||||
unit = CalculatorNumeralBase.getConverter().convert(unit, typeAndroid);
|
||||
}
|
||||
|
||||
return unit.getValue();
|
||||
|
Loading…
Reference in New Issue
Block a user