Conversion

This commit is contained in:
serso 2012-09-24 17:20:16 +04:00
parent b2fe1d43c9
commit 4b08fa133e
25 changed files with 2036 additions and 1783 deletions

View File

@ -32,16 +32,26 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
void simplify(); void simplify();
@NotNull @NotNull
CalculatorEventDataId evaluate(@NotNull JsclOperation operation, CalculatorEventData evaluate(@NotNull JsclOperation operation,
@NotNull String expression); @NotNull String expression);
@NotNull @NotNull
CalculatorEventDataId evaluate(@NotNull JsclOperation operation, CalculatorEventData evaluate(@NotNull JsclOperation operation,
@NotNull String expression, @NotNull String expression,
@NotNull Long sequenceId); @NotNull Long sequenceId);
/*
**********************************************************************
*
* CONVERSION
*
**********************************************************************
*/
boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to);
@NotNull @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 @NotNull
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data); CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
@NotNull @NotNull
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId); CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
} }

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -29,7 +29,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
public CalculatorDisplayImpl(@NotNull Calculator calculator) { public CalculatorDisplayImpl(@NotNull Calculator calculator) {
this.calculator = calculator; this.calculator = calculator;
this.lastCalculatorEventData = CalculatorEventDataImpl.newInstance(CalculatorUtils.createFirstEventDataId()); this.lastCalculatorEventData = CalculatorUtils.createFirstEventDataId();
this.calculator.addCalculatorEventListener(this); this.calculator.addCalculatorEventListener(this);
} }
@ -95,15 +95,21 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorEventType calculatorEventType, @NotNull CalculatorEventType calculatorEventType,
@Nullable Object data) { @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)) { if (calculatorEventData.isAfter(lastCalculatorEventData)) {
lastCalculatorEventData = calculatorEventData; lastCalculatorEventData = calculatorEventData;
} }
switch (calculatorEventType) { switch (calculatorEventType) {
case conversion_failed:
processConversationFailed((CalculatorConversionEventData) calculatorEventData, (ConversionFailure) data);
break;
case conversion_result:
processConversationResult((CalculatorConversionEventData)calculatorEventData, (String)data);
break;
case calculation_result: case calculation_result:
processCalculationResult((CalculatorEvaluationEventData)calculatorEventData, (CalculatorOutput) data); processCalculationResult((CalculatorEvaluationEventData) calculatorEventData, (CalculatorOutput) data);
break; break;
case calculation_cancelled: case calculation_cancelled:
processCalculationCancelled((CalculatorEvaluationEventData)calculatorEventData); 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) { private void processCalculationFailed(@NotNull CalculatorEvaluationEventData calculatorEventData, @NotNull CalculatorFailure data) {
final CalculatorEvalException calculatorEvalException = data.getCalculationEvalException(); final CalculatorEvalException calculatorEvalException = data.getCalculationEvalException();
@ -145,4 +157,14 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
final String stringResult = data.getStringResult(); final String stringResult = data.getStringResult();
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId()); 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());
}
} }

View File

@ -8,7 +8,7 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
* Date: 9/20/12 * Date: 9/20/12
* Time: 10:00 PM * Time: 10:00 PM
*/ */
public interface CalculatorEvaluationEventData extends CalculatorEventData{ public interface CalculatorEvaluationEventData extends CalculatorEventData {
@NotNull @NotNull
JsclOperation getOperation(); JsclOperation getOperation();

View File

@ -51,17 +51,17 @@ public class CalculatorEvaluationEventDataImpl implements CalculatorEvaluationEv
} }
@Override @Override
public boolean isAfter(@NotNull CalculatorEventDataId that) { public boolean isAfter(@NotNull CalculatorEventData that) {
return calculatorEventData.isAfter(that); return calculatorEventData.isAfter(that);
} }
@Override @Override
public boolean isSameSequence(@NotNull CalculatorEventDataId that) { public boolean isSameSequence(@NotNull CalculatorEventData that) {
return this.calculatorEventData.isSameSequence(that); return this.calculatorEventData.isSameSequence(that);
} }
@Override @Override
public boolean isAfterSequence(@NotNull CalculatorEventDataId that) { public boolean isAfterSequence(@NotNull CalculatorEventData that) {
return this.calculatorEventData.isAfterSequence(that); return this.calculatorEventData.isAfterSequence(that);
} }
} }

View File

@ -1,10 +1,24 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 20.09.12 * 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);
} }

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -5,46 +5,51 @@ import org.jetbrains.annotations.NotNull;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 20.09.12 * Date: 20.09.12
* Time: 16:54 * Time: 18:18
*/ */
class CalculatorEventDataImpl implements CalculatorEventData { class CalculatorEventDataImpl implements CalculatorEventData {
@NotNull private static final long NO_SEQUENCE = -1L;
private CalculatorEventDataId calculatorEventDataId;
private CalculatorEventDataImpl(@NotNull CalculatorEventDataId calculatorEventDataId) { private final long eventId;
this.calculatorEventDataId = calculatorEventDataId;
@NotNull
private Long sequenceId = NO_SEQUENCE;
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId) {
this.eventId = id;
this.sequenceId = sequenceId;
} }
@NotNull @NotNull
public static CalculatorEventData newInstance(@NotNull CalculatorEventDataId calculatorEventDataId) { static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
return new CalculatorEventDataImpl(calculatorEventDataId); return new CalculatorEventDataImpl(id, sequenceId);
} }
@Override @Override
public long getEventId() { public long getEventId() {
return calculatorEventDataId.getEventId(); return this.eventId;
} }
@NotNull @NotNull
@Override @Override
public Long getSequenceId() { public Long getSequenceId() {
return calculatorEventDataId.getSequenceId(); return this.sequenceId;
} }
@Override @Override
public boolean isAfter(@NotNull CalculatorEventDataId that) { public boolean isAfter(@NotNull CalculatorEventData that) {
return this.calculatorEventDataId.isAfter(that); return this.eventId > that.getEventId();
} }
@Override @Override
public boolean isSameSequence(@NotNull CalculatorEventDataId that) { public boolean isSameSequence(@NotNull CalculatorEventData that) {
return this.calculatorEventDataId.isSameSequence(that); return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId.equals(that.getSequenceId());
} }
@Override @Override
public boolean isAfterSequence(@NotNull CalculatorEventDataId that) { public boolean isAfterSequence(@NotNull CalculatorEventData that) {
return this.calculatorEventDataId.isAfterSequence(that); return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId > that.getSequenceId();
} }
@Override @Override
@ -54,13 +59,17 @@ class CalculatorEventDataImpl implements CalculatorEventData {
CalculatorEventDataImpl that = (CalculatorEventDataImpl) o; 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; return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return calculatorEventDataId.hashCode(); int result = (int) (eventId ^ (eventId >>> 32));
result = 31 * result + (sequenceId.hashCode());
return result;
} }
} }

View File

@ -12,7 +12,8 @@ public enum CalculatorEventType {
/* /*
********************************************************************** **********************************************************************
* *
* org.solovyev.android.calculator.CalculatorEvaluationEventData * CALCULATION
* org.solovyev.android.calculator.CalculatorEvaluationEventData
* *
********************************************************************** **********************************************************************
*/ */
@ -29,21 +30,27 @@ public enum CalculatorEventType {
calculation_cancelled, calculation_cancelled,
calculation_finished,
// @NotNull org.solovyev.android.calculator.CalculatorFailure // @NotNull org.solovyev.android.calculator.CalculatorFailure
calculation_failed, calculation_failed,
calculation_finished,
/* /*
********************************************************************** **********************************************************************
* *
* CONVERSION * CONVERSION
* CalculatorConversionEventData
* *
********************************************************************** **********************************************************************
*/ */
conversion_started, conversion_started,
// @NotNull String conversion result // @NotNull String conversion result
conversion_result,
// @NotNull ConversionFailure
conversion_failed,
conversion_finished, conversion_finished,
/* /*

View File

@ -11,13 +11,12 @@ import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.msg.MessageRegistry; import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
import org.solovyev.math.units.UnitConverter; import org.solovyev.math.units.*;
import org.solovyev.math.units.UnitImpl;
import org.solovyev.math.units.UnitType;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -48,41 +47,15 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
} }
@NotNull @NotNull
public static String doConversion(@NotNull UnitConverter<String> converter, private CalculatorEventData nextEventData() {
@Nullable String from, long eventId = counter.incrementAndGet();
@NotNull UnitType<String> fromUnitType, return CalculatorEventDataImpl.newInstance(eventId, eventId);
@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 @NotNull
private CalculatorEventDataId nextEventDataId() { private CalculatorEventData nextEventData(@NotNull Long sequenceId) {
long eventId = counter.incrementAndGet(); long eventId = counter.incrementAndGet();
return CalculatorEventDataIdImpl.newInstance(eventId, eventId); return CalculatorEventDataImpl.newInstance(eventId, sequenceId);
}
@NotNull
private CalculatorEventDataId nextEventDataId(@NotNull Long sequenceId) {
long eventId = counter.incrementAndGet();
return CalculatorEventDataIdImpl.newInstance(eventId, sequenceId);
} }
/* /*
@ -116,10 +89,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull @NotNull
@Override @Override
public CalculatorEventDataId evaluate(@NotNull final JsclOperation operation, public CalculatorEventData evaluate(@NotNull final JsclOperation operation,
@NotNull final String expression) { @NotNull final String expression) {
final CalculatorEventDataId eventDataId = nextEventDataId(); final CalculatorEventData eventDataId = nextEventData();
threadPoolExecutor.execute(new Runnable() { threadPoolExecutor.execute(new Runnable() {
@Override @Override
@ -133,8 +106,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull @NotNull
@Override @Override
public CalculatorEventDataId evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) { public CalculatorEventData evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
final CalculatorEventDataId eventDataId = nextEventDataId(sequenceId); final CalculatorEventData eventDataId = nextEventData(sequenceId);
threadPoolExecutor.execute(new Runnable() { threadPoolExecutor.execute(new Runnable() {
@Override @Override
@ -146,72 +119,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
return eventDataId; 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 @Override
public void init() { public void init() {
CalculatorLocatorImpl.getInstance().getEngine().init(); CalculatorLocatorImpl.getInstance().getEngine().init();
@ -219,8 +126,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
} }
@NotNull @NotNull
private CalculatorEventData newConversionEventData(@NotNull Long sequenceId) { private CalculatorConversionEventData newConversionEventData(@NotNull Long sequenceId,
return CalculatorEventDataImpl.newInstance(nextEventDataId(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, private void evaluate(@NotNull Long sequenceId,
@ -280,7 +191,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation, private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
@NotNull String expression, @NotNull String expression,
@NotNull Long calculationId) { @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, 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)); 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); 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 @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
@ -362,6 +383,14 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
} }
} }
/*
**********************************************************************
*
* HISTORY
*
**********************************************************************
*/
@Override @Override
public void doHistoryAction(@NotNull HistoryAction historyAction) { public void doHistoryAction(@NotNull HistoryAction historyAction) {
final CalculatorHistory history = CalculatorLocatorImpl.getInstance().getHistory(); final CalculatorHistory history = CalculatorLocatorImpl.getInstance().getHistory();
@ -378,28 +407,27 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay()); editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
} }
@NotNull
private CalculatorEditor getEditor() {
return CalculatorLocatorImpl.getInstance().getEditor();
}
@NotNull @NotNull
@Override @Override
public CalculatorHistoryState getCurrentHistoryState() { public CalculatorHistoryState getCurrentHistoryState() {
return CalculatorHistoryState.newInstance(getEditor(), getDisplay()); return CalculatorHistoryState.newInstance(getEditor(), getDisplay());
} }
/*
**********************************************************************
*
* OTHER
*
**********************************************************************
*/
@NotNull
private CalculatorEditor getEditor() {
return CalculatorLocatorImpl.getInstance().getEditor();
}
@NotNull @NotNull
private CalculatorDisplay getDisplay() { private CalculatorDisplay getDisplay() {
return CalculatorLocatorImpl.getInstance().getDisplay(); return CalculatorLocatorImpl.getInstance().getDisplay();
} }
public static final class ConversionException extends Exception {
private ConversionException() {
}
private ConversionException(Throwable throwable) {
super(throwable);
}
}
} }

View File

@ -16,7 +16,7 @@ public final class CalculatorUtils {
} }
@NotNull @NotNull
public static CalculatorEventDataId createFirstEventDataId() { public static CalculatorEventData createFirstEventDataId() {
return CalculatorEventDataIdImpl.newInstance(FIRST_ID, FIRST_ID); return CalculatorEventDataImpl.newInstance(FIRST_ID, FIRST_ID);
} }
} }

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -32,7 +32,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>(); private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
@NotNull @NotNull
private volatile CalculatorEventDataId lastEventDataId = CalculatorUtils.createFirstEventDataId(); private volatile CalculatorEventData lastEventDataId = CalculatorUtils.createFirstEventDataId();
@Nullable @Nullable
private volatile CalculatorEditorViewState lastEditorViewState; private volatile CalculatorEditorViewState lastEditorViewState;

View File

@ -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!");
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -44,31 +44,36 @@ public class AndroidCalculator implements Calculator {
@Override @Override
@NotNull @NotNull
public CalculatorEventDataId evaluate(@NotNull JsclOperation operation, @NotNull String expression) { public CalculatorEventData evaluate(@NotNull JsclOperation operation, @NotNull String expression) {
return calculator.evaluate(operation, expression); return calculator.evaluate(operation, expression);
} }
@Override @Override
@NotNull @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); 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 @Override
@NotNull @NotNull
public CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to) { public CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to) {
return calculator.convert(generic, to); return calculator.convert(generic, to);
} }
@Override @Override
@NotNull @NotNull
public CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
return calculator.fireCalculatorEvent(calculatorEventType, data); return calculator.fireCalculatorEvent(calculatorEventType, data);
} }
@Override @Override
@NotNull @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); return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId);
} }

View File

@ -3,14 +3,10 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.math.units.Unit; import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.math.units.UnitConverter;
import org.solovyev.math.units.UnitImpl;
import org.solovyev.math.units.UnitType;
import org.solovyev.android.view.drag.DirectionDragButton; import org.solovyev.android.view.drag.DirectionDragButton;
import org.solovyev.android.view.drag.DragDirection; import org.solovyev.android.view.drag.DragDirection;
import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -20,9 +16,9 @@ import java.util.List;
* Date: 4/21/12 * Date: 4/21/12
* Time: 8:00 PM * Time: 8:00 PM
*/ */
public enum AndroidNumeralBase implements UnitType<String> { public enum AndroidNumeralBase {
bin(NumeralBase.bin) { bin(CalculatorNumeralBase.bin) {
@NotNull @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
@ -30,7 +26,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
} }
}, },
oct(NumeralBase.oct) { oct(CalculatorNumeralBase.oct) {
@NotNull @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
@ -40,7 +36,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
} }
}, },
dec(NumeralBase.dec) { dec(CalculatorNumeralBase.dec) {
@NotNull @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
@ -50,7 +46,7 @@ public enum AndroidNumeralBase implements UnitType<String> {
} }
}, },
hex(NumeralBase.hex) { hex(CalculatorNumeralBase.hex) {
@NotNull @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); 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 @NotNull
private final NumeralBase numeralBase; private final CalculatorNumeralBase calculatorNumeralBase;
private AndroidNumeralBase(@NotNull NumeralBase numeralBase) { private AndroidNumeralBase(@NotNull CalculatorNumeralBase calculatorNumeralBase) {
this.numeralBase = numeralBase; this.calculatorNumeralBase = calculatorNumeralBase;
}
@NotNull
public Unit<String> createUnit(@NotNull String value) {
return UnitImpl.newInstance(value, this);
} }
@NotNull @NotNull
@ -101,54 +92,13 @@ public enum AndroidNumeralBase implements UnitType<String> {
@NotNull @NotNull
public NumeralBase getNumeralBase() { public NumeralBase getNumeralBase() {
return numeralBase; return calculatorNumeralBase.getNumeralBase();
}
@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);
}
} }
@NotNull @NotNull
public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) { public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) {
for (AndroidNumeralBase androidNumeralBase : values()) { for (AndroidNumeralBase androidNumeralBase : values()) {
if (androidNumeralBase.numeralBase == nb) { if (androidNumeralBase.calculatorNumeralBase.getNumeralBase() == nb) {
return androidNumeralBase; return androidNumeralBase;
} }
} }

View File

@ -30,10 +30,11 @@ enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
if (operation == JsclOperation.numeric) { if (operation == JsclOperation.numeric) {
if (generic.getConstants().isEmpty()) { if (generic.getConstants().isEmpty()) {
convert(generic);
// conversion possible => return true // 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 @Override
public void onClick(@NotNull CalculatorDisplayViewState data, @NotNull Context context) { 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 (result != null) {
CalculatorLocatorImpl.getInstance().getCalculator().convert(result, this.toNumeralBase);
if (lastResult != null) {
convert(lastResult);
} }
} }
private void convert(@NotNull Generic generic) {
CalculatorLocatorImpl.getInstance().getCalculator().convert(generic, this.toNumeralBase);
}
} }

View File

@ -7,6 +7,7 @@ import android.view.WindowManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.math.units.Unit; import org.solovyev.math.units.Unit;
import org.solovyev.math.units.UnitImpl; import org.solovyev.math.units.UnitImpl;
import org.solovyev.common.MutableObject; import org.solovyev.common.MutableObject;
@ -30,22 +31,22 @@ public class NumeralBaseConverterDialog {
public void show(@NotNull Context context) { public void show(@NotNull Context context) {
final UnitConverterViewBuilder b = new UnitConverterViewBuilder(); final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values())); b.setFromUnitTypes(Arrays.asList(CalculatorNumeralBase.values()));
b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.values())); b.setToUnitTypes(Arrays.asList(CalculatorNumeralBase.values()));
if (!StringUtils.isEmpty(initialFromValue)) { if (!StringUtils.isEmpty(initialFromValue)) {
String value = initialFromValue; String value = initialFromValue;
try { try {
value = ToJsclTextProcessor.getInstance().process(value).getExpression(); 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) { } 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 { } 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>(); final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
b.setOkButtonOnClickListener(new View.OnClickListener() { b.setOkButtonOnClickListener(new View.OnClickListener() {

View File

@ -11,10 +11,7 @@ import android.widget.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorImpl; import org.solovyev.android.calculator.CalculatorImpl;
import org.solovyev.math.units.Unit; import org.solovyev.math.units.*;
import org.solovyev.math.units.UnitConverter;
import org.solovyev.math.units.UnitImpl;
import org.solovyev.math.units.UnitType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.ViewBuilder; import org.solovyev.android.view.ViewBuilder;
import org.solovyev.android.view.ViewFromLayoutBuilder; import org.solovyev.android.view.ViewFromLayoutBuilder;
@ -173,8 +170,8 @@ public class UnitConverterViewBuilder implements ViewBuilder<View> {
final String from = fromEditText.getText().toString(); final String from = fromEditText.getText().toString();
try { try {
toEditText.setText(CalculatorImpl.doConversion(converter, from, getFromUnitType(main), getToUnitType(main))); toEditText.setText(ConversionUtils.doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
} catch (CalculatorImpl.ConversionException e) { } catch (ConversionException e) {
toEditText.setText(context.getString(R.string.c_error)); toEditText.setText(context.getString(R.string.c_error));
} }
} }

View File

@ -3,7 +3,7 @@ package org.solovyev.android;
import junit.framework.Assert; import junit.framework.Assert;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.Test; 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.Unit;
import org.solovyev.math.units.UnitConverter; import org.solovyev.math.units.UnitConverter;
@ -18,32 +18,32 @@ import java.util.Random;
public class AndroidNumeralBaseTest { public class AndroidNumeralBaseTest {
@NotNull @NotNull
private final UnitConverter c = AndroidNumeralBase.getConverter(); private final UnitConverter c = CalculatorNumeralBase.getConverter();
@Test @Test
public void testIsSupported() throws Exception { public void testIsSupported() throws Exception {
Assert.assertTrue(c.isSupported(AndroidNumeralBase.bin, AndroidNumeralBase.dec)); Assert.assertTrue(c.isSupported(CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
} }
@Test @Test
public void testConvertFromDec() throws Exception { public void testConvertFromDec() throws Exception {
Assert.assertEquals("101", c.convert(AndroidNumeralBase.dec.createUnit("5"), AndroidNumeralBase.bin).getValue()); Assert.assertEquals("101", c.convert(CalculatorNumeralBase.dec.createUnit("5"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("1", c.convert(AndroidNumeralBase.dec.createUnit("1"), AndroidNumeralBase.bin).getValue()); Assert.assertEquals("1", c.convert(CalculatorNumeralBase.dec.createUnit("1"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.bin).getValue()); Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("1111100111", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.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("A23", c.convert(CalculatorNumeralBase.dec.createUnit("2595"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("AEE", c.convert(AndroidNumeralBase.dec.createUnit("2798"), AndroidNumeralBase.hex).getValue()); Assert.assertEquals("AEE", c.convert(CalculatorNumeralBase.dec.createUnit("2798"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("15", c.convert(AndroidNumeralBase.dec.createUnit("21"), AndroidNumeralBase.hex).getValue()); Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("21"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.hex).getValue()); Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("3E7", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.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("76", c.convert(CalculatorNumeralBase.dec.createUnit("62"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("12", c.convert(AndroidNumeralBase.dec.createUnit("10"), AndroidNumeralBase.oct).getValue()); Assert.assertEquals("12", c.convert(CalculatorNumeralBase.dec.createUnit("10"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("15", c.convert(AndroidNumeralBase.dec.createUnit("13"), AndroidNumeralBase.oct).getValue()); Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("13"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.oct).getValue()); Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("10445", c.convert(AndroidNumeralBase.dec.createUnit("4389"), AndroidNumeralBase.oct).getValue()); Assert.assertEquals("10445", c.convert(CalculatorNumeralBase.dec.createUnit("4389"), CalculatorNumeralBase.oct).getValue());
} }
@Test @Test
@ -51,20 +51,20 @@ public class AndroidNumeralBaseTest {
final Random random = new Random(new Date().getTime()); final Random random = new Random(new Date().getTime());
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {
final String value = String.valueOf(random.nextInt()); 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, CalculatorNumeralBase.dec, CalculatorNumeralBase.oct, CalculatorNumeralBase.oct, CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.bin, AndroidNumeralBase.hex, AndroidNumeralBase.dec, AndroidNumeralBase.dec)); Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.bin, CalculatorNumeralBase.hex, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.oct, AndroidNumeralBase.dec)); Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.bin, AndroidNumeralBase.oct, AndroidNumeralBase.dec)); Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.bin, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
} }
} }
@NotNull @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); Unit<String> unit = baseAndroid.createUnit(value);
for (AndroidNumeralBase typeAndroid : typeAndroids) { for (CalculatorNumeralBase typeAndroid : typeAndroids) {
unit = AndroidNumeralBase.getConverter().convert(unit, typeAndroid); unit = CalculatorNumeralBase.getConverter().convert(unit, typeAndroid);
} }
return unit.getValue(); return unit.getValue();