code reformat
This commit is contained in:
@@ -29,20 +29,19 @@ public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P ext
|
||||
@NotNull
|
||||
private final String prefix;
|
||||
|
||||
@NotNull
|
||||
private final MathEntityDao<P> mathEntityDao;
|
||||
@NotNull
|
||||
private final MathEntityDao<P> mathEntityDao;
|
||||
|
||||
protected AbstractCalculatorMathRegistry(@NotNull MathRegistry<T> mathRegistry,
|
||||
@NotNull String prefix,
|
||||
@NotNull MathEntityDao<P> mathEntityDao) {
|
||||
@NotNull String prefix,
|
||||
@NotNull MathEntityDao<P> mathEntityDao) {
|
||||
this.mathRegistry = mathRegistry;
|
||||
this.prefix = prefix;
|
||||
this.mathEntityDao = mathEntityDao;
|
||||
}
|
||||
this.mathEntityDao = mathEntityDao;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
protected abstract Map<String, String> getSubstitutes();
|
||||
|
||||
@Nullable
|
||||
@@ -58,60 +57,60 @@ public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P ext
|
||||
stringName = prefix + substitute;
|
||||
}
|
||||
|
||||
return mathEntityDao.getDescription(stringName);
|
||||
return mathEntityDao.getDescription(stringName);
|
||||
}
|
||||
|
||||
public synchronized void load() {
|
||||
final MathEntityPersistenceContainer<P> persistenceContainer = mathEntityDao.load();
|
||||
public synchronized void load() {
|
||||
final MathEntityPersistenceContainer<P> persistenceContainer = mathEntityDao.load();
|
||||
|
||||
final List<P> notCreatedEntities = new ArrayList<P>();
|
||||
final List<P> notCreatedEntities = new ArrayList<P>();
|
||||
|
||||
if (persistenceContainer != null) {
|
||||
for (P entity : persistenceContainer.getEntities()) {
|
||||
if (!contains(entity.getName())) {
|
||||
try {
|
||||
final JBuilder<? extends T> builder = createBuilder(entity);
|
||||
add(builder);
|
||||
} catch (RuntimeException e) {
|
||||
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
|
||||
notCreatedEntities.add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (persistenceContainer != null) {
|
||||
for (P entity : persistenceContainer.getEntities()) {
|
||||
if (!contains(entity.getName())) {
|
||||
try {
|
||||
final JBuilder<? extends T> builder = createBuilder(entity);
|
||||
add(builder);
|
||||
} catch (RuntimeException e) {
|
||||
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
|
||||
notCreatedEntities.add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (!notCreatedEntities.isEmpty()) {
|
||||
final StringBuilder errorMessage = new StringBuilder(notCreatedEntities.size() * 100);
|
||||
for (P notCreatedEntity : notCreatedEntities) {
|
||||
errorMessage.append(notCreatedEntity).append("\n\n");
|
||||
}
|
||||
try {
|
||||
if (!notCreatedEntities.isEmpty()) {
|
||||
final StringBuilder errorMessage = new StringBuilder(notCreatedEntities.size() * 100);
|
||||
for (P notCreatedEntity : notCreatedEntities) {
|
||||
errorMessage.append(notCreatedEntity).append("\n\n");
|
||||
}
|
||||
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_message_dialog, MessageDialogData.newInstance(CalculatorMessages.newErrorMessage(CalculatorMessages.msg_007, errorMessage.toString()), null));
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
// just in case
|
||||
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_message_dialog, MessageDialogData.newInstance(CalculatorMessages.newErrorMessage(CalculatorMessages.msg_007, errorMessage.toString()), null));
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
// just in case
|
||||
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
protected abstract JBuilder<? extends T> createBuilder(@NotNull P entity);
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public synchronized void save() {
|
||||
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
|
||||
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
|
||||
|
||||
for (T entity : this.getEntities()) {
|
||||
if (!entity.isSystem()) {
|
||||
final P persistenceEntity = transform(entity);
|
||||
if (persistenceEntity != null) {
|
||||
container.getEntities().add(persistenceEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (T entity : this.getEntities()) {
|
||||
if (!entity.isSystem()) {
|
||||
final P persistenceEntity = transform(entity);
|
||||
if (persistenceEntity != null) {
|
||||
container.getEntities().add(persistenceEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.mathEntityDao.save(container);
|
||||
this.mathEntityDao.save(container);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@@ -40,11 +40,11 @@ public abstract class AbstractNumberBuilder {
|
||||
* @return true if we can continue of processing of current number, if false - new number should be constructed
|
||||
*/
|
||||
protected boolean canContinue(@NotNull MathType.Result mathTypeResult) {
|
||||
boolean result = mathTypeResult.getMathType().getGroupType() == MathType.MathGroupType.number &&
|
||||
!spaceBefore(mathTypeResult) &&
|
||||
numeralBaseCheck(mathTypeResult) &&
|
||||
numeralBaseInTheStart(mathTypeResult.getMathType()) || isSignAfterE(mathTypeResult);
|
||||
return result;
|
||||
boolean result = mathTypeResult.getMathType().getGroupType() == MathType.MathGroupType.number &&
|
||||
!spaceBefore(mathTypeResult) &&
|
||||
numeralBaseCheck(mathTypeResult) &&
|
||||
numeralBaseInTheStart(mathTypeResult.getMathType()) || isSignAfterE(mathTypeResult);
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean spaceBefore(@NotNull MathType.Result mathTypeResult) {
|
||||
@@ -60,17 +60,17 @@ public abstract class AbstractNumberBuilder {
|
||||
}
|
||||
|
||||
private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) {
|
||||
if (!isHexMode()) {
|
||||
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
|
||||
final StringBuilder localNb = numberBuilder;
|
||||
if (localNb != null && localNb.length() > 0) {
|
||||
if (localNb.charAt(localNb.length() - 1) == MathType.POWER_10) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (!isHexMode()) {
|
||||
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
|
||||
final StringBuilder localNb = numberBuilder;
|
||||
if (localNb != null && localNb.length() > 0) {
|
||||
if (localNb.charAt(localNb.length() - 1) == MathType.POWER_10) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isHexMode() {
|
||||
|
@@ -15,59 +15,59 @@ import org.solovyev.common.history.HistoryControl;
|
||||
*/
|
||||
public interface Calculator extends CalculatorEventContainer, HistoryControl<CalculatorHistoryState> {
|
||||
|
||||
void init();
|
||||
void init();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
void evaluate();
|
||||
void evaluate();
|
||||
|
||||
void evaluate(@NotNull Long sequenceId);
|
||||
void evaluate(@NotNull Long sequenceId);
|
||||
|
||||
void simplify();
|
||||
void simplify();
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression);
|
||||
@NotNull
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long sequenceId);
|
||||
@NotNull
|
||||
CalculatorEventData evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long sequenceId);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to);
|
||||
boolean isConversionPossible(@NotNull Generic generic, @NotNull NumeralBase from, @NotNull NumeralBase to);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
@NotNull
|
||||
CalculatorEventData convert(@NotNull Generic generic, @NotNull NumeralBase to);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Object source);
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Object source);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
||||
@NotNull
|
||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
||||
|
||||
@NotNull
|
||||
PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException;
|
||||
|
@@ -10,10 +10,10 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface CalculatorClipboard {
|
||||
|
||||
@Nullable
|
||||
String getText();
|
||||
@Nullable
|
||||
String getText();
|
||||
|
||||
void setText(@NotNull String text);
|
||||
void setText(@NotNull String text);
|
||||
|
||||
void setText(@NotNull CharSequence text);
|
||||
void setText(@NotNull CharSequence text);
|
||||
}
|
||||
|
@@ -11,16 +11,16 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface CalculatorConversionEventData extends CalculatorEventData {
|
||||
|
||||
// display state on the moment of conversion
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getDisplayState();
|
||||
// display state on the moment of conversion
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getDisplayState();
|
||||
|
||||
@NotNull
|
||||
NumeralBase getFromNumeralBase();
|
||||
@NotNull
|
||||
NumeralBase getFromNumeralBase();
|
||||
|
||||
@NotNull
|
||||
NumeralBase getToNumeralBase();
|
||||
@NotNull
|
||||
NumeralBase getToNumeralBase();
|
||||
|
||||
@NotNull
|
||||
Generic getValue();
|
||||
@NotNull
|
||||
Generic getValue();
|
||||
}
|
||||
|
@@ -11,93 +11,93 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class CalculatorConversionEventDataImpl implements CalculatorConversionEventData {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
@NotNull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
|
||||
@NotNull
|
||||
private NumeralBase fromNumeralBase;
|
||||
@NotNull
|
||||
private NumeralBase fromNumeralBase;
|
||||
|
||||
@NotNull
|
||||
private NumeralBase toNumeralBase;
|
||||
@NotNull
|
||||
private NumeralBase toNumeralBase;
|
||||
|
||||
@NotNull
|
||||
private Generic value;
|
||||
@NotNull
|
||||
private Generic value;
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState displayState;
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState displayState;
|
||||
|
||||
private CalculatorConversionEventDataImpl() {
|
||||
}
|
||||
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();
|
||||
@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;
|
||||
result.calculatorEventData = calculatorEventData;
|
||||
result.value = value;
|
||||
result.displayState = displayViewState;
|
||||
result.fromNumeralBase = from;
|
||||
result.toNumeralBase = to;
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventData.getEventId();
|
||||
}
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventData.getEventId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return calculatorEventData.getSource();
|
||||
}
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return calculatorEventData.getSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
@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 isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getDisplayState() {
|
||||
return this.displayState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getDisplayState() {
|
||||
return this.displayState;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public NumeralBase getFromNumeralBase() {
|
||||
return fromNumeralBase;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public NumeralBase getFromNumeralBase() {
|
||||
return fromNumeralBase;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public NumeralBase getToNumeralBase() {
|
||||
return toNumeralBase;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public NumeralBase getToNumeralBase() {
|
||||
return toNumeralBase;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Generic getValue() {
|
||||
return value;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public Generic getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@@ -16,16 +16,16 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface CalculatorDisplay extends CalculatorEventListener {
|
||||
|
||||
void setView(@Nullable CalculatorDisplayView view);
|
||||
void setView(@Nullable CalculatorDisplayView view);
|
||||
|
||||
@Nullable
|
||||
CalculatorDisplayView getView();
|
||||
@Nullable
|
||||
CalculatorDisplayView getView();
|
||||
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getViewState();
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getViewState();
|
||||
|
||||
void setViewState(@NotNull CalculatorDisplayViewState viewState);
|
||||
void setViewState(@NotNull CalculatorDisplayViewState viewState);
|
||||
|
||||
@NotNull
|
||||
CalculatorEventData getLastEventData();
|
||||
@NotNull
|
||||
CalculatorEventData getLastEventData();
|
||||
}
|
||||
|
@@ -9,26 +9,26 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayChangeEventData {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorDisplayViewState oldState;
|
||||
@NotNull
|
||||
private final CalculatorDisplayViewState oldState;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorDisplayViewState newState;
|
||||
@NotNull
|
||||
private final CalculatorDisplayViewState newState;
|
||||
|
||||
public CalculatorDisplayChangeEventDataImpl(@NotNull CalculatorDisplayViewState oldState, @NotNull CalculatorDisplayViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
public CalculatorDisplayChangeEventDataImpl(@NotNull CalculatorDisplayViewState oldState, @NotNull CalculatorDisplayViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
||||
|
@@ -12,158 +12,158 @@ import static org.solovyev.android.calculator.CalculatorEventType.*;
|
||||
*/
|
||||
public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEvent;
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEvent;
|
||||
|
||||
@Nullable
|
||||
private CalculatorDisplayView view;
|
||||
@Nullable
|
||||
private CalculatorDisplayView view;
|
||||
|
||||
@NotNull
|
||||
private final Object viewLock = new Object();
|
||||
@NotNull
|
||||
private final Object viewLock = new Object();
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState viewState = CalculatorDisplayViewStateImpl.newDefaultInstance();
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState viewState = CalculatorDisplayViewStateImpl.newDefaultInstance();
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
public CalculatorDisplayImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
public CalculatorDisplayImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.lastEvent = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setView(@Nullable CalculatorDisplayView view) {
|
||||
synchronized (viewLock) {
|
||||
this.view = view;
|
||||
@Override
|
||||
public void setView(@Nullable CalculatorDisplayView view) {
|
||||
synchronized (viewLock) {
|
||||
this.view = view;
|
||||
|
||||
if (view != null) {
|
||||
this.view.setState(viewState);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (view != null) {
|
||||
this.view.setState(viewState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public CalculatorDisplayView getView() {
|
||||
return this.view;
|
||||
}
|
||||
@Nullable
|
||||
@Override
|
||||
public CalculatorDisplayView getView() {
|
||||
return this.view;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getViewState() {
|
||||
return this.viewState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getViewState() {
|
||||
return this.viewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewState(@NotNull CalculatorDisplayViewState newViewState) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
@Override
|
||||
public void setViewState(@NotNull CalculatorDisplayViewState newViewState) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
|
||||
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
|
||||
private void setViewStateForSequence(@NotNull CalculatorDisplayViewState newViewState, @NotNull Long sequenceId) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
private void setViewStateForSequence(@NotNull CalculatorDisplayViewState newViewState, @NotNull Long sequenceId) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
|
||||
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState), sequenceId);
|
||||
}
|
||||
}
|
||||
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState), sequenceId);
|
||||
}
|
||||
}
|
||||
|
||||
// must be synchronized with viewLock
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState setViewState0(@NotNull CalculatorDisplayViewState newViewState) {
|
||||
final CalculatorDisplayViewState oldViewState = this.viewState;
|
||||
// must be synchronized with viewLock
|
||||
@NotNull
|
||||
private CalculatorDisplayViewState setViewState0(@NotNull CalculatorDisplayViewState newViewState) {
|
||||
final CalculatorDisplayViewState oldViewState = this.viewState;
|
||||
|
||||
this.viewState = newViewState;
|
||||
if (this.view != null) {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
return oldViewState;
|
||||
}
|
||||
this.viewState = newViewState;
|
||||
if (this.view != null) {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
return oldViewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData getLastEventData() {
|
||||
return lastEvent.getLastEventData();
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorEventData getLastEventData() {
|
||||
return lastEvent.getLastEventData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(calculation_result, calculation_failed, calculation_cancelled, conversion_result, conversion_failed)) {
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(calculation_result, calculation_failed, calculation_cancelled, conversion_result, conversion_failed)) {
|
||||
|
||||
final CalculatorEventHolder.Result result = lastEvent.apply(calculatorEventData);
|
||||
final CalculatorEventHolder.Result result = lastEvent.apply(calculatorEventData);
|
||||
|
||||
if (result.isNewAfter()) {
|
||||
switch (calculatorEventType) {
|
||||
case conversion_failed:
|
||||
processConversationFailed((CalculatorConversionEventData) calculatorEventData, (ConversionFailure) data);
|
||||
break;
|
||||
case conversion_result:
|
||||
processConversationResult((CalculatorConversionEventData)calculatorEventData, (String)data);
|
||||
break;
|
||||
case calculation_result:
|
||||
processCalculationResult((CalculatorEvaluationEventData) calculatorEventData, (CalculatorOutput) data);
|
||||
break;
|
||||
case calculation_cancelled:
|
||||
processCalculationCancelled((CalculatorEvaluationEventData)calculatorEventData);
|
||||
break;
|
||||
case calculation_failed:
|
||||
processCalculationFailed((CalculatorEvaluationEventData)calculatorEventData, (CalculatorFailure) data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (result.isNewAfter()) {
|
||||
switch (calculatorEventType) {
|
||||
case conversion_failed:
|
||||
processConversationFailed((CalculatorConversionEventData) calculatorEventData, (ConversionFailure) data);
|
||||
break;
|
||||
case conversion_result:
|
||||
processConversationResult((CalculatorConversionEventData) calculatorEventData, (String) data);
|
||||
break;
|
||||
case calculation_result:
|
||||
processCalculationResult((CalculatorEvaluationEventData) calculatorEventData, (CalculatorOutput) data);
|
||||
break;
|
||||
case calculation_cancelled:
|
||||
processCalculationCancelled((CalculatorEvaluationEventData) calculatorEventData);
|
||||
break;
|
||||
case calculation_failed:
|
||||
processCalculationFailed((CalculatorEvaluationEventData) calculatorEventData, (CalculatorFailure) data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processConversationFailed(@NotNull CalculatorConversionEventData calculatorEventData,
|
||||
@NotNull ConversionFailure data) {
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getDisplayState().getOperation(), CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error)), calculatorEventData.getSequenceId());
|
||||
private void 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();
|
||||
|
||||
final String errorMessage;
|
||||
if (calculatorEvalException != null) {
|
||||
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
} else {
|
||||
final CalculatorParseException calculationParseException = data.getCalculationParseException();
|
||||
if (calculationParseException != null) {
|
||||
errorMessage = calculationParseException.getLocalizedMessage();
|
||||
} else {
|
||||
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
}
|
||||
}
|
||||
final String errorMessage;
|
||||
if (calculatorEvalException != null) {
|
||||
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
} else {
|
||||
final CalculatorParseException calculationParseException = data.getCalculationParseException();
|
||||
if (calculationParseException != null) {
|
||||
errorMessage = calculationParseException.getLocalizedMessage();
|
||||
} else {
|
||||
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
}
|
||||
}
|
||||
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
|
||||
}
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
|
||||
}
|
||||
|
||||
private void processCalculationCancelled(@NotNull CalculatorEvaluationEventData calculatorEventData) {
|
||||
final String errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
private void processCalculationCancelled(@NotNull CalculatorEvaluationEventData calculatorEventData) {
|
||||
final String errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
|
||||
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
|
||||
}
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
|
||||
}
|
||||
|
||||
private void processCalculationResult(@NotNull CalculatorEvaluationEventData calculatorEventData, @NotNull CalculatorOutput data) {
|
||||
final String stringResult = data.getStringResult();
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId());
|
||||
}
|
||||
private void processCalculationResult(@NotNull CalculatorEvaluationEventData calculatorEventData, @NotNull CalculatorOutput data) {
|
||||
final String stringResult = data.getStringResult();
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId());
|
||||
}
|
||||
|
||||
private void processConversationResult(@NotNull CalculatorConversionEventData calculatorEventData, @NotNull String result) {
|
||||
// add prefix
|
||||
if (calculatorEventData.getFromNumeralBase() != calculatorEventData.getToNumeralBase()) {
|
||||
result = calculatorEventData.getToNumeralBase().getJsclPrefix() + result;
|
||||
}
|
||||
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());
|
||||
}
|
||||
final CalculatorDisplayViewState displayState = calculatorEventData.getDisplayState();
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId());
|
||||
}
|
||||
}
|
||||
|
@@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface CalculatorDisplayView {
|
||||
|
||||
void setState(@NotNull CalculatorDisplayViewState state);
|
||||
void setState(@NotNull CalculatorDisplayViewState state);
|
||||
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getState();
|
||||
@NotNull
|
||||
CalculatorDisplayViewState getState();
|
||||
}
|
||||
|
@@ -14,22 +14,22 @@ import java.io.Serializable;
|
||||
*/
|
||||
public interface CalculatorDisplayViewState extends Serializable {
|
||||
|
||||
@NotNull
|
||||
String getText();
|
||||
@NotNull
|
||||
String getText();
|
||||
|
||||
int getSelection();
|
||||
int getSelection();
|
||||
|
||||
@Nullable
|
||||
Generic getResult();
|
||||
@Nullable
|
||||
Generic getResult();
|
||||
|
||||
boolean isValid();
|
||||
boolean isValid();
|
||||
|
||||
@Nullable
|
||||
String getErrorMessage();
|
||||
@Nullable
|
||||
String getErrorMessage();
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
||||
@Nullable
|
||||
String getStringResult();
|
||||
@Nullable
|
||||
String getStringResult();
|
||||
}
|
||||
|
@@ -13,116 +13,116 @@ import org.solovyev.common.text.Strings;
|
||||
*/
|
||||
public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewState {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private JsclOperation operation = JsclOperation.numeric;
|
||||
@NotNull
|
||||
private JsclOperation operation = JsclOperation.numeric;
|
||||
|
||||
@Nullable
|
||||
private transient Generic result;
|
||||
@Nullable
|
||||
private transient Generic result;
|
||||
|
||||
@Nullable
|
||||
private String stringResult = "";
|
||||
@Nullable
|
||||
private String stringResult = "";
|
||||
|
||||
private boolean valid = true;
|
||||
private boolean valid = true;
|
||||
|
||||
@Nullable
|
||||
private String errorMessage;
|
||||
@Nullable
|
||||
private String errorMessage;
|
||||
|
||||
private int selection = 0;
|
||||
private int selection = 0;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private CalculatorDisplayViewStateImpl() {
|
||||
}
|
||||
private CalculatorDisplayViewStateImpl() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newDefaultInstance() {
|
||||
return new CalculatorDisplayViewStateImpl();
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newDefaultInstance() {
|
||||
return new CalculatorDisplayViewStateImpl();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newErrorState(@NotNull JsclOperation operation,
|
||||
@NotNull String errorMessage) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
calculatorDisplayState.valid = false;
|
||||
calculatorDisplayState.errorMessage = errorMessage;
|
||||
calculatorDisplayState.operation = operation;
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newErrorState(@NotNull JsclOperation operation,
|
||||
@NotNull String errorMessage) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
calculatorDisplayState.valid = false;
|
||||
calculatorDisplayState.errorMessage = errorMessage;
|
||||
calculatorDisplayState.operation = operation;
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newValidState(@NotNull JsclOperation operation,
|
||||
@Nullable Generic result,
|
||||
@NotNull String stringResult,
|
||||
int selection) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
calculatorDisplayState.valid = true;
|
||||
calculatorDisplayState.result = result;
|
||||
calculatorDisplayState.stringResult = stringResult;
|
||||
calculatorDisplayState.operation = operation;
|
||||
calculatorDisplayState.selection = selection;
|
||||
@NotNull
|
||||
public static CalculatorDisplayViewState newValidState(@NotNull JsclOperation operation,
|
||||
@Nullable Generic result,
|
||||
@NotNull String stringResult,
|
||||
int selection) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
calculatorDisplayState.valid = true;
|
||||
calculatorDisplayState.result = result;
|
||||
calculatorDisplayState.stringResult = stringResult;
|
||||
calculatorDisplayState.operation = operation;
|
||||
calculatorDisplayState.selection = selection;
|
||||
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return Strings.getNotEmpty(isValid() ? stringResult : errorMessage, "");
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return Strings.getNotEmpty(isValid() ? stringResult : errorMessage, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return selection;
|
||||
}
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return selection;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Generic getResult() {
|
||||
return this.result;
|
||||
}
|
||||
@Nullable
|
||||
@Override
|
||||
public Generic getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return this.valid;
|
||||
}
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
@Nullable
|
||||
@Override
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
@Override
|
||||
@Nullable
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
}
|
||||
|
@@ -11,79 +11,80 @@ import org.solovyev.common.gui.CursorControl;
|
||||
*/
|
||||
public interface CalculatorEditor extends CalculatorEventListener {
|
||||
|
||||
void setView(@Nullable CalculatorEditorView view);
|
||||
void setView(@Nullable CalculatorEditorView view);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState getViewState();
|
||||
@NotNull
|
||||
CalculatorEditorViewState getViewState();
|
||||
|
||||
// updates state of view (view.setState())
|
||||
void updateViewState();
|
||||
// updates state of view (view.setState())
|
||||
void updateViewState();
|
||||
|
||||
void setViewState(@NotNull CalculatorEditorViewState viewState);
|
||||
void setViewState(@NotNull CalculatorEditorViewState viewState);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CURSOR CONTROL
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/**
|
||||
* Method sets the cursor to the beginning
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnStart();
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CURSOR CONTROL
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
/**
|
||||
* Method sets the cursor to the end
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnEnd();
|
||||
/**
|
||||
* Method sets the cursor to the beginning
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnStart();
|
||||
|
||||
/**
|
||||
* Method moves cursor to the left of current position
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorLeft();
|
||||
/**
|
||||
* Method sets the cursor to the end
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnEnd();
|
||||
|
||||
/**
|
||||
* Method moves cursor to the right of current position
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorRight();
|
||||
/**
|
||||
* Method moves cursor to the left of current position
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorLeft();
|
||||
|
||||
@NotNull
|
||||
CursorControl asCursorControl();
|
||||
/**
|
||||
* Method moves cursor to the right of current position
|
||||
*/
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorRight();
|
||||
|
||||
@NotNull
|
||||
CursorControl asCursorControl();
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR OPERATIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEditorViewState erase();
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR OPERATIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
CalculatorEditorViewState erase();
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState clear();
|
||||
@NotNull
|
||||
CalculatorEditorViewState clear();
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState setText(@NotNull String text);
|
||||
@NotNull
|
||||
CalculatorEditorViewState setText(@NotNull String text);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState setText(@NotNull String text, int selection);
|
||||
@NotNull
|
||||
CalculatorEditorViewState setText(@NotNull String text, int selection);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState insert(@NotNull String text);
|
||||
@NotNull
|
||||
CalculatorEditorViewState insert(@NotNull String text);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState insert(@NotNull String text, int selectionOffset);
|
||||
@NotNull
|
||||
CalculatorEditorViewState insert(@NotNull String text, int selectionOffset);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState moveSelection(int offset);
|
||||
@NotNull
|
||||
CalculatorEditorViewState moveSelection(int offset);
|
||||
|
||||
@NotNull
|
||||
CalculatorEditorViewState setSelection(int selection);
|
||||
@NotNull
|
||||
CalculatorEditorViewState setSelection(int selection);
|
||||
}
|
||||
|
@@ -5,5 +5,5 @@ package org.solovyev.android.calculator;
|
||||
* Date: 21.09.12
|
||||
* Time: 13:46
|
||||
*/
|
||||
public interface CalculatorEditorChangeEventData extends Change<CalculatorEditorViewState>{
|
||||
public interface CalculatorEditorChangeEventData extends Change<CalculatorEditorViewState> {
|
||||
}
|
||||
|
@@ -9,27 +9,27 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class CalculatorEditorChangeEventDataImpl implements CalculatorEditorChangeEventData {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState oldState;
|
||||
@NotNull
|
||||
private CalculatorEditorViewState oldState;
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState newState;
|
||||
@NotNull
|
||||
private CalculatorEditorViewState newState;
|
||||
|
||||
public CalculatorEditorChangeEventDataImpl(@NotNull CalculatorEditorViewState oldState,
|
||||
@NotNull CalculatorEditorViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
public CalculatorEditorChangeEventDataImpl(@NotNull CalculatorEditorViewState oldState,
|
||||
@NotNull CalculatorEditorViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
||||
|
@@ -14,298 +14,298 @@ import org.solovyev.common.text.Strings;
|
||||
*/
|
||||
public class CalculatorEditorImpl implements CalculatorEditor {
|
||||
|
||||
@Nullable
|
||||
private CalculatorEditorView view;
|
||||
@Nullable
|
||||
private CalculatorEditorView view;
|
||||
|
||||
@NotNull
|
||||
private final Object viewLock = new Object();
|
||||
@NotNull
|
||||
private final Object viewLock = new Object();
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance();
|
||||
@NotNull
|
||||
private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance();
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEventHolder;
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEventHolder;
|
||||
|
||||
@NotNull
|
||||
private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this);
|
||||
@NotNull
|
||||
private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this);
|
||||
|
||||
public CalculatorEditorImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
}
|
||||
public CalculatorEditorImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setView(@Nullable CalculatorEditorView view) {
|
||||
synchronized (viewLock) {
|
||||
this.view = view;
|
||||
@Override
|
||||
public void setView(@Nullable CalculatorEditorView view) {
|
||||
synchronized (viewLock) {
|
||||
this.view = view;
|
||||
|
||||
if ( view != null ) {
|
||||
view.setState(lastViewState);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (view != null) {
|
||||
view.setState(lastViewState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getViewState() {
|
||||
return lastViewState;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState getViewState() {
|
||||
return lastViewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateViewState() {
|
||||
setViewState(this.lastViewState, false);
|
||||
}
|
||||
@Override
|
||||
public void updateViewState() {
|
||||
setViewState(this.lastViewState, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewState(@NotNull CalculatorEditorViewState newViewState) {
|
||||
setViewState(newViewState, true);
|
||||
}
|
||||
@Override
|
||||
public void setViewState(@NotNull CalculatorEditorViewState newViewState) {
|
||||
setViewState(newViewState, true);
|
||||
}
|
||||
|
||||
private void setViewState(@NotNull CalculatorEditorViewState newViewState, boolean majorChanges) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState oldViewState = this.lastViewState;
|
||||
private void setViewState(@NotNull CalculatorEditorViewState newViewState, boolean majorChanges) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState oldViewState = this.lastViewState;
|
||||
|
||||
this.lastViewState = newViewState;
|
||||
if (this.view != null) {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
this.lastViewState = newViewState;
|
||||
if (this.view != null) {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
|
||||
if (majorChanges) {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
} else {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed_light, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (majorChanges) {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
} else {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed_light, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
final CalculatorEventHolder.Result result = lastEventHolder.apply(calculatorEventData);
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
final CalculatorEventHolder.Result result = lastEventHolder.apply(calculatorEventData);
|
||||
|
||||
if (result.isNewAfter()) {
|
||||
switch (calculatorEventType) {
|
||||
case use_history_state:
|
||||
final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState)data;
|
||||
final EditorHistoryState editorState = calculatorHistoryState.getEditorState();
|
||||
this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (result.isNewAfter()) {
|
||||
switch (calculatorEventType) {
|
||||
case use_history_state:
|
||||
final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState) data;
|
||||
final EditorHistoryState editorState = calculatorHistoryState.getEditorState();
|
||||
this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* SELECTION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* SELECTION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditorViewState newSelectionViewState(int newSelection) {
|
||||
if (this.lastViewState.getSelection() != newSelection) {
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection);
|
||||
setViewState(result, false);
|
||||
return result;
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorEditorViewState newSelectionViewState(int newSelection) {
|
||||
if (this.lastViewState.getSelection() != newSelection) {
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection);
|
||||
setViewState(result, false);
|
||||
return result;
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnStart() {
|
||||
synchronized (viewLock) {
|
||||
return newSelectionViewState(0);
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnStart() {
|
||||
synchronized (viewLock) {
|
||||
return newSelectionViewState(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnEnd() {
|
||||
synchronized (viewLock) {
|
||||
return newSelectionViewState(this.lastViewState.getText().length());
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public CalculatorEditorViewState setCursorOnEnd() {
|
||||
synchronized (viewLock) {
|
||||
return newSelectionViewState(this.lastViewState.getText().length());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorLeft() {
|
||||
synchronized (viewLock) {
|
||||
if (this.lastViewState.getSelection() > 0) {
|
||||
return newSelectionViewState(this.lastViewState.getSelection() - 1);
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorLeft() {
|
||||
synchronized (viewLock) {
|
||||
if (this.lastViewState.getSelection() > 0) {
|
||||
return newSelectionViewState(this.lastViewState.getSelection() - 1);
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorRight() {
|
||||
synchronized (viewLock) {
|
||||
if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) {
|
||||
return newSelectionViewState(this.lastViewState.getSelection() + 1);
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public CalculatorEditorViewState moveCursorRight() {
|
||||
synchronized (viewLock) {
|
||||
if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) {
|
||||
return newSelectionViewState(this.lastViewState.getSelection() + 1);
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CursorControl asCursorControl() {
|
||||
return cursorControlAdapter;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CursorControl asCursorControl() {
|
||||
return cursorControlAdapter;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR ACTIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR ACTIONS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState erase() {
|
||||
synchronized (viewLock) {
|
||||
int selection = this.lastViewState.getSelection();
|
||||
final String text = this.lastViewState.getText();
|
||||
if (selection > 0 && text.length() > 0 && selection <= text.length()) {
|
||||
final StringBuilder newText = new StringBuilder(text.length() - 1);
|
||||
newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length()));
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState erase() {
|
||||
synchronized (viewLock) {
|
||||
int selection = this.lastViewState.getSelection();
|
||||
final String text = this.lastViewState.getText();
|
||||
if (selection > 0 && text.length() > 0 && selection <= text.length()) {
|
||||
final StringBuilder newText = new StringBuilder(text.length() - 1);
|
||||
newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length()));
|
||||
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1);
|
||||
setViewState(result);
|
||||
return result;
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1);
|
||||
setViewState(result);
|
||||
return result;
|
||||
} else {
|
||||
return this.lastViewState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState clear() {
|
||||
synchronized (viewLock) {
|
||||
return setText("");
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState clear() {
|
||||
synchronized (viewLock) {
|
||||
return setText("");
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setText(@NotNull String text) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length());
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setText(@NotNull String text) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length());
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setText(@NotNull String text, int selection) {
|
||||
synchronized (viewLock) {
|
||||
selection = correctSelection(selection, text);
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setText(@NotNull String text, int selection) {
|
||||
synchronized (viewLock) {
|
||||
selection = correctSelection(selection, text);
|
||||
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection);
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection);
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState insert(@NotNull String text) {
|
||||
synchronized (viewLock) {
|
||||
return insert(text, 0);
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState insert(@NotNull String text) {
|
||||
synchronized (viewLock) {
|
||||
return insert(text, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState insert(@NotNull String text, int selectionOffset) {
|
||||
synchronized (viewLock) {
|
||||
final int selection = this.lastViewState.getSelection();
|
||||
final String oldText = this.lastViewState.getText();
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState insert(@NotNull String text, int selectionOffset) {
|
||||
synchronized (viewLock) {
|
||||
final int selection = this.lastViewState.getSelection();
|
||||
final String oldText = this.lastViewState.getText();
|
||||
|
||||
int newTextLength = text.length() + oldText.length();
|
||||
final StringBuilder newText = new StringBuilder(newTextLength);
|
||||
int newTextLength = text.length() + oldText.length();
|
||||
final StringBuilder newText = new StringBuilder(newTextLength);
|
||||
|
||||
newText.append(oldText.substring(0, selection));
|
||||
newText.append(text);
|
||||
newText.append(oldText.substring(selection));
|
||||
newText.append(oldText.substring(0, selection));
|
||||
newText.append(text);
|
||||
newText.append(oldText.substring(selection));
|
||||
|
||||
int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength);
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection);
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength);
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection);
|
||||
setViewState(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState moveSelection(int offset) {
|
||||
synchronized (viewLock) {
|
||||
int selection = this.lastViewState.getSelection() + offset;
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState moveSelection(int offset) {
|
||||
synchronized (viewLock) {
|
||||
int selection = this.lastViewState.getSelection() + offset;
|
||||
|
||||
return setSelection(selection);
|
||||
}
|
||||
}
|
||||
return setSelection(selection);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setSelection(int selection) {
|
||||
synchronized (viewLock) {
|
||||
selection = correctSelection(selection, this.lastViewState.getText());
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditorViewState setSelection(int selection) {
|
||||
synchronized (viewLock) {
|
||||
selection = correctSelection(selection, this.lastViewState.getText());
|
||||
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection);
|
||||
setViewState(result, false);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection);
|
||||
setViewState(result, false);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static int correctSelection(int selection, @NotNull CharSequence text) {
|
||||
return correctSelection(selection, text.length());
|
||||
}
|
||||
return correctSelection(selection, text.length());
|
||||
}
|
||||
|
||||
public static int correctSelection(int selection, int textLength) {
|
||||
int result = Math.max(selection, 0);
|
||||
result = Math.min(result, textLength);
|
||||
return result;
|
||||
}
|
||||
public static int correctSelection(int selection, int textLength) {
|
||||
int result = Math.max(selection, 0);
|
||||
result = Math.min(result, textLength);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static final class CursorControlAdapter implements CursorControl {
|
||||
private static final class CursorControlAdapter implements CursorControl {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEditor calculatorEditor;
|
||||
@NotNull
|
||||
private final CalculatorEditor calculatorEditor;
|
||||
|
||||
private CursorControlAdapter(@NotNull CalculatorEditor calculatorEditor) {
|
||||
this.calculatorEditor = calculatorEditor;
|
||||
}
|
||||
private CursorControlAdapter(@NotNull CalculatorEditor calculatorEditor) {
|
||||
this.calculatorEditor = calculatorEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCursorOnStart() {
|
||||
this.calculatorEditor.setCursorOnStart();
|
||||
}
|
||||
@Override
|
||||
public void setCursorOnStart() {
|
||||
this.calculatorEditor.setCursorOnStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCursorOnEnd() {
|
||||
this.calculatorEditor.setCursorOnEnd();
|
||||
}
|
||||
@Override
|
||||
public void setCursorOnEnd() {
|
||||
this.calculatorEditor.setCursorOnEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorLeft() {
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
}
|
||||
@Override
|
||||
public void moveCursorLeft() {
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorRight() {
|
||||
this.calculatorEditor.moveCursorRight();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void moveCursorRight() {
|
||||
this.calculatorEditor.moveCursorRight();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,5 +9,5 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface CalculatorEditorView {
|
||||
|
||||
void setState(@NotNull CalculatorEditorViewState viewState);
|
||||
void setState(@NotNull CalculatorEditorViewState viewState);
|
||||
}
|
||||
|
@@ -11,8 +11,8 @@ import java.io.Serializable;
|
||||
*/
|
||||
public interface CalculatorEditorViewState extends Serializable {
|
||||
|
||||
@NotNull
|
||||
String getText();
|
||||
@NotNull
|
||||
String getText();
|
||||
|
||||
int getSelection();
|
||||
int getSelection();
|
||||
}
|
||||
|
@@ -9,49 +9,49 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState {
|
||||
|
||||
@NotNull
|
||||
private String text = "";
|
||||
@NotNull
|
||||
private String text = "";
|
||||
|
||||
private int selection = 0;
|
||||
private int selection = 0;
|
||||
|
||||
private CalculatorEditorViewStateImpl() {
|
||||
}
|
||||
private CalculatorEditorViewStateImpl() {
|
||||
}
|
||||
|
||||
public CalculatorEditorViewStateImpl(@NotNull CalculatorEditorViewState viewState) {
|
||||
this.text = viewState.getText();
|
||||
this.selection = viewState.getSelection();
|
||||
}
|
||||
public CalculatorEditorViewStateImpl(@NotNull CalculatorEditorViewState viewState) {
|
||||
this.text = viewState.getText();
|
||||
this.selection = viewState.getSelection();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return this.text;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return this.selection;
|
||||
}
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return this.selection;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newDefaultInstance() {
|
||||
return new CalculatorEditorViewStateImpl();
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newDefaultInstance() {
|
||||
return new CalculatorEditorViewStateImpl();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newSelection(@NotNull CalculatorEditorViewState viewState, int newSelection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl(viewState);
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newSelection(@NotNull CalculatorEditorViewState viewState, int newSelection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl(viewState);
|
||||
|
||||
result.selection = newSelection;
|
||||
result.selection = newSelection;
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newInstance(@NotNull String text, int selection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl();
|
||||
result.text = text;
|
||||
result.selection = selection;
|
||||
return result;
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorEditorViewState newInstance(@NotNull String text, int selection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl();
|
||||
result.text = text;
|
||||
result.selection = selection;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -17,81 +17,81 @@ import java.text.DecimalFormatSymbols;
|
||||
*/
|
||||
public interface CalculatorEngine {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* INIT
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* INIT
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
void init();
|
||||
void init();
|
||||
|
||||
void reset();
|
||||
void reset();
|
||||
|
||||
void softReset();
|
||||
void softReset();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<IConstant> getVarsRegistry();
|
||||
@NotNull
|
||||
CalculatorMathRegistry<IConstant> getVarsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Function> getFunctionsRegistry();
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Function> getFunctionsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Operator> getOperatorsRegistry();
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Operator> getOperatorsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry();
|
||||
@NotNull
|
||||
CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry();
|
||||
|
||||
@NotNull
|
||||
CalculatorMathEngine getMathEngine();
|
||||
@NotNull
|
||||
CalculatorMathEngine getMathEngine();
|
||||
|
||||
@Deprecated
|
||||
@NotNull
|
||||
MathEngine getMathEngine0();
|
||||
@Deprecated
|
||||
@NotNull
|
||||
MathEngine getMathEngine0();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
String getMultiplicationSign();
|
||||
@NotNull
|
||||
String getMultiplicationSign();
|
||||
|
||||
void setUseGroupingSeparator(boolean useGroupingSeparator);
|
||||
void setUseGroupingSeparator(boolean useGroupingSeparator);
|
||||
|
||||
void setGroupingSeparator(char groupingSeparator);
|
||||
void setGroupingSeparator(char groupingSeparator);
|
||||
|
||||
void setPrecision(@NotNull Integer precision);
|
||||
void setPrecision(@NotNull Integer precision);
|
||||
|
||||
void setRoundResult(@NotNull Boolean round);
|
||||
void setRoundResult(@NotNull Boolean round);
|
||||
|
||||
@NotNull
|
||||
AngleUnit getAngleUnits();
|
||||
@NotNull
|
||||
AngleUnit getAngleUnits();
|
||||
|
||||
void setAngleUnits(@NotNull AngleUnit angleUnits);
|
||||
void setAngleUnits(@NotNull AngleUnit angleUnits);
|
||||
|
||||
@NotNull
|
||||
NumeralBase getNumeralBase();
|
||||
@NotNull
|
||||
NumeralBase getNumeralBase();
|
||||
|
||||
void setNumeralBase(@NotNull NumeralBase numeralBase);
|
||||
void setNumeralBase(@NotNull NumeralBase numeralBase);
|
||||
|
||||
void setMultiplicationSign(@NotNull String multiplicationSign);
|
||||
void setMultiplicationSign(@NotNull String multiplicationSign);
|
||||
|
||||
void setScienceNotation(@NotNull Boolean scienceNotation);
|
||||
void setScienceNotation(@NotNull Boolean scienceNotation);
|
||||
|
||||
void setTimeout(@NotNull Integer timeout);
|
||||
void setTimeout(@NotNull Integer timeout);
|
||||
|
||||
void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols);
|
||||
void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols);
|
||||
}
|
||||
|
@@ -21,300 +21,300 @@ import java.text.DecimalFormatSymbols;
|
||||
*/
|
||||
public class CalculatorEngineImpl implements CalculatorEngine {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final String MULTIPLICATION_SIGN_DEFAULT = "×";
|
||||
private static final String MULTIPLICATION_SIGN_DEFAULT = "×";
|
||||
|
||||
private static final String MAX_CALCULATION_TIME_DEFAULT = "5";
|
||||
private static final String MAX_CALCULATION_TIME_DEFAULT = "5";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE/REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
private final MathEngine engine;
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE/REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
private final MathEngine engine;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathEngine mathEngine;
|
||||
@NotNull
|
||||
private final CalculatorMathEngine mathEngine;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<IConstant> varsRegistry;
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<IConstant> varsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Function> functionsRegistry;
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Function> functionsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Operator> operatorsRegistry;
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Operator> operatorsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Operator> postfixFunctionsRegistry;
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<Operator> postfixFunctionsRegistry;
|
||||
|
||||
@NotNull
|
||||
private final Object lock;
|
||||
@NotNull
|
||||
private final Object lock;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
private int timeout = Integer.valueOf(MAX_CALCULATION_TIME_DEFAULT);
|
||||
private int timeout = Integer.valueOf(MAX_CALCULATION_TIME_DEFAULT);
|
||||
|
||||
@NotNull
|
||||
private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT;
|
||||
@NotNull
|
||||
private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT;
|
||||
|
||||
public CalculatorEngineImpl(@NotNull JsclMathEngine engine,
|
||||
@NotNull CalculatorMathRegistry<IConstant> varsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Function> functionsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Operator> operatorsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Operator> postfixFunctionsRegistry,
|
||||
@Nullable Object lock) {
|
||||
public CalculatorEngineImpl(@NotNull JsclMathEngine engine,
|
||||
@NotNull CalculatorMathRegistry<IConstant> varsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Function> functionsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Operator> operatorsRegistry,
|
||||
@NotNull CalculatorMathRegistry<Operator> postfixFunctionsRegistry,
|
||||
@Nullable Object lock) {
|
||||
|
||||
this.engine = engine;
|
||||
this.mathEngine = new JsclCalculatorMathEngine(engine);
|
||||
this.engine = engine;
|
||||
this.mathEngine = new JsclCalculatorMathEngine(engine);
|
||||
|
||||
this.engine.setRoundResult(true);
|
||||
this.engine.setUseGroupingSeparator(true);
|
||||
this.engine.setRoundResult(true);
|
||||
this.engine.setUseGroupingSeparator(true);
|
||||
|
||||
this.varsRegistry = varsRegistry;
|
||||
this.functionsRegistry = functionsRegistry;
|
||||
this.operatorsRegistry = operatorsRegistry;
|
||||
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
|
||||
this.lock = lock == null ? new Object() : lock;
|
||||
}
|
||||
this.varsRegistry = varsRegistry;
|
||||
this.functionsRegistry = functionsRegistry;
|
||||
this.operatorsRegistry = operatorsRegistry;
|
||||
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
|
||||
this.lock = lock == null ? new Object() : lock;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<IConstant> getVarsRegistry() {
|
||||
return this.varsRegistry;
|
||||
}
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* REGISTRIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<IConstant> getVarsRegistry() {
|
||||
return this.varsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Function> getFunctionsRegistry() {
|
||||
return this.functionsRegistry;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Function> getFunctionsRegistry() {
|
||||
return this.functionsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Operator> getOperatorsRegistry() {
|
||||
return this.operatorsRegistry;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Operator> getOperatorsRegistry() {
|
||||
return this.operatorsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry() {
|
||||
return this.postfixFunctionsRegistry;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathRegistry<Operator> getPostfixFunctionsRegistry() {
|
||||
return this.postfixFunctionsRegistry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathEngine getMathEngine() {
|
||||
return this.mathEngine;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorMathEngine getMathEngine() {
|
||||
return this.mathEngine;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEngine getMathEngine0() {
|
||||
return this.engine;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEngine getMathEngine0() {
|
||||
return this.engine;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* INIT
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* INIT
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
synchronized (lock) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void init() {
|
||||
synchronized (lock) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
synchronized (lock) {
|
||||
varsRegistry.load();
|
||||
functionsRegistry.load();
|
||||
operatorsRegistry.load();
|
||||
postfixFunctionsRegistry.load();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void reset() {
|
||||
synchronized (lock) {
|
||||
varsRegistry.load();
|
||||
functionsRegistry.load();
|
||||
operatorsRegistry.load();
|
||||
postfixFunctionsRegistry.load();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void softReset() {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null);
|
||||
}
|
||||
@Override
|
||||
public void softReset() {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* PREFERENCES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMultiplicationSign() {
|
||||
return this.multiplicationSign;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMultiplicationSign() {
|
||||
return this.multiplicationSign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUseGroupingSeparator(boolean useGroupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.engine.setUseGroupingSeparator(true);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setUseGroupingSeparator(boolean useGroupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.engine.setUseGroupingSeparator(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGroupingSeparator(char groupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.engine.setGroupingSeparator(groupingSeparator);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setGroupingSeparator(char groupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.engine.setGroupingSeparator(groupingSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrecision(@NotNull Integer precision) {
|
||||
synchronized (lock) {
|
||||
this.engine.setPrecision(precision);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setPrecision(@NotNull Integer precision) {
|
||||
synchronized (lock) {
|
||||
this.engine.setPrecision(precision);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRoundResult(@NotNull Boolean round) {
|
||||
synchronized (lock) {
|
||||
this.engine.setRoundResult(round);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setRoundResult(@NotNull Boolean round) {
|
||||
synchronized (lock) {
|
||||
this.engine.setRoundResult(round);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public AngleUnit getAngleUnits() {
|
||||
synchronized (lock) {
|
||||
return this.engine.getAngleUnits();
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public AngleUnit getAngleUnits() {
|
||||
synchronized (lock) {
|
||||
return this.engine.getAngleUnits();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAngleUnits(@NotNull AngleUnit angleUnits) {
|
||||
synchronized (lock) {
|
||||
this.engine.setAngleUnits(angleUnits);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setAngleUnits(@NotNull AngleUnit angleUnits) {
|
||||
synchronized (lock) {
|
||||
this.engine.setAngleUnits(angleUnits);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public NumeralBase getNumeralBase() {
|
||||
synchronized (lock) {
|
||||
return this.engine.getNumeralBase();
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public NumeralBase getNumeralBase() {
|
||||
synchronized (lock) {
|
||||
return this.engine.getNumeralBase();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
synchronized (lock) {
|
||||
this.engine.setNumeralBase(numeralBase);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
synchronized (lock) {
|
||||
this.engine.setNumeralBase(numeralBase);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMultiplicationSign(@NotNull String multiplicationSign) {
|
||||
this.multiplicationSign = multiplicationSign;
|
||||
}
|
||||
@Override
|
||||
public void setMultiplicationSign(@NotNull String multiplicationSign) {
|
||||
this.multiplicationSign = multiplicationSign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScienceNotation(@NotNull Boolean scienceNotation) {
|
||||
synchronized (lock) {
|
||||
this.engine.setScienceNotation(scienceNotation);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setScienceNotation(@NotNull Boolean scienceNotation) {
|
||||
synchronized (lock) {
|
||||
this.engine.setScienceNotation(scienceNotation);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeout(@NotNull Integer timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
@Override
|
||||
public void setTimeout(@NotNull Integer timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) {
|
||||
synchronized (lock) {
|
||||
this.engine.setDecimalGroupSymbols(decimalGroupSymbols);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) {
|
||||
synchronized (lock) {
|
||||
this.engine.setDecimalGroupSymbols(decimalGroupSymbols);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC CLASSES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC CLASSES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final class JsclCalculatorMathEngine implements CalculatorMathEngine {
|
||||
private static final class JsclCalculatorMathEngine implements CalculatorMathEngine {
|
||||
|
||||
@NotNull
|
||||
private final MathEngine mathEngine;
|
||||
@NotNull
|
||||
private final MathEngine mathEngine;
|
||||
|
||||
private JsclCalculatorMathEngine(@NotNull MathEngine mathEngine) {
|
||||
this.mathEngine = mathEngine;
|
||||
}
|
||||
private JsclCalculatorMathEngine(@NotNull MathEngine mathEngine) {
|
||||
this.mathEngine = mathEngine;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String evaluate(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.evaluate(expression);
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String evaluate(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.evaluate(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String simplify(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.simplify(expression);
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String simplify(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.simplify(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String elementary(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.elementary(expression);
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String elementary(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.elementary(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic evaluateGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.evaluateGeneric(expression);
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic evaluateGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.evaluateGeneric(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic simplifyGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.simplifyGeneric(expression);
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic simplifyGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.simplifyGeneric(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic elementaryGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.elementaryGeneric(expression);
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Generic elementaryGeneric(@NotNull String expression) throws ParseException {
|
||||
return this.mathEngine.elementaryGeneric(expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,9 +10,9 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
*/
|
||||
public interface CalculatorEvaluationEventData extends CalculatorEventData {
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
||||
@NotNull
|
||||
String getExpression();
|
||||
@NotNull
|
||||
String getExpression();
|
||||
}
|
||||
|
@@ -10,63 +10,63 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
*/
|
||||
public class CalculatorEvaluationEventDataImpl implements CalculatorEvaluationEventData {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventData calculatorEventData;
|
||||
@NotNull
|
||||
private final CalculatorEventData calculatorEventData;
|
||||
|
||||
@NotNull
|
||||
private final JsclOperation operation;
|
||||
@NotNull
|
||||
private final JsclOperation operation;
|
||||
|
||||
@NotNull
|
||||
private final String expression;
|
||||
@NotNull
|
||||
private final String expression;
|
||||
|
||||
public CalculatorEvaluationEventDataImpl(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.operation = operation;
|
||||
this.expression = expression;
|
||||
}
|
||||
public CalculatorEvaluationEventDataImpl(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.operation = operation;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return this.expression;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return this.expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventData.getEventId();
|
||||
}
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return calculatorEventData.getEventId();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return calculatorEventData.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return calculatorEventData.getSource();
|
||||
}
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return calculatorEventData.getSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return calculatorEventData.isAfter(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isSameSequence(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return this.calculatorEventData.isAfterSequence(that);
|
||||
}
|
||||
}
|
||||
|
@@ -12,46 +12,46 @@ import java.util.List;
|
||||
*/
|
||||
public interface CalculatorEventContainer {
|
||||
|
||||
void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener);
|
||||
void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener);
|
||||
|
||||
void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener);
|
||||
void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener);
|
||||
|
||||
void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents);
|
||||
void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents);
|
||||
|
||||
public static class CalculatorEvent {
|
||||
public static class CalculatorEvent {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
@NotNull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventType calculatorEventType;
|
||||
@NotNull
|
||||
private CalculatorEventType calculatorEventType;
|
||||
|
||||
@Nullable
|
||||
private Object data;
|
||||
@Nullable
|
||||
private Object data;
|
||||
|
||||
public CalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.calculatorEventType = calculatorEventType;
|
||||
this.data = data;
|
||||
}
|
||||
public CalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.calculatorEventType = calculatorEventType;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEventData getCalculatorEventData() {
|
||||
return calculatorEventData;
|
||||
}
|
||||
@NotNull
|
||||
public CalculatorEventData getCalculatorEventData() {
|
||||
return calculatorEventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEventType getCalculatorEventType() {
|
||||
return calculatorEventType;
|
||||
}
|
||||
@NotNull
|
||||
public CalculatorEventType getCalculatorEventType() {
|
||||
return calculatorEventType;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@Nullable
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,19 +10,19 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface CalculatorEventData {
|
||||
|
||||
// the higher id => the later event
|
||||
long getEventId();
|
||||
// the higher id => the later event
|
||||
long getEventId();
|
||||
|
||||
// the higher id => the later event
|
||||
@NotNull
|
||||
Long getSequenceId();
|
||||
// the higher id => the later event
|
||||
@NotNull
|
||||
Long getSequenceId();
|
||||
|
||||
@Nullable
|
||||
Object getSource();
|
||||
@Nullable
|
||||
Object getSource();
|
||||
|
||||
boolean isAfter(@NotNull CalculatorEventData that);
|
||||
boolean isAfter(@NotNull CalculatorEventData that);
|
||||
|
||||
boolean isSameSequence(@NotNull CalculatorEventData that);
|
||||
boolean isSameSequence(@NotNull CalculatorEventData that);
|
||||
|
||||
boolean isAfterSequence(@NotNull CalculatorEventData that);
|
||||
boolean isAfterSequence(@NotNull CalculatorEventData that);
|
||||
}
|
||||
|
@@ -10,80 +10,80 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
class CalculatorEventDataImpl implements CalculatorEventData {
|
||||
|
||||
private static final long NO_SEQUENCE = -1L;
|
||||
private static final long NO_SEQUENCE = -1L;
|
||||
|
||||
private final long eventId;
|
||||
private final long eventId;
|
||||
|
||||
@NotNull
|
||||
private Long sequenceId = NO_SEQUENCE;
|
||||
@NotNull
|
||||
private Long sequenceId = NO_SEQUENCE;
|
||||
|
||||
private final Object source;
|
||||
private final Object source;
|
||||
|
||||
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId, @Nullable Object source) {
|
||||
this.eventId = id;
|
||||
this.sequenceId = sequenceId;
|
||||
this.source = source;
|
||||
}
|
||||
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId, @Nullable Object source) {
|
||||
this.eventId = id;
|
||||
this.sequenceId = sequenceId;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId, null);
|
||||
}
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId, @NotNull Object source) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId, source);
|
||||
}
|
||||
@NotNull
|
||||
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId, @NotNull Object source) {
|
||||
return new CalculatorEventDataImpl(id, sequenceId, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return this.eventId;
|
||||
}
|
||||
@Override
|
||||
public long getEventId() {
|
||||
return this.eventId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return this.sequenceId;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Long getSequenceId() {
|
||||
return this.sequenceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return this.source;
|
||||
}
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return this.eventId > that.getEventId();
|
||||
}
|
||||
@Override
|
||||
public boolean isAfter(@NotNull CalculatorEventData that) {
|
||||
return this.eventId > that.getEventId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId.equals(that.getSequenceId());
|
||||
}
|
||||
@Override
|
||||
public boolean isSameSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId.equals(that.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId > that.getSequenceId();
|
||||
}
|
||||
@Override
|
||||
public boolean isAfterSequence(@NotNull CalculatorEventData that) {
|
||||
return !this.sequenceId.equals(NO_SEQUENCE) && this.sequenceId > that.getSequenceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CalculatorEventDataImpl)) return false;
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CalculatorEventDataImpl)) return false;
|
||||
|
||||
CalculatorEventDataImpl that = (CalculatorEventDataImpl) o;
|
||||
CalculatorEventDataImpl that = (CalculatorEventDataImpl) o;
|
||||
|
||||
if (eventId != that.eventId) return false;
|
||||
if (!sequenceId.equals(that.sequenceId))
|
||||
return false;
|
||||
if (eventId != that.eventId) return false;
|
||||
if (!sequenceId.equals(that.sequenceId))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (int) (eventId ^ (eventId >>> 32));
|
||||
result = 31 * result + (sequenceId.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (int) (eventId ^ (eventId >>> 32));
|
||||
result = 31 * result + (sequenceId.hashCode());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -10,69 +10,69 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public class CalculatorEventHolder {
|
||||
|
||||
@NotNull
|
||||
private volatile CalculatorEventData lastEventData;
|
||||
@NotNull
|
||||
private volatile CalculatorEventData lastEventData;
|
||||
|
||||
public CalculatorEventHolder(@NotNull CalculatorEventData lastEventData) {
|
||||
this.lastEventData = lastEventData;
|
||||
}
|
||||
public CalculatorEventHolder(@NotNull CalculatorEventData lastEventData) {
|
||||
this.lastEventData = lastEventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public synchronized CalculatorEventData getLastEventData() {
|
||||
return lastEventData;
|
||||
}
|
||||
@NotNull
|
||||
public synchronized CalculatorEventData getLastEventData() {
|
||||
return lastEventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public synchronized Result apply(@NotNull CalculatorEventData newEventData) {
|
||||
final Result result = new Result(lastEventData, newEventData);
|
||||
@NotNull
|
||||
public synchronized Result apply(@NotNull CalculatorEventData newEventData) {
|
||||
final Result result = new Result(lastEventData, newEventData);
|
||||
|
||||
if (result.isNewAfter()) {
|
||||
this.lastEventData = newEventData;
|
||||
}
|
||||
if (result.isNewAfter()) {
|
||||
this.lastEventData = newEventData;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
public static class Result {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventData lastEventData;
|
||||
@NotNull
|
||||
private final CalculatorEventData lastEventData;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventData newEventData;
|
||||
@NotNull
|
||||
private final CalculatorEventData newEventData;
|
||||
|
||||
@Nullable
|
||||
private Boolean after = null;
|
||||
@Nullable
|
||||
private Boolean after = null;
|
||||
|
||||
@Nullable
|
||||
private Boolean sameSequence = null;
|
||||
@Nullable
|
||||
private Boolean sameSequence = null;
|
||||
|
||||
public Result(@NotNull CalculatorEventData lastEventData,
|
||||
@NotNull CalculatorEventData newEventData) {
|
||||
this.lastEventData = lastEventData;
|
||||
this.newEventData = newEventData;
|
||||
}
|
||||
public Result(@NotNull CalculatorEventData lastEventData,
|
||||
@NotNull CalculatorEventData newEventData) {
|
||||
this.lastEventData = lastEventData;
|
||||
this.newEventData = newEventData;
|
||||
}
|
||||
|
||||
public boolean isNewAfter() {
|
||||
if (after == null) {
|
||||
after = newEventData.isAfter(lastEventData);
|
||||
}
|
||||
return after;
|
||||
}
|
||||
public boolean isNewAfter() {
|
||||
if (after == null) {
|
||||
after = newEventData.isAfter(lastEventData);
|
||||
}
|
||||
return after;
|
||||
}
|
||||
|
||||
public boolean isSameSequence() {
|
||||
if (sameSequence == null) {
|
||||
sameSequence = newEventData.isSameSequence(lastEventData);
|
||||
}
|
||||
return sameSequence;
|
||||
}
|
||||
public boolean isSameSequence() {
|
||||
if (sameSequence == null) {
|
||||
sameSequence = newEventData.isSameSequence(lastEventData);
|
||||
}
|
||||
return sameSequence;
|
||||
}
|
||||
|
||||
public boolean isNewAfterSequence() {
|
||||
return newEventData.isAfterSequence(lastEventData);
|
||||
}
|
||||
public boolean isNewAfterSequence() {
|
||||
return newEventData.isAfterSequence(lastEventData);
|
||||
}
|
||||
|
||||
public boolean isNewSameOrAfterSequence() {
|
||||
return isSameSequence() || isNewAfterSequence();
|
||||
}
|
||||
}
|
||||
public boolean isNewSameOrAfterSequence() {
|
||||
return isSameSequence() || isNewAfterSequence();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,6 @@ import java.util.EventListener;
|
||||
*/
|
||||
public interface CalculatorEventListener extends EventListener {
|
||||
|
||||
void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
}
|
||||
|
@@ -9,111 +9,111 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public enum CalculatorEventType {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
* org.solovyev.android.calculator.CalculatorEvaluationEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
* org.solovyev.android.calculator.CalculatorEvaluationEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
// @NotNull CalculatorEditorViewState
|
||||
manual_calculation_requested,
|
||||
// @NotNull CalculatorEditorViewState
|
||||
manual_calculation_requested,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorOutput
|
||||
calculation_result,
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorOutput
|
||||
calculation_result,
|
||||
|
||||
calculation_cancelled,
|
||||
calculation_cancelled,
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorFailure
|
||||
calculation_failed,
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorFailure
|
||||
calculation_failed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
* CalculatorConversionEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
conversion_started,
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
* CalculatorConversionEventData
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
conversion_started,
|
||||
|
||||
// @NotNull String conversion result
|
||||
conversion_result,
|
||||
// @NotNull String conversion result
|
||||
conversion_result,
|
||||
|
||||
// @NotNull ConversionFailure
|
||||
conversion_failed,
|
||||
// @NotNull ConversionFailure
|
||||
conversion_failed,
|
||||
|
||||
conversion_finished,
|
||||
conversion_finished,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EDITOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData
|
||||
editor_state_changed,
|
||||
editor_state_changed_light,
|
||||
// @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData
|
||||
editor_state_changed,
|
||||
editor_state_changed_light,
|
||||
|
||||
// @NotNull CalculatorDisplayChangeEventData
|
||||
display_state_changed,
|
||||
// @NotNull CalculatorDisplayChangeEventData
|
||||
display_state_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
engine_preferences_changed,
|
||||
engine_preferences_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull CalculatorHistoryState
|
||||
history_state_added,
|
||||
// @NotNull CalculatorHistoryState
|
||||
history_state_added,
|
||||
|
||||
// @NotNull CalculatorHistoryState
|
||||
use_history_state,
|
||||
// @NotNull CalculatorHistoryState
|
||||
use_history_state,
|
||||
|
||||
clear_history_requested,
|
||||
clear_history_requested,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* MATH ENTITIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* MATH ENTITIES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// @NotNull IConstant
|
||||
use_constant,
|
||||
// @NotNull IConstant
|
||||
use_constant,
|
||||
|
||||
// @NotNull Function
|
||||
use_function,
|
||||
// @NotNull Function
|
||||
use_function,
|
||||
|
||||
// @NotNull Operator
|
||||
use_operator,
|
||||
// @NotNull Operator
|
||||
use_operator,
|
||||
|
||||
// @NotNull IConstant
|
||||
constant_added,
|
||||
// @NotNull IConstant
|
||||
constant_added,
|
||||
|
||||
// @NotNull Change<IConstant>
|
||||
constant_changed,
|
||||
// @NotNull Change<IConstant>
|
||||
constant_changed,
|
||||
|
||||
// @NotNull IConstant
|
||||
constant_removed,
|
||||
// @NotNull IConstant
|
||||
constant_removed,
|
||||
|
||||
|
||||
// @NotNull Function
|
||||
@@ -125,59 +125,63 @@ public enum CalculatorEventType {
|
||||
// @NotNull Change<IFunction>
|
||||
function_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* OTHER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* OTHER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// List<Message>
|
||||
calculation_messages,
|
||||
// List<Message>
|
||||
calculation_messages,
|
||||
|
||||
show_history,
|
||||
show_history_detached,
|
||||
show_history,
|
||||
show_history_detached,
|
||||
|
||||
show_functions,
|
||||
show_functions_detached,
|
||||
show_functions,
|
||||
show_functions_detached,
|
||||
|
||||
show_vars,
|
||||
show_vars_detached,
|
||||
show_vars,
|
||||
show_vars_detached,
|
||||
|
||||
open_app,
|
||||
open_app,
|
||||
|
||||
show_operators,
|
||||
show_operators_detached,
|
||||
show_operators,
|
||||
show_operators_detached,
|
||||
|
||||
show_settings,
|
||||
show_settings_detached,
|
||||
show_settings,
|
||||
show_settings_detached,
|
||||
|
||||
show_like_dialog,
|
||||
show_like_dialog,
|
||||
|
||||
show_create_var_dialog,
|
||||
show_create_matrix_dialog,
|
||||
show_create_function_dialog,
|
||||
show_create_var_dialog,
|
||||
show_create_matrix_dialog,
|
||||
show_create_function_dialog,
|
||||
|
||||
/** {@link DialogData} */
|
||||
show_message_dialog,
|
||||
/**
|
||||
* {@link DialogData}
|
||||
*/
|
||||
show_message_dialog,
|
||||
|
||||
plot_graph,
|
||||
plot_graph,
|
||||
|
||||
/** {@link org.solovyev.android.calculator.plot.PlotData} */
|
||||
plot_data_changed,
|
||||
/**
|
||||
* {@link org.solovyev.android.calculator.plot.PlotData}
|
||||
*/
|
||||
plot_data_changed,
|
||||
|
||||
//String
|
||||
show_evaluation_error;
|
||||
//String
|
||||
show_evaluation_error;
|
||||
|
||||
public boolean isOfType(@NotNull CalculatorEventType... types) {
|
||||
for (CalculatorEventType type : types) {
|
||||
if ( this == type ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public boolean isOfType(@NotNull CalculatorEventType... types) {
|
||||
for (CalculatorEventType type : types) {
|
||||
if (this == type) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -10,12 +10,12 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface CalculatorFailure {
|
||||
|
||||
@NotNull
|
||||
Exception getException();
|
||||
@NotNull
|
||||
Exception getException();
|
||||
|
||||
@Nullable
|
||||
CalculatorParseException getCalculationParseException();
|
||||
@Nullable
|
||||
CalculatorParseException getCalculationParseException();
|
||||
|
||||
@Nullable
|
||||
CalculatorEvalException getCalculationEvalException();
|
||||
@Nullable
|
||||
CalculatorEvalException getCalculationEvalException();
|
||||
}
|
||||
|
@@ -9,33 +9,33 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class CalculatorFailureImpl implements CalculatorFailure {
|
||||
|
||||
@NotNull
|
||||
private Exception exception;
|
||||
@NotNull
|
||||
private Exception exception;
|
||||
|
||||
public CalculatorFailureImpl(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
public CalculatorFailureImpl(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Exception getException() {
|
||||
return this.exception;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Exception getException() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorParseException getCalculationParseException() {
|
||||
return exception instanceof CalculatorParseException ? (CalculatorParseException)exception : null;
|
||||
}
|
||||
@Override
|
||||
public CalculatorParseException getCalculationParseException() {
|
||||
return exception instanceof CalculatorParseException ? (CalculatorParseException) exception : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorEvalException getCalculationEvalException() {
|
||||
return exception instanceof CalculatorEvalException ? (CalculatorEvalException)exception : null;
|
||||
}
|
||||
@Override
|
||||
public CalculatorEvalException getCalculationEvalException() {
|
||||
return exception instanceof CalculatorEvalException ? (CalculatorEvalException) exception : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorFailureImpl{" +
|
||||
"exception=" + exception +
|
||||
'}';
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorFailureImpl{" +
|
||||
"exception=" + exception +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@@ -15,43 +15,43 @@ import java.util.List;
|
||||
*/
|
||||
public enum CalculatorFixableError implements FixableError {
|
||||
|
||||
must_be_rad(Messages.msg_23, Messages.msg_24, Messages.msg_25) {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setAngleUnits(AngleUnit.rad);
|
||||
}
|
||||
},
|
||||
must_be_rad(Messages.msg_23, Messages.msg_24, Messages.msg_25) {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setAngleUnits(AngleUnit.rad);
|
||||
}
|
||||
},
|
||||
|
||||
preferred_numeral_base() {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setPreferredNumeralBase();
|
||||
}
|
||||
},
|
||||
preferred_numeral_base() {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setPreferredNumeralBase();
|
||||
}
|
||||
},
|
||||
|
||||
preferred_angle_units() {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setPreferredAngleUnits();
|
||||
}
|
||||
};
|
||||
preferred_angle_units() {
|
||||
@Override
|
||||
public void fix() {
|
||||
Locator.getInstance().getPreferenceService().setPreferredAngleUnits();
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private final List<String> messageCodes;
|
||||
@NotNull
|
||||
private final List<String> messageCodes;
|
||||
|
||||
CalculatorFixableError(@Nullable String... messageCodes) {
|
||||
this.messageCodes = Collections.asList(messageCodes);
|
||||
}
|
||||
CalculatorFixableError(@Nullable String... messageCodes) {
|
||||
this.messageCodes = Collections.asList(messageCodes);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CalculatorFixableError getErrorByMessageCode(@NotNull String messageCode) {
|
||||
for (CalculatorFixableError fixableError : values()) {
|
||||
if (fixableError.messageCodes.contains(messageCode)) {
|
||||
return fixableError;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Nullable
|
||||
public static CalculatorFixableError getErrorByMessageCode(@NotNull String messageCode) {
|
||||
for (CalculatorFixableError fixableError : values()) {
|
||||
if (fixableError.messageCodes.contains(messageCode)) {
|
||||
return fixableError;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
|
@@ -31,8 +31,9 @@ import java.util.Map;
|
||||
*/
|
||||
public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegistry<Function, AFunction> {
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
|
||||
static {
|
||||
substitutes.put("√", "sqrt");
|
||||
}
|
||||
@@ -41,21 +42,21 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
|
||||
|
||||
public CalculatorFunctionsMathRegistry(@NotNull MathRegistry<Function> functionsRegistry,
|
||||
@NotNull MathEntityDao<AFunction> mathEntityDao) {
|
||||
@NotNull MathEntityDao<AFunction> mathEntityDao) {
|
||||
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
|
||||
add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
|
||||
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
add(new CustomFunction.Builder(true, "re", Arrays.asList("x"), "(x+conjugate(x))/2"));
|
||||
add(new CustomFunction.Builder(true, "im", Arrays.asList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
|
||||
add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
|
||||
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
add(new CustomFunction.Builder(true, "re", Arrays.asList("x"), "(x+conjugate(x))/2"));
|
||||
add(new CustomFunction.Builder(true, "im", Arrays.asList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
|
||||
super.load();
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry,
|
||||
@NotNull MathEntityBuilder<? extends Function> builder,
|
||||
@@ -80,16 +81,16 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Function function) {
|
||||
for (FunctionCategory category : FunctionCategory.values()) {
|
||||
if ( category.isInCategory(function) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public String getCategory(@NotNull Function function) {
|
||||
for (FunctionCategory category : FunctionCategory.values()) {
|
||||
if (category.isInCategory(function)) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@@ -97,11 +98,11 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
final Function function = get(functionName);
|
||||
|
||||
String result = null;
|
||||
if ( function instanceof CustomFunction ) {
|
||||
if (function instanceof CustomFunction) {
|
||||
result = ((CustomFunction) function).getDescription();
|
||||
}
|
||||
|
||||
if (Strings.isEmpty(result) ) {
|
||||
if (Strings.isEmpty(result)) {
|
||||
result = super.getDescription(functionName);
|
||||
}
|
||||
|
||||
@@ -115,7 +116,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
return new FunctionBuilderAdapter(new AFunction.Builder(function));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
protected AFunction transform(@NotNull Function function) {
|
||||
if (function instanceof CustomFunction) {
|
||||
return AFunction.fromIFunction((CustomFunction) function);
|
||||
|
@@ -38,185 +38,185 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
*/
|
||||
public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
// one minute
|
||||
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
||||
// one minute
|
||||
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||
@NotNull
|
||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||
|
||||
@NotNull
|
||||
private final AtomicLong counter = new AtomicLong(CalculatorUtils.FIRST_ID);
|
||||
@NotNull
|
||||
private final AtomicLong counter = new AtomicLong(CalculatorUtils.FIRST_ID);
|
||||
|
||||
@NotNull
|
||||
private final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance();
|
||||
@NotNull
|
||||
private final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance();
|
||||
|
||||
@NotNull
|
||||
private final Executor calculationsExecutor = Executors.newFixedThreadPool(10);
|
||||
@NotNull
|
||||
private final Executor calculationsExecutor = Executors.newFixedThreadPool(10);
|
||||
|
||||
// NOTE: only one thread is responsible for events as all events must be done in order of their creating
|
||||
@NotNull
|
||||
private final Executor eventExecutor = Executors.newFixedThreadPool(1);
|
||||
// NOTE: only one thread is responsible for events as all events must be done in order of their creating
|
||||
@NotNull
|
||||
private final Executor eventExecutor = Executors.newFixedThreadPool(1);
|
||||
|
||||
private volatile boolean calculateOnFly = true;
|
||||
private volatile boolean calculateOnFly = true;
|
||||
|
||||
private volatile long lastPreferenceCheck = 0L;
|
||||
private volatile long lastPreferenceCheck = 0L;
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public CalculatorImpl() {
|
||||
this.addCalculatorEventListener(this);
|
||||
}
|
||||
public CalculatorImpl() {
|
||||
this.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData() {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData(@NotNull Object source) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId, source);
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData(@NotNull Object source) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId, source);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData(@NotNull Long sequenceId) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, sequenceId);
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorEventData nextEventData(@NotNull Long sequenceId) {
|
||||
long eventId = counter.incrementAndGet();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, sequenceId);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CALCULATION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
@Override
|
||||
public void evaluate() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate(@NotNull Long sequenceId) {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId);
|
||||
}
|
||||
@Override
|
||||
public void evaluate(@NotNull Long sequenceId) {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simplify() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
@Override
|
||||
public void simplify() {
|
||||
final CalculatorEditorViewState viewState = getEditor().getViewState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation,
|
||||
@NotNull final String expression) {
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation,
|
||||
@NotNull final String expression) {
|
||||
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventDataId = nextEventData(sequenceId);
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData evaluate(@NotNull final JsclOperation operation, @NotNull final String expression, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventDataId = nextEventData(sequenceId);
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CalculatorImpl.this.evaluate(eventDataId.getSequenceId(), operation, expression, null);
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
Locator.getInstance().getEngine().init();
|
||||
Locator.getInstance().getHistory().load();
|
||||
}
|
||||
@Override
|
||||
public void init() {
|
||||
Locator.getInstance().getEngine().init();
|
||||
Locator.getInstance().getHistory().load();
|
||||
}
|
||||
|
||||
public void setCalculateOnFly(boolean calculateOnFly) {
|
||||
this.calculateOnFly = calculateOnFly;
|
||||
}
|
||||
public void setCalculateOnFly(boolean calculateOnFly) {
|
||||
this.calculateOnFly = calculateOnFly;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorConversionEventData newConversionEventData(@NotNull Long sequenceId,
|
||||
@NotNull Generic value,
|
||||
@NotNull NumeralBase from,
|
||||
@NotNull NumeralBase to,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState);
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorConversionEventData newConversionEventData(@NotNull Long sequenceId,
|
||||
@NotNull Generic value,
|
||||
@NotNull NumeralBase from,
|
||||
@NotNull NumeralBase to,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState);
|
||||
}
|
||||
|
||||
private void evaluate(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr) {
|
||||
private void evaluate(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr) {
|
||||
|
||||
checkPreferredPreferences();
|
||||
checkPreferredPreferences();
|
||||
|
||||
PreparedExpression preparedExpression = null;
|
||||
PreparedExpression preparedExpression = null;
|
||||
|
||||
try {
|
||||
try {
|
||||
|
||||
expression = expression.trim();
|
||||
expression = expression.trim();
|
||||
|
||||
if (Strings.isEmpty(expression)) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
||||
} else {
|
||||
if (Strings.isEmpty(expression)) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
||||
} else {
|
||||
preparedExpression = prepareExpression(expression);
|
||||
|
||||
final String jsclExpression = preparedExpression.toString();
|
||||
|
||||
try {
|
||||
try {
|
||||
|
||||
final CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
|
||||
|
||||
@@ -225,378 +225,378 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
final Generic result = operation.evaluateGeneric(jsclExpression, mathEngine);
|
||||
|
||||
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||
result.toString();
|
||||
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||
result.toString();
|
||||
|
||||
if (messageRegistry.hasMessage()) {
|
||||
final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
try {
|
||||
final List<Message> messages = new ArrayList<Message>();
|
||||
while (messageRegistry.hasMessage()) {
|
||||
messages.add(messageRegistry.getMessage());
|
||||
}
|
||||
if (!messages.isEmpty()) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// todo serso: not good but we need proper synchronization
|
||||
logger.error("Calculator", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
if (messageRegistry.hasMessage()) {
|
||||
final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
try {
|
||||
final List<Message> messages = new ArrayList<Message>();
|
||||
while (messageRegistry.hasMessage()) {
|
||||
messages.add(messageRegistry.getMessage());
|
||||
}
|
||||
if (!messages.isEmpty()) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// todo serso: not good but we need proper synchronization
|
||||
logger.error("Calculator", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
|
||||
final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
|
||||
|
||||
} catch (AbstractJsclArithmeticException e) {
|
||||
handleException(sequenceId, operation, expression, mr, new CalculatorEvalException(e, e, jsclExpression));
|
||||
}
|
||||
}
|
||||
} catch (AbstractJsclArithmeticException e) {
|
||||
handleException(sequenceId, operation, expression, mr, new CalculatorEvalException(e, e, jsclExpression));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ArithmeticException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
|
||||
} catch (StackOverflowError e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
|
||||
} catch (jscl.text.ParseException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
|
||||
} catch (ParseInterruptedException e) {
|
||||
} catch (ArithmeticException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
|
||||
} catch (StackOverflowError e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
|
||||
} catch (jscl.text.ParseException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
|
||||
} catch (ParseInterruptedException e) {
|
||||
|
||||
// do nothing - we ourselves interrupt the calculations
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
|
||||
// do nothing - we ourselves interrupt the calculations
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
|
||||
|
||||
} catch (CalculatorParseException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, e);
|
||||
}
|
||||
}
|
||||
} catch (CalculatorParseException e) {
|
||||
handleException(sequenceId, operation, expression, mr, preparedExpression, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkPreferredPreferences() {
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
private void checkPreferredPreferences() {
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
|
||||
if ( currentTime - lastPreferenceCheck > PREFERENCE_CHECK_INTERVAL ) {
|
||||
lastPreferenceCheck = currentTime;
|
||||
Locator.getInstance().getPreferenceService().checkPreferredPreferences(false);
|
||||
}
|
||||
}
|
||||
if (currentTime - lastPreferenceCheck > PREFERENCE_CHECK_INTERVAL) {
|
||||
lastPreferenceCheck = currentTime;
|
||||
Locator.getInstance().getPreferenceService().checkPreferredPreferences(false);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
@Override
|
||||
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
||||
return preprocessor.process(expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long calculationId) {
|
||||
return new CalculatorEvaluationEventDataImpl(nextEventData(calculationId), operation, expression);
|
||||
}
|
||||
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@NotNull Long calculationId) {
|
||||
return new CalculatorEvaluationEventDataImpl(nextEventData(calculationId), operation, expression);
|
||||
}
|
||||
|
||||
private void handleException(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr,
|
||||
@Nullable PreparedExpression preparedExpression,
|
||||
@NotNull CalculatorParseException parseException) {
|
||||
private void handleException(@NotNull Long sequenceId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr,
|
||||
@Nullable PreparedExpression preparedExpression,
|
||||
@NotNull CalculatorParseException parseException) {
|
||||
|
||||
if (operation == JsclOperation.numeric
|
||||
&& preparedExpression != null
|
||||
&& preparedExpression.isExistsUndefinedVar()) {
|
||||
if (operation == JsclOperation.numeric
|
||||
&& preparedExpression != null
|
||||
&& preparedExpression.isExistsUndefinedVar()) {
|
||||
|
||||
evaluate(sequenceId, JsclOperation.simplify, expression, mr);
|
||||
} else {
|
||||
evaluate(sequenceId, JsclOperation.simplify, expression, mr);
|
||||
} else {
|
||||
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(parseException));
|
||||
}
|
||||
}
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(parseException));
|
||||
}
|
||||
}
|
||||
|
||||
private void handleException(@NotNull Long calculationId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr,
|
||||
@NotNull CalculatorEvalException evalException) {
|
||||
private void handleException(@NotNull Long calculationId,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry mr,
|
||||
@NotNull CalculatorEvalException evalException) {
|
||||
|
||||
if (operation == JsclOperation.numeric && evalException.getCause() instanceof NumeralBaseException) {
|
||||
evaluate(calculationId, JsclOperation.simplify, expression, mr);
|
||||
} else {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(evalException));
|
||||
}
|
||||
}
|
||||
if (operation == JsclOperation.numeric && evalException.getCause() instanceof NumeralBaseException) {
|
||||
evaluate(calculationId, JsclOperation.simplify, expression, mr);
|
||||
} else {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(evalException));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONVERSION
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData convert(@NotNull final Generic value,
|
||||
@NotNull final NumeralBase to) {
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData convert(@NotNull final Generic value,
|
||||
@NotNull final NumeralBase to) {
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase();
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase();
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Long sequenceId = eventDataId.getSequenceId();
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Long sequenceId = eventDataId.getSequenceId();
|
||||
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_started, null);
|
||||
try {
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_started, null);
|
||||
try {
|
||||
|
||||
final String result = doConversion(value, from, to);
|
||||
final String result = doConversion(value, from, to);
|
||||
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_result, result);
|
||||
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));
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (ConversionException e) {
|
||||
fireCalculatorEvent(newConversionEventData(sequenceId, value, from, to, displayViewState), CalculatorEventType.conversion_failed, new ConversionFailureImpl(e));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return eventDataId;
|
||||
}
|
||||
return eventDataId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String doConversion(@NotNull Generic generic,
|
||||
@NotNull NumeralBase from,
|
||||
@NotNull NumeralBase to) throws ConversionException {
|
||||
final String result;
|
||||
@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 (!Strings.isEmpty(fromString)) {
|
||||
try {
|
||||
fromString = ToJsclTextProcessor.getInstance().process(fromString).getExpression();
|
||||
} catch (CalculatorParseException e) {
|
||||
// ok, problems while processing occurred
|
||||
}
|
||||
}
|
||||
if (from != to) {
|
||||
String fromString = generic.toString();
|
||||
if (!Strings.isEmpty(fromString)) {
|
||||
try {
|
||||
fromString = ToJsclTextProcessor.getInstance().process(fromString).getExpression();
|
||||
} catch (CalculatorParseException e) {
|
||||
// ok, problems while processing occurred
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
result = Conversions.doConversion(CalculatorNumeralBase.getConverter(), fromString, CalculatorNumeralBase.valueOf(from), CalculatorNumeralBase.valueOf(to));
|
||||
} else {
|
||||
result = generic.toString();
|
||||
}
|
||||
result = Conversions.doConversion(CalculatorNumeralBase.getConverter(), fromString, CalculatorNumeralBase.valueOf(from), CalculatorNumeralBase.valueOf(to));
|
||||
} else {
|
||||
result = generic.toString();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isConversionPossible(@NotNull Generic generic, NumeralBase from, @NotNull NumeralBase to) {
|
||||
try {
|
||||
doConversion(generic, from, to);
|
||||
return true;
|
||||
} catch (ConversionException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculatorEventContainer.addCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculatorEventContainer.addCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculatorEventContainer.removeCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
calculatorEventContainer.removeCalculatorEventListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull final CalculatorEventData calculatorEventData, @NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
eventExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull final CalculatorEventData calculatorEventData, @NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
eventExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull final List<CalculatorEvent> calculatorEvents) {
|
||||
eventExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull final List<CalculatorEvent> calculatorEvents) {
|
||||
eventExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
final CalculatorEventData eventData = nextEventData();
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
final CalculatorEventData eventData = nextEventData();
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
return eventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Object source) {
|
||||
final CalculatorEventData eventData = nextEventData(source);
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Object source) {
|
||||
final CalculatorEventData eventData = nextEventData(source);
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
return eventData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventData = nextEventData(sequenceId);
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Long sequenceId) {
|
||||
final CalculatorEventData eventData = nextEventData(sequenceId);
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
return eventData;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS HANDLER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* EVENTS HANDLER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
|
||||
switch (calculatorEventType) {
|
||||
case editor_state_changed:
|
||||
if (calculateOnFly) {
|
||||
final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data;
|
||||
switch (calculatorEventType) {
|
||||
case editor_state_changed:
|
||||
if (calculateOnFly) {
|
||||
final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data;
|
||||
|
||||
final String newText = editorChangeEventData.getNewValue().getText();
|
||||
final String oldText = editorChangeEventData.getOldValue().getText();
|
||||
final String newText = editorChangeEventData.getNewValue().getText();
|
||||
final String oldText = editorChangeEventData.getOldValue().getText();
|
||||
|
||||
if (!newText.equals(oldText)) {
|
||||
evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
|
||||
}
|
||||
}
|
||||
break;
|
||||
if (!newText.equals(oldText)) {
|
||||
evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case display_state_changed:
|
||||
onDisplayStateChanged((CalculatorDisplayChangeEventData) data);
|
||||
break;
|
||||
case display_state_changed:
|
||||
onDisplayStateChanged((CalculatorDisplayChangeEventData) data);
|
||||
break;
|
||||
|
||||
case constant_changed:
|
||||
final IConstant newConstant = ((Change<IConstant>)data) .getNewValue();
|
||||
if (!newConstant.getName().equals(CalculatorVarsRegistry.ANS)) {
|
||||
evaluate();
|
||||
}
|
||||
break;
|
||||
case constant_changed:
|
||||
final IConstant newConstant = ((Change<IConstant>) data).getNewValue();
|
||||
if (!newConstant.getName().equals(CalculatorVarsRegistry.ANS)) {
|
||||
evaluate();
|
||||
}
|
||||
break;
|
||||
|
||||
case constant_added:
|
||||
case constant_removed:
|
||||
case function_added:
|
||||
case function_changed:
|
||||
case function_removed:
|
||||
evaluate();
|
||||
break;
|
||||
case constant_added:
|
||||
case constant_removed:
|
||||
case function_added:
|
||||
case function_changed:
|
||||
case function_removed:
|
||||
evaluate();
|
||||
break;
|
||||
|
||||
case engine_preferences_changed:
|
||||
evaluate(calculatorEventData.getSequenceId());
|
||||
break;
|
||||
case engine_preferences_changed:
|
||||
evaluate(calculatorEventData.getSequenceId());
|
||||
break;
|
||||
|
||||
case use_constant:
|
||||
final IConstant constant = (IConstant)data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(constant.getName());
|
||||
break;
|
||||
case use_constant:
|
||||
final IConstant constant = (IConstant) data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(constant.getName());
|
||||
break;
|
||||
|
||||
case use_operator:
|
||||
final Operator operator = (Operator)data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(operator.getName());
|
||||
break;
|
||||
case use_operator:
|
||||
final Operator operator = (Operator) data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(operator.getName());
|
||||
break;
|
||||
|
||||
case use_function:
|
||||
final Function function = (Function)data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(function.getName());
|
||||
break;
|
||||
case use_function:
|
||||
final Function function = (Function) data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(function.getName());
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onDisplayStateChanged(@NotNull CalculatorDisplayChangeEventData displayChangeEventData) {
|
||||
final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue();
|
||||
if (newState.isValid()) {
|
||||
final String result = newState.getStringResult();
|
||||
if ( !Strings.isEmpty(result) ) {
|
||||
final CalculatorMathRegistry<IConstant> varsRegistry = Locator.getInstance().getEngine().getVarsRegistry();
|
||||
final IConstant ansVar = varsRegistry.get(CalculatorVarsRegistry.ANS);
|
||||
private void onDisplayStateChanged(@NotNull CalculatorDisplayChangeEventData displayChangeEventData) {
|
||||
final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue();
|
||||
if (newState.isValid()) {
|
||||
final String result = newState.getStringResult();
|
||||
if (!Strings.isEmpty(result)) {
|
||||
final CalculatorMathRegistry<IConstant> varsRegistry = Locator.getInstance().getEngine().getVarsRegistry();
|
||||
final IConstant ansVar = varsRegistry.get(CalculatorVarsRegistry.ANS);
|
||||
|
||||
final Var.Builder varBuilder;
|
||||
if (ansVar != null) {
|
||||
varBuilder = new Var.Builder(ansVar);
|
||||
} else {
|
||||
varBuilder = new Var.Builder();
|
||||
}
|
||||
final Var.Builder varBuilder;
|
||||
if (ansVar != null) {
|
||||
varBuilder = new Var.Builder(ansVar);
|
||||
} else {
|
||||
varBuilder = new Var.Builder();
|
||||
}
|
||||
|
||||
varBuilder.setName(CalculatorVarsRegistry.ANS);
|
||||
varBuilder.setValue(result);
|
||||
varBuilder.setDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description));
|
||||
varBuilder.setName(CalculatorVarsRegistry.ANS);
|
||||
varBuilder.setValue(result);
|
||||
varBuilder.setDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description));
|
||||
|
||||
CalculatorVarsRegistry.saveVariable(varsRegistry, varBuilder, ansVar, this, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
CalculatorVarsRegistry.saveVariable(varsRegistry, varBuilder, ansVar, this, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* HISTORY
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = Locator.getInstance().getHistory();
|
||||
if (history.isActionAvailable(historyAction)) {
|
||||
final CalculatorHistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
|
||||
if (newState != null) {
|
||||
setCurrentHistoryState(newState);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void doHistoryAction(@NotNull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = Locator.getInstance().getHistory();
|
||||
if (history.isActionAvailable(historyAction)) {
|
||||
final CalculatorHistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
|
||||
if (newState != null) {
|
||||
setCurrentHistoryState(newState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) {
|
||||
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
|
||||
}
|
||||
@Override
|
||||
public void setCurrentHistoryState(@NotNull CalculatorHistoryState editorHistoryState) {
|
||||
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return CalculatorHistoryState.newInstance(getEditor(), getDisplay());
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return CalculatorHistoryState.newInstance(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* OTHER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* OTHER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor getEditor() {
|
||||
return Locator.getInstance().getEditor();
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorEditor getEditor() {
|
||||
return Locator.getInstance().getEditor();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplay getDisplay() {
|
||||
return Locator.getInstance().getDisplay();
|
||||
}
|
||||
@NotNull
|
||||
private CalculatorDisplay getDisplay() {
|
||||
return Locator.getInstance().getDisplay();
|
||||
}
|
||||
}
|
||||
|
@@ -10,9 +10,9 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
*/
|
||||
public interface CalculatorInput {
|
||||
|
||||
@NotNull
|
||||
String getExpression();
|
||||
@NotNull
|
||||
String getExpression();
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
}
|
||||
|
@@ -10,26 +10,26 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
*/
|
||||
public class CalculatorInputImpl implements CalculatorInput {
|
||||
|
||||
@NotNull
|
||||
private String expression;
|
||||
@NotNull
|
||||
private String expression;
|
||||
|
||||
@NotNull
|
||||
private JsclOperation operation;
|
||||
@NotNull
|
||||
private JsclOperation operation;
|
||||
|
||||
public CalculatorInputImpl(@NotNull String expression, @NotNull JsclOperation operation) {
|
||||
this.expression = expression;
|
||||
this.operation = operation;
|
||||
}
|
||||
public CalculatorInputImpl(@NotNull String expression, @NotNull JsclOperation operation) {
|
||||
this.expression = expression;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JsclOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public JsclOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
|
@@ -9,17 +9,17 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface CalculatorKeyboard {
|
||||
|
||||
void buttonPressed(@Nullable String text);
|
||||
void buttonPressed(@Nullable String text);
|
||||
|
||||
void roundBracketsButtonPressed();
|
||||
void roundBracketsButtonPressed();
|
||||
|
||||
void pasteButtonPressed();
|
||||
void pasteButtonPressed();
|
||||
|
||||
void clearButtonPressed();
|
||||
void clearButtonPressed();
|
||||
|
||||
void copyButtonPressed();
|
||||
void copyButtonPressed();
|
||||
|
||||
void moveCursorLeft();
|
||||
void moveCursorLeft();
|
||||
|
||||
void moveCursorRight();
|
||||
void moveCursorRight();
|
||||
}
|
||||
|
@@ -12,113 +12,113 @@ import org.solovyev.common.text.Strings;
|
||||
*/
|
||||
public class CalculatorKeyboardImpl implements CalculatorKeyboard {
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
public CalculatorKeyboardImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
}
|
||||
public CalculatorKeyboardImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buttonPressed(@Nullable final String text) {
|
||||
@Override
|
||||
public void buttonPressed(@Nullable final String text) {
|
||||
|
||||
if (!Strings.isEmpty(text)) {
|
||||
assert text != null;
|
||||
if (!Strings.isEmpty(text)) {
|
||||
assert text != null;
|
||||
|
||||
// process special buttons
|
||||
boolean processed = processSpecialButtons(text);
|
||||
// process special buttons
|
||||
boolean processed = processSpecialButtons(text);
|
||||
|
||||
if (!processed) {
|
||||
int cursorPositionOffset = 0;
|
||||
final StringBuilder textToBeInserted = new StringBuilder(text);
|
||||
if (!processed) {
|
||||
int cursorPositionOffset = 0;
|
||||
final StringBuilder textToBeInserted = new StringBuilder(text);
|
||||
|
||||
final MathType.Result mathType = MathType.getType(text, 0, false);
|
||||
switch (mathType.getMathType()) {
|
||||
case function:
|
||||
textToBeInserted.append("()");
|
||||
cursorPositionOffset = -1;
|
||||
break;
|
||||
case operator:
|
||||
textToBeInserted.append("()");
|
||||
cursorPositionOffset = -1;
|
||||
break;
|
||||
case comma:
|
||||
textToBeInserted.append(" ");
|
||||
break;
|
||||
}
|
||||
final MathType.Result mathType = MathType.getType(text, 0, false);
|
||||
switch (mathType.getMathType()) {
|
||||
case function:
|
||||
textToBeInserted.append("()");
|
||||
cursorPositionOffset = -1;
|
||||
break;
|
||||
case operator:
|
||||
textToBeInserted.append("()");
|
||||
cursorPositionOffset = -1;
|
||||
break;
|
||||
case comma:
|
||||
textToBeInserted.append(" ");
|
||||
break;
|
||||
}
|
||||
|
||||
if (cursorPositionOffset == 0) {
|
||||
if (MathType.openGroupSymbols.contains(text)) {
|
||||
cursorPositionOffset = -1;
|
||||
}
|
||||
}
|
||||
if (cursorPositionOffset == 0) {
|
||||
if (MathType.openGroupSymbols.contains(text)) {
|
||||
cursorPositionOffset = -1;
|
||||
}
|
||||
}
|
||||
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
editor.insert(textToBeInserted.toString(), cursorPositionOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
editor.insert(textToBeInserted.toString(), cursorPositionOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean processSpecialButtons(@NotNull String text) {
|
||||
boolean result = false;
|
||||
private boolean processSpecialButtons(@NotNull String text) {
|
||||
boolean result = false;
|
||||
|
||||
final CalculatorSpecialButton button = CalculatorSpecialButton.getByActionCode(text);
|
||||
if ( button != null ) {
|
||||
button.onClick(this);
|
||||
result = true;
|
||||
}
|
||||
final CalculatorSpecialButton button = CalculatorSpecialButton.getByActionCode(text);
|
||||
if (button != null) {
|
||||
button.onClick(this);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void roundBracketsButtonPressed() {
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
CalculatorEditorViewState viewState = editor.getViewState();
|
||||
@Override
|
||||
public void roundBracketsButtonPressed() {
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
CalculatorEditorViewState viewState = editor.getViewState();
|
||||
|
||||
final int cursorPosition = viewState.getSelection();
|
||||
final String oldText = viewState.getText();
|
||||
final int cursorPosition = viewState.getSelection();
|
||||
final String oldText = viewState.getText();
|
||||
|
||||
final StringBuilder newText = new StringBuilder(oldText.length() + 2);
|
||||
newText.append("(");
|
||||
newText.append(oldText.substring(0, cursorPosition));
|
||||
newText.append(")");
|
||||
newText.append(oldText.substring(cursorPosition));
|
||||
editor.setText(newText.toString(), cursorPosition + 2);
|
||||
}
|
||||
final StringBuilder newText = new StringBuilder(oldText.length() + 2);
|
||||
newText.append("(");
|
||||
newText.append(oldText.substring(0, cursorPosition));
|
||||
newText.append(")");
|
||||
newText.append(oldText.substring(cursorPosition));
|
||||
editor.setText(newText.toString(), cursorPosition + 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pasteButtonPressed() {
|
||||
final String text = Locator.getInstance().getClipboard().getText();
|
||||
if (text != null) {
|
||||
Locator.getInstance().getEditor().insert(text);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void pasteButtonPressed() {
|
||||
final String text = Locator.getInstance().getClipboard().getText();
|
||||
if (text != null) {
|
||||
Locator.getInstance().getEditor().insert(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearButtonPressed() {
|
||||
Locator.getInstance().getEditor().clear();
|
||||
}
|
||||
@Override
|
||||
public void clearButtonPressed() {
|
||||
Locator.getInstance().getEditor().clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copyButtonPressed() {
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
if (displayViewState.isValid()) {
|
||||
final CharSequence text = displayViewState.getText();
|
||||
if (!Strings.isEmpty(text)) {
|
||||
Locator.getInstance().getClipboard().setText(text);
|
||||
Locator.getInstance().getNotifier().showMessage(CalculatorMessage.newInfoMessage(CalculatorMessages.result_copied));
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void copyButtonPressed() {
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
if (displayViewState.isValid()) {
|
||||
final CharSequence text = displayViewState.getText();
|
||||
if (!Strings.isEmpty(text)) {
|
||||
Locator.getInstance().getClipboard().setText(text);
|
||||
Locator.getInstance().getNotifier().showMessage(CalculatorMessage.newInfoMessage(CalculatorMessages.result_copied));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorLeft() {
|
||||
Locator.getInstance().getEditor().moveCursorLeft();
|
||||
}
|
||||
@Override
|
||||
public void moveCursorLeft() {
|
||||
Locator.getInstance().getEditor().moveCursorLeft();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveCursorRight() {
|
||||
Locator.getInstance().getEditor().moveCursorRight();
|
||||
}
|
||||
@Override
|
||||
public void moveCursorRight() {
|
||||
Locator.getInstance().getEditor().moveCursorRight();
|
||||
}
|
||||
}
|
||||
|
@@ -12,49 +12,49 @@ import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||
*/
|
||||
public interface CalculatorLocator {
|
||||
|
||||
void init(@NotNull Calculator calculator,
|
||||
@NotNull CalculatorEngine engine,
|
||||
@NotNull CalculatorClipboard clipboard,
|
||||
@NotNull CalculatorNotifier notifier,
|
||||
@NotNull CalculatorHistory history,
|
||||
@NotNull CalculatorLogger logger,
|
||||
@NotNull CalculatorPreferenceService preferenceService,
|
||||
@NotNull CalculatorKeyboard keyboard,
|
||||
@NotNull CalculatorExternalListenersContainer externalListenersContainer,
|
||||
@NotNull CalculatorPlotter plotter);
|
||||
void init(@NotNull Calculator calculator,
|
||||
@NotNull CalculatorEngine engine,
|
||||
@NotNull CalculatorClipboard clipboard,
|
||||
@NotNull CalculatorNotifier notifier,
|
||||
@NotNull CalculatorHistory history,
|
||||
@NotNull CalculatorLogger logger,
|
||||
@NotNull CalculatorPreferenceService preferenceService,
|
||||
@NotNull CalculatorKeyboard keyboard,
|
||||
@NotNull CalculatorExternalListenersContainer externalListenersContainer,
|
||||
@NotNull CalculatorPlotter plotter);
|
||||
|
||||
@NotNull
|
||||
Calculator getCalculator();
|
||||
@NotNull
|
||||
Calculator getCalculator();
|
||||
|
||||
@NotNull
|
||||
CalculatorEngine getEngine();
|
||||
@NotNull
|
||||
CalculatorEngine getEngine();
|
||||
|
||||
@NotNull
|
||||
CalculatorDisplay getDisplay();
|
||||
@NotNull
|
||||
CalculatorDisplay getDisplay();
|
||||
|
||||
@NotNull
|
||||
CalculatorEditor getEditor();
|
||||
@NotNull
|
||||
CalculatorEditor getEditor();
|
||||
|
||||
@NotNull
|
||||
CalculatorKeyboard getKeyboard();
|
||||
@NotNull
|
||||
CalculatorKeyboard getKeyboard();
|
||||
|
||||
@NotNull
|
||||
CalculatorClipboard getClipboard();
|
||||
@NotNull
|
||||
CalculatorClipboard getClipboard();
|
||||
|
||||
@NotNull
|
||||
CalculatorNotifier getNotifier();
|
||||
@NotNull
|
||||
CalculatorNotifier getNotifier();
|
||||
|
||||
@NotNull
|
||||
CalculatorHistory getHistory();
|
||||
@NotNull
|
||||
CalculatorHistory getHistory();
|
||||
|
||||
@NotNull
|
||||
CalculatorLogger getLogger();
|
||||
@NotNull
|
||||
CalculatorLogger getLogger();
|
||||
|
||||
@NotNull
|
||||
CalculatorPlotter getPlotter();
|
||||
@NotNull
|
||||
CalculatorPlotter getPlotter();
|
||||
|
||||
@NotNull
|
||||
CalculatorPreferenceService getPreferenceService();
|
||||
@NotNull
|
||||
CalculatorPreferenceService getPreferenceService();
|
||||
|
||||
@NotNull
|
||||
CalculatorExternalListenersContainer getExternalListenersContainer();
|
||||
|
@@ -10,12 +10,12 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface CalculatorLogger {
|
||||
|
||||
void debug(@Nullable String tag, @NotNull String message);
|
||||
void debug(@Nullable String tag, @NotNull String message);
|
||||
|
||||
void debug(@Nullable String tag, @Nullable String message, @NotNull Throwable e);
|
||||
void debug(@Nullable String tag, @Nullable String message, @NotNull Throwable e);
|
||||
|
||||
void error(@Nullable String tag, @Nullable String message, @NotNull Throwable e);
|
||||
void error(@Nullable String tag, @Nullable String message, @NotNull Throwable e);
|
||||
|
||||
void error(@Nullable String tag, @Nullable String message);
|
||||
void error(@Nullable String tag, @Nullable String message);
|
||||
|
||||
}
|
||||
|
@@ -11,21 +11,21 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface CalculatorMathEngine {
|
||||
|
||||
@NotNull
|
||||
String evaluate(@NotNull String expression) throws ParseException;
|
||||
@NotNull
|
||||
String evaluate(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
String simplify(@NotNull String expression) throws ParseException;
|
||||
@NotNull
|
||||
String simplify(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
String elementary(@NotNull String expression) throws ParseException;
|
||||
@NotNull
|
||||
String elementary(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
Generic evaluateGeneric(@NotNull String expression) throws ParseException;
|
||||
@NotNull
|
||||
Generic evaluateGeneric(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
Generic simplifyGeneric(@NotNull String expression) throws ParseException;
|
||||
@NotNull
|
||||
Generic simplifyGeneric(@NotNull String expression) throws ParseException;
|
||||
|
||||
@NotNull
|
||||
Generic elementaryGeneric(@NotNull String expression) throws ParseException;
|
||||
@NotNull
|
||||
Generic elementaryGeneric(@NotNull String expression) throws ParseException;
|
||||
}
|
||||
|
@@ -21,8 +21,8 @@ public interface CalculatorMathRegistry<T extends MathEntity> extends MathRegist
|
||||
@Nullable
|
||||
String getDescription(@NotNull String mathEntityName);
|
||||
|
||||
@Nullable
|
||||
String getCategory(@NotNull T mathEntity);
|
||||
@Nullable
|
||||
String getCategory(@NotNull T mathEntity);
|
||||
|
||||
void load();
|
||||
|
||||
|
@@ -17,32 +17,32 @@ import java.util.ResourceBundle;
|
||||
*/
|
||||
public class CalculatorMessage extends AbstractMessage {
|
||||
|
||||
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
|
||||
super(messageCode, messageType, parameters);
|
||||
}
|
||||
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
|
||||
super(messageCode, messageType, parameters);
|
||||
}
|
||||
|
||||
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @NotNull List<?> parameters) {
|
||||
super(messageCode, messageType, parameters);
|
||||
}
|
||||
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @NotNull List<?> parameters) {
|
||||
super(messageCode, messageType, parameters);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message newInfoMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.info, parameters);
|
||||
}
|
||||
@NotNull
|
||||
public static Message newInfoMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.info, parameters);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message newWarningMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.warning, parameters);
|
||||
}
|
||||
@NotNull
|
||||
public static Message newWarningMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.warning, parameters);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.error, parameters);
|
||||
}
|
||||
@NotNull
|
||||
public static Message newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.error, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMessagePattern(@NotNull Locale locale) {
|
||||
final ResourceBundle rb = CalculatorMessages.getBundle(locale);
|
||||
return rb.getString(getMessageCode());
|
||||
}
|
||||
@Override
|
||||
protected String getMessagePattern(@NotNull Locale locale) {
|
||||
final ResourceBundle rb = CalculatorMessages.getBundle(locale);
|
||||
return rb.getString(getMessageCode());
|
||||
}
|
||||
}
|
||||
|
@@ -15,55 +15,57 @@ import java.util.ResourceBundle;
|
||||
public final class CalculatorMessages {
|
||||
|
||||
|
||||
private CalculatorMessages() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
private CalculatorMessages() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ResourceBundle getBundle() {
|
||||
return getBundle(Locale.getDefault());
|
||||
}
|
||||
@NotNull
|
||||
public static ResourceBundle getBundle() {
|
||||
return getBundle(Locale.getDefault());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ResourceBundle getBundle(@NotNull Locale locale) {
|
||||
return ResourceBundle.getBundle("org/solovyev/android/calculator/messages", locale);
|
||||
}
|
||||
@NotNull
|
||||
public static ResourceBundle getBundle(@NotNull Locale locale) {
|
||||
return ResourceBundle.getBundle("org/solovyev/android/calculator/messages", locale);
|
||||
}
|
||||
|
||||
/* Arithmetic error occurred: {0} */
|
||||
public static final String msg_001 = "msg_1";
|
||||
/* Arithmetic error occurred: {0} */
|
||||
public static final String msg_001 = "msg_1";
|
||||
|
||||
/* Too complex expression*/
|
||||
public static final String msg_002 = "msg_2";
|
||||
/* Too complex expression*/
|
||||
public static final String msg_002 = "msg_2";
|
||||
|
||||
/* Too long execution time - check the expression*/
|
||||
public static final String msg_003 = "msg_3";
|
||||
/* Too long execution time - check the expression*/
|
||||
public static final String msg_003 = "msg_3";
|
||||
|
||||
/* Evaluation was cancelled*/
|
||||
public static final String msg_004 = "msg_4";
|
||||
/* Evaluation was cancelled*/
|
||||
public static final String msg_004 = "msg_4";
|
||||
|
||||
/* No parameters are specified for function: {0}*/
|
||||
public static final String msg_005 = "msg_5";
|
||||
/* No parameters are specified for function: {0}*/
|
||||
public static final String msg_005 = "msg_5";
|
||||
|
||||
/* Infinite loop is detected in expression*/
|
||||
public static final String msg_006 = "msg_6";
|
||||
/* Infinite loop is detected in expression*/
|
||||
public static final String msg_006 = "msg_6";
|
||||
|
||||
/** Some data could not be loaded. Contact authors of application with information below.\n\nUnable to load:\n{0} */
|
||||
public static final String msg_007 = "msg_7";
|
||||
/**
|
||||
* Some data could not be loaded. Contact authors of application with information below.\n\nUnable to load:\n{0}
|
||||
*/
|
||||
public static final String msg_007 = "msg_7";
|
||||
|
||||
/* Error */
|
||||
public static final String syntax_error = "syntax_error";
|
||||
/* Error */
|
||||
public static final String syntax_error = "syntax_error";
|
||||
|
||||
/* Result copied to clipboard! */
|
||||
public static final String result_copied = "result_copied";
|
||||
/* Result copied to clipboard! */
|
||||
public static final String result_copied = "result_copied";
|
||||
|
||||
/* Text copied to clipboard! */
|
||||
public static final String text_copied = "text_copied";
|
||||
/* Text copied to clipboard! */
|
||||
public static final String text_copied = "text_copied";
|
||||
|
||||
/* Last calculated value */
|
||||
public static final String ans_description = "ans_description";
|
||||
public static final String ans_description = "ans_description";
|
||||
|
||||
@NotNull
|
||||
public static CalculatorMessage newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters ) {
|
||||
return new CalculatorMessage(messageCode, MessageType.error, parameters);
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorMessage newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters) {
|
||||
return new CalculatorMessage(messageCode, MessageType.error, parameters);
|
||||
}
|
||||
}
|
||||
|
@@ -14,11 +14,11 @@ import java.util.List;
|
||||
*/
|
||||
public interface CalculatorNotifier {
|
||||
|
||||
void showMessage(@NotNull Message message);
|
||||
void showMessage(@NotNull Message message);
|
||||
|
||||
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters);
|
||||
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters);
|
||||
|
||||
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters);
|
||||
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters);
|
||||
|
||||
void showDebugMessage(@Nullable String tag, @NotNull String message);
|
||||
void showDebugMessage(@Nullable String tag, @NotNull String message);
|
||||
}
|
||||
|
@@ -6,12 +6,13 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.math.operator.*;
|
||||
import jscl.math.operator.Operator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
@@ -22,6 +23,7 @@ public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegis
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
|
||||
static {
|
||||
substitutes.put("Σ", "sum");
|
||||
substitutes.put("∏", "product");
|
||||
@@ -35,7 +37,7 @@ public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegis
|
||||
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
|
||||
|
||||
public CalculatorOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
@@ -45,15 +47,15 @@ public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegis
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : OperatorCategory.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : OperatorCategory.values()) {
|
||||
if (category.isInCategory(operator)) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
@@ -66,7 +68,7 @@ public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegis
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public void save() {
|
||||
// not supported yet
|
||||
}
|
||||
@@ -82,12 +84,12 @@ public class CalculatorOperatorsMathRegistry extends AbstractCalculatorMathRegis
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
}
|
||||
|
@@ -12,14 +12,14 @@ import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
*/
|
||||
public interface CalculatorOutput {
|
||||
|
||||
@NotNull
|
||||
String getStringResult();
|
||||
@NotNull
|
||||
String getStringResult();
|
||||
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
@NotNull
|
||||
JsclOperation getOperation();
|
||||
|
||||
|
||||
// null in case of empty expression
|
||||
@Nullable
|
||||
Generic getResult();
|
||||
// null in case of empty expression
|
||||
@Nullable
|
||||
Generic getResult();
|
||||
}
|
||||
|
@@ -6,56 +6,56 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:28 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 7:28 PM
|
||||
*/
|
||||
public class CalculatorOutputImpl implements CalculatorOutput {
|
||||
|
||||
@Nullable
|
||||
private Generic result;
|
||||
@Nullable
|
||||
private Generic result;
|
||||
|
||||
@NotNull
|
||||
private String stringResult;
|
||||
@NotNull
|
||||
private String stringResult;
|
||||
|
||||
@NotNull
|
||||
private JsclOperation operation;
|
||||
@NotNull
|
||||
private JsclOperation operation;
|
||||
|
||||
private CalculatorOutputImpl(@NotNull String stringResult,
|
||||
@NotNull JsclOperation operation,
|
||||
@Nullable Generic result) {
|
||||
this.stringResult = stringResult;
|
||||
this.operation = operation;
|
||||
this.result = result;
|
||||
}
|
||||
private CalculatorOutputImpl(@NotNull String stringResult,
|
||||
@NotNull JsclOperation operation,
|
||||
@Nullable Generic result) {
|
||||
this.stringResult = stringResult;
|
||||
this.operation = operation;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOutput newOutput(@NotNull String stringResult,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull Generic result) {
|
||||
return new CalculatorOutputImpl(stringResult, operation, result);
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorOutput newOutput(@NotNull String stringResult,
|
||||
@NotNull JsclOperation operation,
|
||||
@NotNull Generic result) {
|
||||
return new CalculatorOutputImpl(stringResult, operation, result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOutput newEmptyOutput(@NotNull JsclOperation operation) {
|
||||
return new CalculatorOutputImpl("", operation, null);
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorOutput newEmptyOutput(@NotNull JsclOperation operation) {
|
||||
return new CalculatorOutputImpl("", operation, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JsclOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public JsclOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Generic getResult() {
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
@Nullable
|
||||
public Generic getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -37,16 +37,16 @@ public class CalculatorParseException extends Exception implements Message {
|
||||
}
|
||||
|
||||
public CalculatorParseException(@Nullable Integer position,
|
||||
@NotNull String expression,
|
||||
@NotNull Message message) {
|
||||
@NotNull String expression,
|
||||
@NotNull Message message) {
|
||||
this.message = message;
|
||||
this.expression = expression;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public CalculatorParseException(@NotNull String expression,
|
||||
@NotNull Message message) {
|
||||
this(null, expression, message);
|
||||
@NotNull Message message) {
|
||||
this(null, expression, message);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@@ -23,6 +23,7 @@ public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRe
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
|
||||
static {
|
||||
substitutes.put("%", "percent");
|
||||
substitutes.put("!", "factorial");
|
||||
@@ -34,7 +35,7 @@ public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRe
|
||||
private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_";
|
||||
|
||||
public CalculatorPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
@@ -45,15 +46,15 @@ public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRe
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : OperatorCategory.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : OperatorCategory.values()) {
|
||||
if (category.isInCategory(operator)) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
@@ -66,7 +67,7 @@ public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRe
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public void save() {
|
||||
// not supported yet
|
||||
}
|
||||
|
@@ -11,11 +11,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface CalculatorPreferenceService {
|
||||
|
||||
void setPreferredAngleUnits();
|
||||
void setAngleUnits(@NotNull AngleUnit angleUnit);
|
||||
void setPreferredAngleUnits();
|
||||
|
||||
void setPreferredNumeralBase();
|
||||
void setNumeralBase(@NotNull NumeralBase numeralBase);
|
||||
void setAngleUnits(@NotNull AngleUnit angleUnit);
|
||||
|
||||
void checkPreferredPreferences(boolean force);
|
||||
void setPreferredNumeralBase();
|
||||
|
||||
void setNumeralBase(@NotNull NumeralBase numeralBase);
|
||||
|
||||
void checkPreferredPreferences(boolean force);
|
||||
}
|
||||
|
@@ -13,159 +13,159 @@ import java.util.Map;
|
||||
*/
|
||||
public enum CalculatorSpecialButton {
|
||||
|
||||
history("history") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history, null);
|
||||
}
|
||||
},
|
||||
history_detached("history_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history_detached, null);
|
||||
}
|
||||
},
|
||||
cursor_right("▶") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.moveCursorRight();
|
||||
}
|
||||
},
|
||||
cursor_left("◀") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.moveCursorLeft();
|
||||
}
|
||||
},
|
||||
settings("settings") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null);
|
||||
}
|
||||
},
|
||||
history("history") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history, null);
|
||||
}
|
||||
},
|
||||
history_detached("history_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_history_detached, null);
|
||||
}
|
||||
},
|
||||
cursor_right("▶") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.moveCursorRight();
|
||||
}
|
||||
},
|
||||
cursor_left("◀") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.moveCursorLeft();
|
||||
}
|
||||
},
|
||||
settings("settings") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null);
|
||||
}
|
||||
},
|
||||
|
||||
settings_detached("settings_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null);
|
||||
}
|
||||
},
|
||||
settings_detached("settings_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null);
|
||||
}
|
||||
},
|
||||
|
||||
like("like") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_like_dialog, null);
|
||||
}
|
||||
},
|
||||
erase("erase") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getEditor().erase();
|
||||
}
|
||||
},
|
||||
paste("paste") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.pasteButtonPressed();
|
||||
}
|
||||
},
|
||||
copy("copy") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.copyButtonPressed();
|
||||
}
|
||||
},
|
||||
equals("=") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().evaluate();
|
||||
}
|
||||
},
|
||||
clear("clear") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.clearButtonPressed();
|
||||
}
|
||||
},
|
||||
functions("functions") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions, null);
|
||||
}
|
||||
},
|
||||
functions_detached("functions_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions_detached, null);
|
||||
}
|
||||
},
|
||||
open_app("open_app") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.open_app, null);
|
||||
}
|
||||
},
|
||||
vars("vars") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars, null);
|
||||
}
|
||||
},
|
||||
vars_detached("vars_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars_detached, null);
|
||||
}
|
||||
},
|
||||
operators("operators") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null);
|
||||
}
|
||||
},
|
||||
like("like") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_like_dialog, null);
|
||||
}
|
||||
},
|
||||
erase("erase") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getEditor().erase();
|
||||
}
|
||||
},
|
||||
paste("paste") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.pasteButtonPressed();
|
||||
}
|
||||
},
|
||||
copy("copy") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.copyButtonPressed();
|
||||
}
|
||||
},
|
||||
equals("=") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().evaluate();
|
||||
}
|
||||
},
|
||||
clear("clear") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
keyboard.clearButtonPressed();
|
||||
}
|
||||
},
|
||||
functions("functions") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions, null);
|
||||
}
|
||||
},
|
||||
functions_detached("functions_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_functions_detached, null);
|
||||
}
|
||||
},
|
||||
open_app("open_app") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.open_app, null);
|
||||
}
|
||||
},
|
||||
vars("vars") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars, null);
|
||||
}
|
||||
},
|
||||
vars_detached("vars_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_vars_detached, null);
|
||||
}
|
||||
},
|
||||
operators("operators") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null);
|
||||
}
|
||||
},
|
||||
|
||||
operators_detached("operators_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators_detached, null);
|
||||
}
|
||||
};
|
||||
operators_detached("operators_detached") {
|
||||
@Override
|
||||
public void onClick(@NotNull CalculatorKeyboard keyboard) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators_detached, null);
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<String, CalculatorSpecialButton>();
|
||||
@NotNull
|
||||
private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<String, CalculatorSpecialButton>();
|
||||
|
||||
@NotNull
|
||||
private final String actionCode;
|
||||
@NotNull
|
||||
private final String actionCode;
|
||||
|
||||
CalculatorSpecialButton(@NotNull String actionCode) {
|
||||
this.actionCode = actionCode;
|
||||
}
|
||||
CalculatorSpecialButton(@NotNull String actionCode) {
|
||||
this.actionCode = actionCode;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getActionCode() {
|
||||
return actionCode;
|
||||
}
|
||||
@NotNull
|
||||
public String getActionCode() {
|
||||
return actionCode;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CalculatorSpecialButton getByActionCode(@NotNull String actionCode) {
|
||||
initButtonsByActionCodesMap();
|
||||
return buttonsByActionCodes.get(actionCode);
|
||||
}
|
||||
@Nullable
|
||||
public static CalculatorSpecialButton getByActionCode(@NotNull String actionCode) {
|
||||
initButtonsByActionCodesMap();
|
||||
return buttonsByActionCodes.get(actionCode);
|
||||
}
|
||||
|
||||
public abstract void onClick(@NotNull CalculatorKeyboard keyboard);
|
||||
public abstract void onClick(@NotNull CalculatorKeyboard keyboard);
|
||||
|
||||
private static void initButtonsByActionCodesMap() {
|
||||
if ( buttonsByActionCodes.isEmpty() ) {
|
||||
// if not initialized
|
||||
private static void initButtonsByActionCodesMap() {
|
||||
if (buttonsByActionCodes.isEmpty()) {
|
||||
// if not initialized
|
||||
|
||||
final CalculatorSpecialButton[] specialButtons = values();
|
||||
final CalculatorSpecialButton[] specialButtons = values();
|
||||
|
||||
final Map<String, CalculatorSpecialButton> localButtonsByActionCodes = new HashMap<String, CalculatorSpecialButton>(specialButtons.length);
|
||||
for (CalculatorSpecialButton specialButton : specialButtons) {
|
||||
localButtonsByActionCodes.put(specialButton.getActionCode(), specialButton);
|
||||
}
|
||||
final Map<String, CalculatorSpecialButton> localButtonsByActionCodes = new HashMap<String, CalculatorSpecialButton>(specialButtons.length);
|
||||
for (CalculatorSpecialButton specialButton : specialButtons) {
|
||||
localButtonsByActionCodes.put(specialButton.getActionCode(), specialButton);
|
||||
}
|
||||
|
||||
buttonsByActionCodes = localButtonsByActionCodes;
|
||||
}
|
||||
}
|
||||
buttonsByActionCodes = localButtonsByActionCodes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,29 +15,29 @@ import java.util.Set;
|
||||
*/
|
||||
public final class CalculatorUtils {
|
||||
|
||||
static final long FIRST_ID = 0;
|
||||
static final long FIRST_ID = 0;
|
||||
|
||||
private CalculatorUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
private CalculatorUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorEventData createFirstEventDataId() {
|
||||
return CalculatorEventDataImpl.newInstance(FIRST_ID, FIRST_ID);
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorEventData createFirstEventDataId() {
|
||||
return CalculatorEventDataImpl.newInstance(FIRST_ID, FIRST_ID);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Set<Constant> getNotSystemConstants(@NotNull Generic expression) {
|
||||
final Set<Constant> notSystemConstants = new HashSet<Constant>();
|
||||
@NotNull
|
||||
public static Set<Constant> getNotSystemConstants(@NotNull Generic expression) {
|
||||
final Set<Constant> notSystemConstants = new HashSet<Constant>();
|
||||
|
||||
for (Constant constant : expression.getConstants()) {
|
||||
IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(constant.getName());
|
||||
if (var != null && !var.isSystem() && !var.isDefined()) {
|
||||
notSystemConstants.add(constant);
|
||||
}
|
||||
}
|
||||
for (Constant constant : expression.getConstants()) {
|
||||
IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(constant.getName());
|
||||
if (var != null && !var.isSystem() && !var.isDefined()) {
|
||||
notSystemConstants.add(constant);
|
||||
}
|
||||
}
|
||||
|
||||
return notSystemConstants;
|
||||
}
|
||||
return notSystemConstants;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -26,11 +26,12 @@ import java.util.Map;
|
||||
*/
|
||||
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
|
||||
|
||||
@NotNull
|
||||
public static final String ANS = "ans";
|
||||
@NotNull
|
||||
public static final String ANS = "ans";
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
|
||||
static {
|
||||
substitutes.put("π", "pi");
|
||||
substitutes.put("Π", "PI");
|
||||
@@ -39,29 +40,29 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<ICons
|
||||
substitutes.put("NaN", "nan");
|
||||
}
|
||||
|
||||
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
|
||||
@NotNull MathEntityDao<Var> mathEntityDao) {
|
||||
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
|
||||
@NotNull MathEntityDao<Var> mathEntityDao) {
|
||||
super(mathRegistry, "c_var_description_", mathEntityDao);
|
||||
}
|
||||
|
||||
public static <T extends MathEntity> void saveVariable(@NotNull CalculatorMathRegistry<T> registry,
|
||||
@NotNull MathEntityBuilder<? extends T> builder,
|
||||
@Nullable T editedInstance,
|
||||
@NotNull Object source, boolean save) {
|
||||
final T addedVar = registry.add(builder);
|
||||
public static <T extends MathEntity> void saveVariable(@NotNull CalculatorMathRegistry<T> registry,
|
||||
@NotNull MathEntityBuilder<? extends T> builder,
|
||||
@Nullable T editedInstance,
|
||||
@NotNull Object source, boolean save) {
|
||||
final T addedVar = registry.add(builder);
|
||||
|
||||
if (save) {
|
||||
registry.save();
|
||||
}
|
||||
if (save) {
|
||||
registry.save();
|
||||
}
|
||||
|
||||
if (editedInstance == null) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source);
|
||||
} else {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source);
|
||||
}
|
||||
}
|
||||
if (editedInstance == null) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source);
|
||||
} else {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
@@ -89,15 +90,15 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<ICons
|
||||
return new Var.Builder(entity);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
|
||||
return new Vars();
|
||||
}
|
||||
|
||||
private void tryToAddAuxVar(@NotNull String name) {
|
||||
if ( !contains(name) ) {
|
||||
add(new Var.Builder(name, (String)null));
|
||||
private void tryToAddAuxVar(@NotNull String name) {
|
||||
if (!contains(name)) {
|
||||
add(new Var.Builder(name, (String) null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,23 +113,23 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<ICons
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription(@NotNull String mathEntityName) {
|
||||
final IConstant var = get(mathEntityName);
|
||||
if (var != null && !var.isSystem()) {
|
||||
return var.getDescription();
|
||||
} else {
|
||||
return super.getDescription(mathEntityName);
|
||||
}
|
||||
}
|
||||
public String getDescription(@NotNull String mathEntityName) {
|
||||
final IConstant var = get(mathEntityName);
|
||||
if (var != null && !var.isSystem()) {
|
||||
return var.getDescription();
|
||||
} else {
|
||||
return super.getDescription(mathEntityName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull IConstant var) {
|
||||
for (VarCategory category : VarCategory.values()) {
|
||||
if ( category.isInCategory(var) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String getCategory(@NotNull IConstant var) {
|
||||
for (VarCategory category : VarCategory.values()) {
|
||||
if (category.isInCategory(var)) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -9,10 +9,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface Change<T> {
|
||||
|
||||
@NotNull
|
||||
T getOldValue();
|
||||
@NotNull
|
||||
T getOldValue();
|
||||
|
||||
@NotNull
|
||||
T getNewValue();
|
||||
@NotNull
|
||||
T getNewValue();
|
||||
|
||||
}
|
||||
|
@@ -9,34 +9,34 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class ChangeImpl<T> implements Change<T> {
|
||||
|
||||
@NotNull
|
||||
private T oldValue;
|
||||
@NotNull
|
||||
private T oldValue;
|
||||
|
||||
@NotNull
|
||||
private T newValue;
|
||||
@NotNull
|
||||
private T newValue;
|
||||
|
||||
private ChangeImpl() {
|
||||
}
|
||||
private ChangeImpl() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <T> Change<T> newInstance(@NotNull T oldValue, @NotNull T newValue) {
|
||||
final ChangeImpl<T> result = new ChangeImpl<T>();
|
||||
@NotNull
|
||||
public static <T> Change<T> newInstance(@NotNull T oldValue, @NotNull T newValue) {
|
||||
final ChangeImpl<T> result = new ChangeImpl<T>();
|
||||
|
||||
result.oldValue = oldValue;
|
||||
result.newValue = newValue;
|
||||
result.oldValue = oldValue;
|
||||
result.newValue = newValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public T getOldValue() {
|
||||
return this.oldValue;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public T getOldValue() {
|
||||
return this.oldValue;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public T getNewValue() {
|
||||
return this.newValue;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public T getNewValue() {
|
||||
return this.newValue;
|
||||
}
|
||||
}
|
||||
|
@@ -9,6 +9,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface ConversionFailure {
|
||||
|
||||
@NotNull
|
||||
Exception getException();
|
||||
@NotNull
|
||||
Exception getException();
|
||||
}
|
||||
|
@@ -9,16 +9,16 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class ConversionFailureImpl implements ConversionFailure {
|
||||
|
||||
@NotNull
|
||||
private Exception exception;
|
||||
@NotNull
|
||||
private Exception exception;
|
||||
|
||||
public ConversionFailureImpl(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
public ConversionFailureImpl(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Exception getException() {
|
||||
return this.exception;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Exception getException() {
|
||||
return this.exception;
|
||||
}
|
||||
}
|
||||
|
@@ -11,12 +11,12 @@ import org.solovyev.common.msg.MessageLevel;
|
||||
*/
|
||||
public interface DialogData {
|
||||
|
||||
@NotNull
|
||||
String getMessage();
|
||||
@NotNull
|
||||
String getMessage();
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
MessageLevel getMessageLevel();
|
||||
|
||||
@Nullable
|
||||
String getTitle();
|
||||
@Nullable
|
||||
String getTitle();
|
||||
}
|
||||
|
@@ -9,16 +9,16 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class DummyCalculatorClipboard implements CalculatorClipboard {
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public String getText() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(@NotNull String text) {
|
||||
}
|
||||
@Override
|
||||
public void setText(@NotNull String text) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(@NotNull CharSequence text) {
|
||||
}
|
||||
@Override
|
||||
public void setText(@NotNull CharSequence text) {
|
||||
}
|
||||
}
|
||||
|
@@ -14,19 +14,19 @@ import java.util.List;
|
||||
*/
|
||||
public class DummyCalculatorNotifier implements CalculatorNotifier {
|
||||
|
||||
@Override
|
||||
public void showMessage(@NotNull Message message) {
|
||||
}
|
||||
@Override
|
||||
public void showMessage(@NotNull Message message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters) {
|
||||
}
|
||||
@Override
|
||||
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
|
||||
}
|
||||
@Override
|
||||
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDebugMessage(@Nullable String tag, @NotNull String message) {
|
||||
}
|
||||
@Override
|
||||
public void showDebugMessage(@Nullable String tag, @NotNull String message) {
|
||||
}
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ public interface Editor {
|
||||
|
||||
void setText(@Nullable CharSequence text);
|
||||
|
||||
int getSelection();
|
||||
int getSelection();
|
||||
|
||||
void setSelection(int selection);
|
||||
|
||||
|
@@ -14,5 +14,5 @@ public interface FixableError extends Serializable {
|
||||
@Nullable
|
||||
CharSequence getFixCaption();
|
||||
|
||||
void fix();
|
||||
void fix();
|
||||
}
|
||||
|
@@ -12,77 +12,77 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 7:15 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 7:15 PM
|
||||
*/
|
||||
public enum FunctionCategory {
|
||||
|
||||
trigonometric(100){
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function);
|
||||
}
|
||||
},
|
||||
trigonometric(100) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function);
|
||||
}
|
||||
},
|
||||
|
||||
hyperbolic_trigonometric(300) {
|
||||
hyperbolic_trigonometric(300) {
|
||||
|
||||
private final List<String> names = Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth");
|
||||
private final List<String> names = Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth");
|
||||
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return names.contains(function.getName());
|
||||
}
|
||||
},
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return names.contains(function.getName());
|
||||
}
|
||||
},
|
||||
|
||||
comparison(200) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return function instanceof Comparison;
|
||||
}
|
||||
},
|
||||
comparison(200) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return function instanceof Comparison;
|
||||
}
|
||||
},
|
||||
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return !function.isSystem();
|
||||
}
|
||||
},
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
return !function.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
common(50) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
for (FunctionCategory category : values()) {
|
||||
if ( category != this ) {
|
||||
if ( category.isInCategory(function) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
common(50) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Function function) {
|
||||
for (FunctionCategory category : values()) {
|
||||
if (category != this) {
|
||||
if (category.isInCategory(function)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private final int tabOrder;
|
||||
private final int tabOrder;
|
||||
|
||||
FunctionCategory(int tabOrder) {
|
||||
this.tabOrder = tabOrder;
|
||||
}
|
||||
FunctionCategory(int tabOrder) {
|
||||
this.tabOrder = tabOrder;
|
||||
}
|
||||
|
||||
public abstract boolean isInCategory(@NotNull Function function);
|
||||
public abstract boolean isInCategory(@NotNull Function function);
|
||||
|
||||
@NotNull
|
||||
public static List<FunctionCategory> getCategoriesByTabOrder() {
|
||||
final List<FunctionCategory> result = Collections.asList(FunctionCategory.values());
|
||||
@NotNull
|
||||
public static List<FunctionCategory> getCategoriesByTabOrder() {
|
||||
final List<FunctionCategory> result = Collections.asList(FunctionCategory.values());
|
||||
|
||||
java.util.Collections.sort(result, new Comparator<FunctionCategory>() {
|
||||
@Override
|
||||
public int compare(FunctionCategory category, FunctionCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
java.util.Collections.sort(result, new Comparator<FunctionCategory>() {
|
||||
@Override
|
||||
public int compare(FunctionCategory category, FunctionCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -16,45 +16,45 @@ import java.util.List;
|
||||
*/
|
||||
public class ListCalculatorEventContainer implements CalculatorEventContainer {
|
||||
|
||||
@NotNull
|
||||
private static final String TAG = "CalculatorEventData";
|
||||
@NotNull
|
||||
private static final String TAG = "CalculatorEventData";
|
||||
|
||||
@NotNull
|
||||
private final JListeners<CalculatorEventListener> listeners = Listeners.newWeakRefListeners();
|
||||
@NotNull
|
||||
private final JListeners<CalculatorEventListener> listeners = Listeners.newWeakRefListeners();
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.addListener(calculatorEventListener);
|
||||
}
|
||||
@Override
|
||||
public void addCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.addListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.removeListener(calculatorEventListener);
|
||||
}
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@NotNull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.removeListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
fireCalculatorEvents(Arrays.asList(new CalculatorEvent(calculatorEventData, calculatorEventType, data)));
|
||||
}
|
||||
@Override
|
||||
public void fireCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
fireCalculatorEvents(Arrays.asList(new CalculatorEvent(calculatorEventData, calculatorEventType, data)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
final Collection<CalculatorEventListener> listeners = this.listeners.getListeners();
|
||||
@Override
|
||||
public void fireCalculatorEvents(@NotNull List<CalculatorEvent> calculatorEvents) {
|
||||
final Collection<CalculatorEventListener> listeners = this.listeners.getListeners();
|
||||
|
||||
//final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
//final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
|
||||
for (CalculatorEvent e : calculatorEvents) {
|
||||
//Locator.getInstance().getLogger().debug(TAG, "Event fired: " + e.getCalculatorEventType());
|
||||
for (CalculatorEventListener listener : listeners) {
|
||||
/*long startTime = System.currentTimeMillis();*/
|
||||
listener.onCalculatorEvent(e.getCalculatorEventData(), e.getCalculatorEventType(), e.getData());
|
||||
for (CalculatorEvent e : calculatorEvents) {
|
||||
//Locator.getInstance().getLogger().debug(TAG, "Event fired: " + e.getCalculatorEventType());
|
||||
for (CalculatorEventListener listener : listeners) {
|
||||
/*long startTime = System.currentTimeMillis();*/
|
||||
listener.onCalculatorEvent(e.getCalculatorEventData(), e.getCalculatorEventType(), e.getData());
|
||||
/* long endTime = System.currentTimeMillis();
|
||||
long totalTime = (endTime - startTime);
|
||||
if ( totalTime > 300 ) {
|
||||
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Started event: " + e.getCalculatorEventType() + " with data: " + e.getData() + " for: " + listener.getClass().getSimpleName());
|
||||
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Total time, ms: " + totalTime);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,145 +12,145 @@ import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||
*/
|
||||
public class Locator implements CalculatorLocator {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEngine calculatorEngine;
|
||||
@NotNull
|
||||
private CalculatorEngine calculatorEngine;
|
||||
|
||||
@NotNull
|
||||
private Calculator calculator;
|
||||
@NotNull
|
||||
private Calculator calculator;
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
@NotNull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
|
||||
@NotNull
|
||||
private CalculatorDisplay calculatorDisplay;
|
||||
@NotNull
|
||||
private CalculatorDisplay calculatorDisplay;
|
||||
|
||||
@NotNull
|
||||
private CalculatorKeyboard calculatorKeyboard;
|
||||
@NotNull
|
||||
private CalculatorKeyboard calculatorKeyboard;
|
||||
|
||||
@NotNull
|
||||
private CalculatorHistory calculatorHistory;
|
||||
@NotNull
|
||||
private CalculatorHistory calculatorHistory;
|
||||
|
||||
@NotNull
|
||||
private CalculatorNotifier calculatorNotifier = new DummyCalculatorNotifier();
|
||||
@NotNull
|
||||
private CalculatorNotifier calculatorNotifier = new DummyCalculatorNotifier();
|
||||
|
||||
@NotNull
|
||||
private CalculatorLogger calculatorLogger = new SystemOutCalculatorLogger();
|
||||
@NotNull
|
||||
private CalculatorLogger calculatorLogger = new SystemOutCalculatorLogger();
|
||||
|
||||
@NotNull
|
||||
private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard();
|
||||
@NotNull
|
||||
private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard();
|
||||
|
||||
@NotNull
|
||||
private static final CalculatorLocator instance = new Locator();
|
||||
@NotNull
|
||||
private static final CalculatorLocator instance = new Locator();
|
||||
|
||||
@NotNull
|
||||
private CalculatorPreferenceService calculatorPreferenceService;
|
||||
@NotNull
|
||||
private CalculatorPreferenceService calculatorPreferenceService;
|
||||
|
||||
@NotNull
|
||||
private CalculatorExternalListenersContainer calculatorExternalListenersContainer;
|
||||
|
||||
@NotNull
|
||||
private CalculatorPlotter calculatorPlotter;
|
||||
@NotNull
|
||||
private CalculatorPlotter calculatorPlotter;
|
||||
|
||||
public Locator() {
|
||||
}
|
||||
public Locator() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(@NotNull Calculator calculator,
|
||||
@NotNull CalculatorEngine engine,
|
||||
@NotNull CalculatorClipboard clipboard,
|
||||
@NotNull CalculatorNotifier notifier,
|
||||
@NotNull CalculatorHistory history,
|
||||
@NotNull CalculatorLogger logger,
|
||||
@NotNull CalculatorPreferenceService preferenceService,
|
||||
@NotNull CalculatorKeyboard keyboard,
|
||||
@NotNull CalculatorExternalListenersContainer externalListenersContainer,
|
||||
@NotNull CalculatorPlotter plotter) {
|
||||
@Override
|
||||
public void init(@NotNull Calculator calculator,
|
||||
@NotNull CalculatorEngine engine,
|
||||
@NotNull CalculatorClipboard clipboard,
|
||||
@NotNull CalculatorNotifier notifier,
|
||||
@NotNull CalculatorHistory history,
|
||||
@NotNull CalculatorLogger logger,
|
||||
@NotNull CalculatorPreferenceService preferenceService,
|
||||
@NotNull CalculatorKeyboard keyboard,
|
||||
@NotNull CalculatorExternalListenersContainer externalListenersContainer,
|
||||
@NotNull CalculatorPlotter plotter) {
|
||||
|
||||
this.calculator = calculator;
|
||||
this.calculatorEngine = engine;
|
||||
this.calculatorClipboard = clipboard;
|
||||
this.calculatorNotifier = notifier;
|
||||
this.calculatorHistory = history;
|
||||
this.calculatorLogger = logger;
|
||||
this.calculatorPreferenceService = preferenceService;
|
||||
this.calculator = calculator;
|
||||
this.calculatorEngine = engine;
|
||||
this.calculatorClipboard = clipboard;
|
||||
this.calculatorNotifier = notifier;
|
||||
this.calculatorHistory = history;
|
||||
this.calculatorLogger = logger;
|
||||
this.calculatorPreferenceService = preferenceService;
|
||||
this.calculatorExternalListenersContainer = externalListenersContainer;
|
||||
this.calculatorPlotter = plotter;
|
||||
this.calculatorPlotter = plotter;
|
||||
|
||||
calculatorEditor = new CalculatorEditorImpl(this.calculator);
|
||||
calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
|
||||
calculatorKeyboard = keyboard;
|
||||
}
|
||||
calculatorEditor = new CalculatorEditorImpl(this.calculator);
|
||||
calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
|
||||
calculatorKeyboard = keyboard;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorLocator getInstance() {
|
||||
return instance;
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorLocator getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEngine getEngine() {
|
||||
return calculatorEngine;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEngine getEngine() {
|
||||
return calculatorEngine;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Calculator getCalculator() {
|
||||
return this.calculator;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Calculator getCalculator() {
|
||||
return this.calculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorDisplay getDisplay() {
|
||||
return calculatorDisplay;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorDisplay getDisplay() {
|
||||
return calculatorDisplay;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditor getEditor() {
|
||||
return calculatorEditor;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorEditor getEditor() {
|
||||
return calculatorEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorKeyboard getKeyboard() {
|
||||
return calculatorKeyboard;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorKeyboard getKeyboard() {
|
||||
return calculatorKeyboard;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorClipboard getClipboard() {
|
||||
return calculatorClipboard;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorClipboard getClipboard() {
|
||||
return calculatorClipboard;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorNotifier getNotifier() {
|
||||
return calculatorNotifier;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorNotifier getNotifier() {
|
||||
return calculatorNotifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistory getHistory() {
|
||||
return calculatorHistory;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistory getHistory() {
|
||||
return calculatorHistory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorLogger getLogger() {
|
||||
return calculatorLogger;
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorLogger getLogger() {
|
||||
return calculatorLogger;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorPlotter getPlotter() {
|
||||
return calculatorPlotter;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorPlotter getPlotter() {
|
||||
return calculatorPlotter;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorPreferenceService getPreferenceService() {
|
||||
return this.calculatorPreferenceService;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public CalculatorPreferenceService getPreferenceService() {
|
||||
return this.calculatorPreferenceService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
|
@@ -10,11 +10,11 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public interface MathEntityDao<T extends MathPersistenceEntity> {
|
||||
|
||||
void save(@NotNull MathEntityPersistenceContainer<T> container);
|
||||
void save(@NotNull MathEntityPersistenceContainer<T> container);
|
||||
|
||||
@Nullable
|
||||
MathEntityPersistenceContainer<T> load();
|
||||
@Nullable
|
||||
MathEntityPersistenceContainer<T> load();
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull String descriptionId);
|
||||
@Nullable
|
||||
String getDescription(@NotNull String descriptionId);
|
||||
}
|
||||
|
@@ -9,6 +9,6 @@ import java.util.List;
|
||||
*/
|
||||
public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> {
|
||||
|
||||
public List<T> getEntities();
|
||||
public List<T> getEntities();
|
||||
|
||||
}
|
||||
|
@@ -12,37 +12,37 @@ import org.solovyev.common.msg.MessageLevel;
|
||||
*/
|
||||
public class MessageDialogData implements DialogData {
|
||||
|
||||
@NotNull
|
||||
private Message message;
|
||||
@NotNull
|
||||
private Message message;
|
||||
|
||||
@Nullable
|
||||
private String title;
|
||||
@Nullable
|
||||
private String title;
|
||||
|
||||
private MessageDialogData(@NotNull Message message, @Nullable String title) {
|
||||
this.message = message;
|
||||
this.title = title;
|
||||
}
|
||||
private MessageDialogData(@NotNull Message message, @Nullable String title) {
|
||||
this.message = message;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DialogData newInstance(@NotNull Message message, @Nullable String title) {
|
||||
return new MessageDialogData(message, title);
|
||||
}
|
||||
@NotNull
|
||||
public static DialogData newInstance(@NotNull Message message, @Nullable String title) {
|
||||
return new MessageDialogData(message, title);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getMessage() {
|
||||
return message.getLocalizedMessage();
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public String getMessage() {
|
||||
return message.getLocalizedMessage();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageLevel getMessageLevel() {
|
||||
return message.getMessageLevel();
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageLevel getMessageLevel() {
|
||||
return message.getMessageLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@Override
|
||||
@Nullable
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@@ -33,11 +33,9 @@ public class NumberBuilder extends AbstractNumberBuilder {
|
||||
/**
|
||||
* Method replaces number in text according to some rules (e.g. formatting)
|
||||
*
|
||||
* @param text text where number can be replaced
|
||||
* @param text text where number can be replaced
|
||||
* @param mathTypeResult math type result of current token
|
||||
* @param offset offset between new number length and old number length (newNumberLength - oldNumberLength)
|
||||
*
|
||||
*
|
||||
* @param offset offset between new number length and old number length (newNumberLength - oldNumberLength)
|
||||
* @return new math type result (as one can be changed due to substituting of number with constant)
|
||||
*/
|
||||
@NotNull
|
||||
@@ -70,9 +68,8 @@ public class NumberBuilder extends AbstractNumberBuilder {
|
||||
/**
|
||||
* Method replaces number in text according to some rules (e.g. formatting)
|
||||
*
|
||||
* @param text text where number can be replaced
|
||||
* @param text text where number can be replaced
|
||||
* @param offset offset between new number length and old number length (newNumberLength - oldNumberLength)
|
||||
*
|
||||
* @return new math type result (as one can be changed due to substituting of number with constant)
|
||||
*/
|
||||
@Nullable
|
||||
|
@@ -8,70 +8,70 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 7:40 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 7:40 PM
|
||||
*/
|
||||
public enum OperatorCategory {
|
||||
|
||||
derivatives(100){
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
|
||||
}
|
||||
},
|
||||
derivatives(100) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
|
||||
}
|
||||
},
|
||||
|
||||
other(200) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Sum || operator instanceof Product;
|
||||
}
|
||||
},
|
||||
other(200) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Sum || operator instanceof Product;
|
||||
}
|
||||
},
|
||||
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return !operator.isSystem();
|
||||
}
|
||||
},
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
return !operator.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
common(50) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : values()) {
|
||||
if ( category != this ) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
common(50) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull Operator operator) {
|
||||
for (OperatorCategory category : values()) {
|
||||
if (category != this) {
|
||||
if (category.isInCategory(operator)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private final int tabOrder;
|
||||
private final int tabOrder;
|
||||
|
||||
OperatorCategory(int tabOrder) {
|
||||
this.tabOrder = tabOrder;
|
||||
}
|
||||
OperatorCategory(int tabOrder) {
|
||||
this.tabOrder = tabOrder;
|
||||
}
|
||||
|
||||
public abstract boolean isInCategory(@NotNull Operator operator);
|
||||
public abstract boolean isInCategory(@NotNull Operator operator);
|
||||
|
||||
@NotNull
|
||||
public static List<OperatorCategory> getCategoriesByTabOrder() {
|
||||
final List<OperatorCategory> result = Collections.asList(OperatorCategory.values());
|
||||
@NotNull
|
||||
public static List<OperatorCategory> getCategoriesByTabOrder() {
|
||||
final List<OperatorCategory> result = Collections.asList(OperatorCategory.values());
|
||||
|
||||
java.util.Collections.sort(result, new Comparator<OperatorCategory>() {
|
||||
@Override
|
||||
public int compare(OperatorCategory category, OperatorCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
java.util.Collections.sort(result, new Comparator<OperatorCategory>() {
|
||||
@Override
|
||||
public int compare(OperatorCategory category, OperatorCategory category1) {
|
||||
return category.tabOrder - category1.tabOrder;
|
||||
}
|
||||
});
|
||||
|
||||
// todo serso: current solution (as creating operators is not implemented yet)
|
||||
result.remove(my);
|
||||
// todo serso: current solution (as creating operators is not implemented yet)
|
||||
result.remove(my);
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
* Date: 10/18/11
|
||||
* Time: 10:07 PM
|
||||
*/
|
||||
public class PreparedExpression implements CharSequence{
|
||||
public class PreparedExpression implements CharSequence {
|
||||
|
||||
@NotNull
|
||||
private String expression;
|
||||
|
@@ -11,41 +11,41 @@ import org.solovyev.common.msg.MessageType;
|
||||
*/
|
||||
public class StringDialogData implements DialogData {
|
||||
|
||||
@NotNull
|
||||
private final String message;
|
||||
@NotNull
|
||||
private final String message;
|
||||
|
||||
@NotNull
|
||||
private final MessageType messageType;
|
||||
@NotNull
|
||||
private final MessageType messageType;
|
||||
|
||||
@Nullable
|
||||
private final String title;
|
||||
@Nullable
|
||||
private final String title;
|
||||
|
||||
private StringDialogData(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
|
||||
this.message = message;
|
||||
this.messageType = messageType;
|
||||
this.title = title;
|
||||
}
|
||||
private StringDialogData(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
|
||||
this.message = message;
|
||||
this.messageType = messageType;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DialogData newInstance(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
|
||||
return new StringDialogData(message, messageType, title);
|
||||
}
|
||||
@NotNull
|
||||
public static DialogData newInstance(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
|
||||
return new StringDialogData(message, messageType, title);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageLevel() {
|
||||
return messageType;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageLevel() {
|
||||
return messageType;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@Nullable
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@@ -10,33 +10,33 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public class SystemOutCalculatorLogger implements CalculatorLogger {
|
||||
|
||||
@NotNull
|
||||
private static final String TAG = SystemOutCalculatorLogger.class.getSimpleName();
|
||||
@NotNull
|
||||
private static final String TAG = SystemOutCalculatorLogger.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
public void debug(@Nullable String tag, @Nullable String message) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
}
|
||||
@Override
|
||||
public void debug(@Nullable String tag, @Nullable String message) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getTag(@Nullable String tag) {
|
||||
return tag != null ? tag : TAG;
|
||||
}
|
||||
@NotNull
|
||||
private String getTag(@Nullable String tag) {
|
||||
return tag != null ? tag : TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(@Nullable String tag, @Nullable String message, @NotNull Throwable e) {
|
||||
debug(tag, message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
@Override
|
||||
public void debug(@Nullable String tag, @Nullable String message, @NotNull Throwable e) {
|
||||
debug(tag, message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(@Nullable String tag, @Nullable String message, @NotNull Throwable e) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
@Override
|
||||
public void error(@Nullable String tag, @Nullable String message, @NotNull Throwable e) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(@Nullable String tag, @Nullable String message) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
}
|
||||
@Override
|
||||
public void error(@Nullable String tag, @Nullable String message) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
}
|
||||
}
|
||||
|
@@ -23,19 +23,19 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
@NotNull
|
||||
private static final Integer MAX_DEPTH = 20;
|
||||
|
||||
@NotNull
|
||||
private static final TextProcessor<PreparedExpression, String> instance = new ToJsclTextProcessor();
|
||||
@NotNull
|
||||
private static final TextProcessor<PreparedExpression, String> instance = new ToJsclTextProcessor();
|
||||
|
||||
private ToJsclTextProcessor() {
|
||||
}
|
||||
private ToJsclTextProcessor() {
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public static TextProcessor<PreparedExpression, String> getInstance() {
|
||||
return instance;
|
||||
}
|
||||
@NotNull
|
||||
public static TextProcessor<PreparedExpression, String> getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
@NotNull
|
||||
public PreparedExpression process(@NotNull String s) throws CalculatorParseException {
|
||||
return processWithDepth(s, 0, new ArrayList<IConstant>());
|
||||
@@ -73,17 +73,17 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
}
|
||||
}
|
||||
|
||||
if (mathTypeBefore != null &&
|
||||
(mathTypeBefore.getMathType() == MathType.function || mathTypeBefore.getMathType() == MathType.operator) &&
|
||||
Collections.find(MathType.openGroupSymbols, startsWithFinder) != null) {
|
||||
final String functionName = mathTypeBefore.getMatch();
|
||||
final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
|
||||
if ( function == null || function.getMinParameters() > 0 ) {
|
||||
throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.getMatch()));
|
||||
}
|
||||
}
|
||||
if (mathTypeBefore != null &&
|
||||
(mathTypeBefore.getMathType() == MathType.function || mathTypeBefore.getMathType() == MathType.operator) &&
|
||||
Collections.find(MathType.openGroupSymbols, startsWithFinder) != null) {
|
||||
final String functionName = mathTypeBefore.getMatch();
|
||||
final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
|
||||
if (function == null || function.getMinParameters() > 0) {
|
||||
throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.getMatch()));
|
||||
}
|
||||
}
|
||||
|
||||
i = mathTypeResult.processToJscl(result, i);
|
||||
i = mathTypeResult.processToJscl(result, i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -91,7 +91,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
@NotNull
|
||||
private static PreparedExpression replaceVariables(@NotNull final String s, int depth, @NotNull List<IConstant> undefinedVars) throws CalculatorParseException {
|
||||
if (depth >= MAX_DEPTH) {
|
||||
throw new CalculatorParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error));
|
||||
throw new CalculatorParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error));
|
||||
} else {
|
||||
depth++;
|
||||
}
|
||||
@@ -119,7 +119,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
final String value = var.getValue();
|
||||
assert value != null;
|
||||
|
||||
if ( var.getDoubleValue() != null ) {
|
||||
if (var.getDoubleValue() != null) {
|
||||
//result.append(value);
|
||||
// NOTE: append varName as JSCL engine will convert it to double if needed
|
||||
result.append(varName);
|
||||
|
@@ -8,22 +8,22 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 4:25 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 4:25 PM
|
||||
*/
|
||||
public enum VarCategory {
|
||||
|
||||
system(100){
|
||||
system(100) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull IConstant var) {
|
||||
public boolean isInCategory(@NotNull IConstant var) {
|
||||
return var.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
my(0) {
|
||||
@Override
|
||||
public boolean isInCategory(@NotNull IConstant var) {
|
||||
public boolean isInCategory(@NotNull IConstant var) {
|
||||
return !var.isSystem();
|
||||
}
|
||||
};
|
||||
|
@@ -9,44 +9,44 @@ import org.solovyev.android.calculator.model.AFunction;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 12:21 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 12:21 PM
|
||||
*/
|
||||
public final class FunctionBuilderAdapter implements MathEntityBuilder<Function> {
|
||||
|
||||
@NotNull
|
||||
private final AFunction.Builder nestedBuilder;
|
||||
@NotNull
|
||||
private final AFunction.Builder nestedBuilder;
|
||||
|
||||
public FunctionBuilderAdapter(@NotNull AFunction.Builder nestedBuilder) {
|
||||
this.nestedBuilder = nestedBuilder;
|
||||
}
|
||||
public FunctionBuilderAdapter(@NotNull AFunction.Builder nestedBuilder) {
|
||||
this.nestedBuilder = nestedBuilder;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setName(@NotNull String name) {
|
||||
nestedBuilder.setName(name);
|
||||
return this;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setName(@NotNull String name) {
|
||||
nestedBuilder.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setDescription(@Nullable String description) {
|
||||
nestedBuilder.setDescription(description);
|
||||
return this;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setDescription(@Nullable String description) {
|
||||
nestedBuilder.setDescription(description);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setValue(@Nullable String value) {
|
||||
nestedBuilder.setValue(value);
|
||||
return this;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setValue(@Nullable String value) {
|
||||
nestedBuilder.setValue(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Function create() throws CustomFunctionCalculationException, AFunction.Builder.CreationException{
|
||||
final AFunction function = nestedBuilder.create();
|
||||
return new CustomFunction.Builder(function).create();
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Function create() throws CustomFunctionCalculationException, AFunction.Builder.CreationException {
|
||||
final AFunction function = nestedBuilder.create();
|
||||
return new CustomFunction.Builder(function).create();
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ import java.util.Date;
|
||||
* Date: 10/15/11
|
||||
* Time: 1:45 PM
|
||||
*/
|
||||
public class AbstractHistoryState implements Cloneable{
|
||||
public class AbstractHistoryState implements Cloneable {
|
||||
|
||||
@Element
|
||||
private long time = new Date().getTime();
|
||||
@@ -70,7 +70,7 @@ public class AbstractHistoryState implements Cloneable{
|
||||
AbstractHistoryState clone;
|
||||
|
||||
try {
|
||||
clone = (AbstractHistoryState)super.clone();
|
||||
clone = (AbstractHistoryState) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
|
@@ -53,7 +53,7 @@ public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
public static CalculatorDisplayHistoryState newInstance(@NotNull CalculatorDisplayViewState viewState) {
|
||||
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
|
||||
|
||||
result.editorState = EditorHistoryState.newInstance(viewState);
|
||||
result.editorState = EditorHistoryState.newInstance(viewState);
|
||||
|
||||
result.valid = viewState.isValid();
|
||||
result.jsclOperation = viewState.getOperation();
|
||||
@@ -64,11 +64,11 @@ public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
}
|
||||
|
||||
public void setValuesFromHistory(@NotNull CalculatorDisplay display) {
|
||||
if ( this.isValid() ) {
|
||||
display.setViewState(CalculatorDisplayViewStateImpl.newValidState(this.getJsclOperation(), this.getGenericResult(), Strings.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition()));
|
||||
} else {
|
||||
display.setViewState(CalculatorDisplayViewStateImpl.newErrorState(this.getJsclOperation(), Strings.getNotEmpty(this.getErrorMessage(), "")));
|
||||
}
|
||||
if (this.isValid()) {
|
||||
display.setViewState(CalculatorDisplayViewStateImpl.newValidState(this.getJsclOperation(), this.getGenericResult(), Strings.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition()));
|
||||
} else {
|
||||
display.setViewState(CalculatorDisplayViewStateImpl.newErrorState(this.getJsclOperation(), Strings.getNotEmpty(this.getErrorMessage(), "")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -13,28 +13,28 @@ import java.util.List;
|
||||
*/
|
||||
public interface CalculatorHistory extends HistoryHelper<CalculatorHistoryState>, CalculatorEventListener {
|
||||
|
||||
void load();
|
||||
void load();
|
||||
|
||||
void save();
|
||||
void save();
|
||||
|
||||
void fromXml(@NotNull String xml);
|
||||
void fromXml(@NotNull String xml);
|
||||
|
||||
String toXml();
|
||||
String toXml();
|
||||
|
||||
void clearSavedHistory();
|
||||
void clearSavedHistory();
|
||||
|
||||
void removeSavedHistory(@NotNull CalculatorHistoryState historyState);
|
||||
void removeSavedHistory(@NotNull CalculatorHistoryState historyState);
|
||||
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getSavedHistory();
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getSavedHistory();
|
||||
|
||||
@NotNull
|
||||
CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState);
|
||||
@NotNull
|
||||
CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState);
|
||||
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getStates();
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getStates();
|
||||
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getStates(boolean includeIntermediateStates);
|
||||
@NotNull
|
||||
List<CalculatorHistoryState> getStates(boolean includeIntermediateStates);
|
||||
|
||||
}
|
||||
|
@@ -22,97 +22,97 @@ import static org.solovyev.android.calculator.CalculatorEventType.*;
|
||||
*/
|
||||
public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
@NotNull
|
||||
private final HistoryHelper<CalculatorHistoryState> history = SimpleHistoryHelper.newInstance();
|
||||
@NotNull
|
||||
private final HistoryHelper<CalculatorHistoryState> history = SimpleHistoryHelper.newInstance();
|
||||
|
||||
@NotNull
|
||||
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
|
||||
@NotNull
|
||||
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
|
||||
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
@NotNull
|
||||
private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
|
||||
@Nullable
|
||||
private volatile CalculatorEditorViewState lastEditorViewState;
|
||||
@Nullable
|
||||
private volatile CalculatorEditorViewState lastEditorViewState;
|
||||
|
||||
public CalculatorHistoryImpl(@NotNull Calculator calculator) {
|
||||
calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
public CalculatorHistoryImpl(@NotNull Calculator calculator) {
|
||||
calculator.addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
synchronized (history) {
|
||||
return this.history.isEmpty();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
synchronized (history) {
|
||||
return this.history.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState getLastHistoryState() {
|
||||
synchronized (history) {
|
||||
return this.history.getLastHistoryState();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public CalculatorHistoryState getLastHistoryState() {
|
||||
synchronized (history) {
|
||||
return this.history.getLastHistoryState();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUndoAvailable() {
|
||||
synchronized (history) {
|
||||
return history.isUndoAvailable();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isUndoAvailable() {
|
||||
synchronized (history) {
|
||||
return history.isUndoAvailable();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.undo(currentState);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.undo(currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedoAvailable() {
|
||||
return history.isRedoAvailable();
|
||||
}
|
||||
@Override
|
||||
public boolean isRedoAvailable() {
|
||||
return history.isRedoAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.redo(currentState);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.redo(currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionAvailable(@NotNull HistoryAction historyAction) {
|
||||
synchronized (history) {
|
||||
return history.isActionAvailable(historyAction);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isActionAvailable(@NotNull HistoryAction historyAction) {
|
||||
synchronized (history) {
|
||||
return history.isActionAvailable(historyAction);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState doAction(@NotNull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.doAction(historyAction, currentState);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public CalculatorHistoryState doAction(@NotNull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.doAction(historyAction, currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addState(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
history.addState(currentState);
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void addState(@Nullable CalculatorHistoryState currentState) {
|
||||
synchronized (history) {
|
||||
history.addState(currentState);
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
synchronized (history) {
|
||||
return history.getStates();
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
synchronized (history) {
|
||||
return history.getStates();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) {
|
||||
synchronized (history) {
|
||||
if (includeIntermediateStates) {
|
||||
return getStates();
|
||||
@@ -123,17 +123,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
|
||||
CalculatorHistoryState laterState = null;
|
||||
for (CalculatorHistoryState state : org.solovyev.common.collections.Collections.reversed(states)) {
|
||||
if ( laterState != null ) {
|
||||
final String laterEditorText = laterState.getEditorState().getText();
|
||||
final String editorText = state.getEditorState().getText();
|
||||
if ( laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
|
||||
// intermediate result => skip from add
|
||||
} else {
|
||||
result.add(0, state);
|
||||
}
|
||||
} else {
|
||||
result.add(0, state);
|
||||
}
|
||||
if (laterState != null) {
|
||||
final String laterEditorText = laterState.getEditorState().getText();
|
||||
final String editorText = state.getEditorState().getText();
|
||||
if (laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
|
||||
// intermediate result => skip from add
|
||||
} else {
|
||||
result.add(0, state);
|
||||
}
|
||||
} else {
|
||||
result.add(0, state);
|
||||
}
|
||||
|
||||
laterState = state;
|
||||
}
|
||||
@@ -143,116 +143,116 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isIntermediate(@NotNull String laterEditorText,
|
||||
@NotNull String editorText) {
|
||||
if ( Math.abs(laterEditorText.length() - editorText.length()) <= 1 ) {
|
||||
if ( laterEditorText.length() > editorText.length() ) {
|
||||
return laterEditorText.startsWith(editorText);
|
||||
} else {
|
||||
return editorText.startsWith(laterEditorText);
|
||||
}
|
||||
}
|
||||
private boolean isIntermediate(@NotNull String laterEditorText,
|
||||
@NotNull String editorText) {
|
||||
if (Math.abs(laterEditorText.length() - editorText.length()) <= 1) {
|
||||
if (laterEditorText.length() > editorText.length()) {
|
||||
return laterEditorText.startsWith(editorText);
|
||||
} else {
|
||||
return editorText.startsWith(laterEditorText);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
synchronized (history) {
|
||||
this.history.clear();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void clear() {
|
||||
synchronized (history) {
|
||||
this.history.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<CalculatorHistoryState> getSavedHistory() {
|
||||
return Collections.unmodifiableList(savedHistory);
|
||||
}
|
||||
@Override
|
||||
@NotNull
|
||||
public List<CalculatorHistoryState> getSavedHistory() {
|
||||
return Collections.unmodifiableList(savedHistory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState) {
|
||||
if (historyState.isSaved()) {
|
||||
return historyState;
|
||||
} else {
|
||||
final CalculatorHistoryState savedState = historyState.clone();
|
||||
@Override
|
||||
@NotNull
|
||||
public CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState) {
|
||||
if (historyState.isSaved()) {
|
||||
return historyState;
|
||||
} else {
|
||||
final CalculatorHistoryState savedState = historyState.clone();
|
||||
|
||||
savedState.setId(counter.incrementAndGet());
|
||||
savedState.setSaved(true);
|
||||
savedState.setId(counter.incrementAndGet());
|
||||
savedState.setSaved(true);
|
||||
|
||||
savedHistory.add(savedState);
|
||||
savedHistory.add(savedState);
|
||||
|
||||
return savedState;
|
||||
}
|
||||
}
|
||||
return savedState;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// todo serso: create saved/loader class
|
||||
}
|
||||
@Override
|
||||
public void load() {
|
||||
// todo serso: create saved/loader class
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// todo serso: create saved/loader class
|
||||
}
|
||||
@Override
|
||||
public void save() {
|
||||
// todo serso: create saved/loader class
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromXml(@NotNull String xml) {
|
||||
clearSavedHistory();
|
||||
@Override
|
||||
public void fromXml(@NotNull String xml) {
|
||||
clearSavedHistory();
|
||||
|
||||
HistoryUtils.fromXml(xml, this.savedHistory);
|
||||
for (CalculatorHistoryState historyState : savedHistory) {
|
||||
historyState.setSaved(true);
|
||||
historyState.setId(counter.incrementAndGet());
|
||||
}
|
||||
}
|
||||
HistoryUtils.fromXml(xml, this.savedHistory);
|
||||
for (CalculatorHistoryState historyState : savedHistory) {
|
||||
historyState.setSaved(true);
|
||||
historyState.setId(counter.incrementAndGet());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toXml() {
|
||||
return HistoryUtils.toXml(this.savedHistory);
|
||||
}
|
||||
@Override
|
||||
public String toXml() {
|
||||
return HistoryUtils.toXml(this.savedHistory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSavedHistory() {
|
||||
this.savedHistory.clear();
|
||||
}
|
||||
@Override
|
||||
public void clearSavedHistory() {
|
||||
this.savedHistory.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState) {
|
||||
this.savedHistory.remove(historyState);
|
||||
}
|
||||
@Override
|
||||
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState) {
|
||||
this.savedHistory.remove(historyState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(editor_state_changed, display_state_changed, manual_calculation_requested)) {
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
|
||||
@NotNull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
if (calculatorEventType.isOfType(editor_state_changed, display_state_changed, manual_calculation_requested)) {
|
||||
|
||||
final CalculatorEventHolder.Result result = lastEventData.apply(calculatorEventData);
|
||||
final CalculatorEventHolder.Result result = lastEventData.apply(calculatorEventData);
|
||||
|
||||
if (result.isNewAfter() && result.isNewSameOrAfterSequence() ) {
|
||||
switch (calculatorEventType) {
|
||||
case manual_calculation_requested:
|
||||
lastEditorViewState = (CalculatorEditorViewState) data;
|
||||
break;
|
||||
case editor_state_changed:
|
||||
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
|
||||
lastEditorViewState = editorChangeData.getNewValue();
|
||||
break;
|
||||
case display_state_changed:
|
||||
if (result.isSameSequence()) {
|
||||
if (lastEditorViewState != null) {
|
||||
final CalculatorEditorViewState editorViewState = lastEditorViewState;
|
||||
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
|
||||
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue();
|
||||
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
|
||||
}
|
||||
} else {
|
||||
lastEditorViewState = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (result.isNewAfter() && result.isNewSameOrAfterSequence()) {
|
||||
switch (calculatorEventType) {
|
||||
case manual_calculation_requested:
|
||||
lastEditorViewState = (CalculatorEditorViewState) data;
|
||||
break;
|
||||
case editor_state_changed:
|
||||
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
|
||||
lastEditorViewState = editorChangeData.getNewValue();
|
||||
break;
|
||||
case display_state_changed:
|
||||
if (result.isSameSequence()) {
|
||||
if (lastEditorViewState != null) {
|
||||
final CalculatorEditorViewState editorViewState = lastEditorViewState;
|
||||
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
|
||||
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue();
|
||||
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
|
||||
}
|
||||
} else {
|
||||
lastEditorViewState = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,10 @@ package org.solovyev.android.calculator.history;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.CalculatorDisplay;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditor;
|
||||
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
@@ -32,31 +35,31 @@ public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
}
|
||||
|
||||
private CalculatorHistoryState(@NotNull EditorHistoryState editorState,
|
||||
@NotNull CalculatorDisplayHistoryState displayState) {
|
||||
@NotNull CalculatorDisplayHistoryState displayState) {
|
||||
this.editorState = editorState;
|
||||
this.displayState = displayState;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public static CalculatorHistoryState newInstance(@NotNull CalculatorEditor editor,
|
||||
@NotNull CalculatorDisplay display) {
|
||||
final CalculatorEditorViewState editorViewState = editor.getViewState();
|
||||
final CalculatorDisplayViewState displayViewState = display.getViewState();
|
||||
@NotNull CalculatorDisplay display) {
|
||||
final CalculatorEditorViewState editorViewState = editor.getViewState();
|
||||
final CalculatorDisplayViewState displayViewState = display.getViewState();
|
||||
|
||||
return newInstance(editorViewState, displayViewState);
|
||||
return newInstance(editorViewState, displayViewState);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorHistoryState newInstance(@NotNull CalculatorEditorViewState editorViewState,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState);
|
||||
@NotNull
|
||||
public static CalculatorHistoryState newInstance(@NotNull CalculatorEditorViewState editorViewState,
|
||||
@NotNull CalculatorDisplayViewState displayViewState) {
|
||||
final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState);
|
||||
|
||||
final CalculatorDisplayHistoryState displayHistoryState = CalculatorDisplayHistoryState.newInstance(displayViewState);
|
||||
final CalculatorDisplayHistoryState displayHistoryState = CalculatorDisplayHistoryState.newInstance(displayViewState);
|
||||
|
||||
return new CalculatorHistoryState(editorHistoryState, displayHistoryState);
|
||||
}
|
||||
return new CalculatorHistoryState(editorHistoryState, displayHistoryState);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public EditorHistoryState getEditorState() {
|
||||
return editorState;
|
||||
}
|
||||
@@ -113,7 +116,7 @@ public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
|
||||
@Override
|
||||
protected CalculatorHistoryState clone() {
|
||||
final CalculatorHistoryState clone = (CalculatorHistoryState)super.clone();
|
||||
final CalculatorHistoryState clone = (CalculatorHistoryState) super.clone();
|
||||
|
||||
clone.editorState = this.editorState.clone();
|
||||
clone.displayState = this.displayState.clone();
|
||||
|
@@ -15,7 +15,7 @@ import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
@Root
|
||||
public class EditorHistoryState implements Cloneable{
|
||||
public class EditorHistoryState implements Cloneable {
|
||||
|
||||
@Element
|
||||
private int cursorPosition;
|
||||
@@ -38,15 +38,15 @@ public class EditorHistoryState implements Cloneable{
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static EditorHistoryState newInstance(@NotNull CalculatorDisplayViewState viewState) {
|
||||
final EditorHistoryState result = new EditorHistoryState();
|
||||
@NotNull
|
||||
public static EditorHistoryState newInstance(@NotNull CalculatorDisplayViewState viewState) {
|
||||
final EditorHistoryState result = new EditorHistoryState();
|
||||
|
||||
result.text = viewState.getText();
|
||||
result.cursorPosition = viewState.getSelection();
|
||||
result.text = viewState.getText();
|
||||
result.cursorPosition = viewState.getSelection();
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setValuesFromHistory(@NotNull CalculatorEditor editor) {
|
||||
editor.setText(Strings.getNotEmpty(this.getText(), ""));
|
||||
@@ -93,7 +93,7 @@ public class EditorHistoryState implements Cloneable{
|
||||
@Override
|
||||
protected EditorHistoryState clone() {
|
||||
try {
|
||||
return (EditorHistoryState)super.clone();
|
||||
return (EditorHistoryState) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
|
@@ -15,7 +15,10 @@ import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.common.JPredicate;
|
||||
import org.solovyev.common.collections.Collections;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public enum MathType {
|
||||
@@ -25,8 +28,8 @@ public enum MathType {
|
||||
private final List<String> tokens = new ArrayList<String>(10);
|
||||
{
|
||||
for (NumeralBase numeralBase : NumeralBase.values()) {
|
||||
tokens.add(numeralBase.getJsclPrefix());
|
||||
}
|
||||
tokens.add(numeralBase.getJsclPrefix());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -43,7 +46,7 @@ public enum MathType {
|
||||
}
|
||||
},
|
||||
|
||||
grouping_separator(250, false, false, MathGroupType.number, "'", " "){
|
||||
grouping_separator(250, false, false, MathGroupType.number, "'", " ") {
|
||||
@Override
|
||||
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) throws CalculatorParseException {
|
||||
return i;
|
||||
@@ -133,6 +136,7 @@ public enum MathType {
|
||||
tokens.add(character.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot /*&& mathTypeBefore != numeral_base*/;
|
||||
@@ -321,7 +325,7 @@ public enum MathType {
|
||||
public static final String G = "G";
|
||||
public static final Double G_VALUE = 6.6738480E-11;
|
||||
public static final String H_REDUCED = "h";
|
||||
public static final Double H_REDUCED_VALUE = 6.6260695729E-34 / ( 2 * Math.PI );
|
||||
public static final Double H_REDUCED_VALUE = 6.6260695729E-34 / (2 * Math.PI);
|
||||
public final static String NAN = "NaN";
|
||||
|
||||
public final static String INFINITY = "∞";
|
||||
@@ -331,9 +335,8 @@ public enum MathType {
|
||||
/**
|
||||
* Method determines mathematical entity type for text substring starting from ith index
|
||||
*
|
||||
*
|
||||
* @param text analyzed text
|
||||
* @param i index which points to start of substring
|
||||
* @param text analyzed text
|
||||
* @param i index which points to start of substring
|
||||
* @param hexMode
|
||||
* @return math entity type of substring starting from ith index of specified text
|
||||
*/
|
||||
@@ -352,10 +355,10 @@ public enum MathType {
|
||||
for (MathType mathType : getMathTypesByPriority()) {
|
||||
final String s = Collections.find(mathType.getTokens(), startsWithFinder);
|
||||
if (s != null) {
|
||||
if ( s.length() == 1 ) {
|
||||
if (s.length() == 1) {
|
||||
if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) {
|
||||
final Character ch = s.charAt(0);
|
||||
if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) ) {
|
||||
if (NumeralBase.hex.getAcceptableCharacters().contains(ch)) {
|
||||
return new Result(MathType.digit, s);
|
||||
}
|
||||
}
|
||||
@@ -376,11 +379,11 @@ public enum MathType {
|
||||
final List<MathType> result = Collections.asList(MathType.values());
|
||||
|
||||
java.util.Collections.sort(result, new Comparator<MathType>() {
|
||||
@Override
|
||||
public int compare(MathType l, MathType r) {
|
||||
return l.priority.compareTo(r.priority);
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public int compare(MathType l, MathType r) {
|
||||
return l.priority.compareTo(r.priority);
|
||||
}
|
||||
});
|
||||
|
||||
mathTypesByPriority = result;
|
||||
}
|
||||
@@ -442,25 +445,25 @@ public enum MathType {
|
||||
}
|
||||
}
|
||||
|
||||
private static class StartsWithFinder implements JPredicate<String> {
|
||||
private static class StartsWithFinder implements JPredicate<String> {
|
||||
|
||||
private int i;
|
||||
private int i;
|
||||
|
||||
@NotNull
|
||||
private final String targetString;
|
||||
@NotNull
|
||||
private final String targetString;
|
||||
|
||||
public StartsWithFinder(@NotNull String targetString, int i) {
|
||||
this.targetString = targetString;
|
||||
this.i = i;
|
||||
}
|
||||
public StartsWithFinder(@NotNull String targetString, int i) {
|
||||
this.targetString = targetString;
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(@Nullable String s) {
|
||||
return s != null && targetString.startsWith(s, i);
|
||||
}
|
||||
@Override
|
||||
public boolean apply(@Nullable String s) {
|
||||
return s != null && targetString.startsWith(s, i);
|
||||
}
|
||||
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -122,16 +122,16 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
|
||||
return String.valueOf(this.content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AFunction{" +
|
||||
"name='" + name + '\'' +
|
||||
", parameterNames=" + parameterNames +
|
||||
", content='" + content + '\'' +
|
||||
'}';
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AFunction{" +
|
||||
"name='" + name + '\'' +
|
||||
", parameterNames=" + parameterNames +
|
||||
", content='" + content + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* GETTERS/SETTERS
|
||||
@@ -175,7 +175,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
@@ -228,21 +228,21 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
|
||||
this.value = function.getContent();
|
||||
this.system = function.isSystem();
|
||||
this.description = function.getDescription();
|
||||
if (function.isIdDefined()) {
|
||||
this.id = function.getId();
|
||||
}
|
||||
this.parameterNames = new ArrayList<String>(function.getParameterNames());
|
||||
}
|
||||
if (function.isIdDefined()) {
|
||||
this.id = function.getId();
|
||||
}
|
||||
this.parameterNames = new ArrayList<String>(function.getParameterNames());
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name,
|
||||
@NotNull String value,
|
||||
@NotNull List<String> parameterNames) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
public Builder(@NotNull String name,
|
||||
@NotNull String value,
|
||||
@NotNull List<String> parameterNames) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public Builder setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
@@ -270,7 +270,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AFunction create() throws AFunction.Builder.CreationException{
|
||||
public AFunction create() throws AFunction.Builder.CreationException {
|
||||
final AFunction result;
|
||||
if (id != null) {
|
||||
result = new AFunction(id);
|
||||
|
@@ -12,10 +12,10 @@ import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 9:21 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 9:21 PM
|
||||
*/
|
||||
public interface MathEntityBuilder<T extends MathEntity> extends JBuilder<T> {
|
||||
|
||||
@NotNull
|
||||
|
@@ -229,10 +229,10 @@ public class Var implements IConstant, MathPersistenceEntity {
|
||||
return !Strings.isEmpty(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
@@ -13,56 +13,63 @@ import java.util.List;
|
||||
*/
|
||||
public interface CalculatorPlotter {
|
||||
|
||||
@NotNull
|
||||
PlotData getPlotData();
|
||||
@NotNull
|
||||
PlotData getPlotData();
|
||||
|
||||
boolean addFunction(@NotNull Generic expression);
|
||||
boolean addFunction(@NotNull PlotFunction plotFunction);
|
||||
boolean addFunction(@NotNull XyFunction xyFunction);
|
||||
boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef);
|
||||
boolean addFunction(@NotNull Generic expression);
|
||||
|
||||
boolean updateFunction(@NotNull PlotFunction newFunction);
|
||||
boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef);
|
||||
boolean addFunction(@NotNull PlotFunction plotFunction);
|
||||
|
||||
boolean removeFunction(@NotNull PlotFunction plotFunction);
|
||||
boolean removeFunction(@NotNull XyFunction xyFunction);
|
||||
boolean addFunction(@NotNull XyFunction xyFunction);
|
||||
|
||||
@NotNull
|
||||
PlotFunction pin(@NotNull PlotFunction plotFunction);
|
||||
boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef);
|
||||
|
||||
@NotNull
|
||||
PlotFunction unpin(@NotNull PlotFunction plotFunction);
|
||||
boolean updateFunction(@NotNull PlotFunction newFunction);
|
||||
|
||||
@NotNull
|
||||
PlotFunction show(@NotNull PlotFunction plotFunction);
|
||||
boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef);
|
||||
|
||||
@NotNull
|
||||
PlotFunction hide(@NotNull PlotFunction plotFunction);
|
||||
boolean removeFunction(@NotNull PlotFunction plotFunction);
|
||||
|
||||
void clearAllFunctions();
|
||||
boolean removeFunction(@NotNull XyFunction xyFunction);
|
||||
|
||||
@NotNull
|
||||
PlotFunction pin(@NotNull PlotFunction plotFunction);
|
||||
|
||||
@NotNull
|
||||
PlotFunction unpin(@NotNull PlotFunction plotFunction);
|
||||
|
||||
@NotNull
|
||||
PlotFunction show(@NotNull PlotFunction plotFunction);
|
||||
|
||||
@NotNull
|
||||
PlotFunction hide(@NotNull PlotFunction plotFunction);
|
||||
|
||||
void clearAllFunctions();
|
||||
|
||||
@Nullable
|
||||
PlotFunction getFunctionById(@NotNull String functionId);
|
||||
|
||||
@NotNull
|
||||
List<PlotFunction> getFunctions();
|
||||
@NotNull
|
||||
List<PlotFunction> getFunctions();
|
||||
|
||||
@NotNull
|
||||
List<PlotFunction> getVisibleFunctions();
|
||||
@NotNull
|
||||
List<PlotFunction> getVisibleFunctions();
|
||||
|
||||
void plot();
|
||||
void plot(@NotNull Generic expression);
|
||||
void plot();
|
||||
|
||||
boolean is2dPlotPossible();
|
||||
void plot(@NotNull Generic expression);
|
||||
|
||||
boolean isPlotPossibleFor(@NotNull Generic expression);
|
||||
boolean is2dPlotPossible();
|
||||
|
||||
void setPlot3d(boolean plot3d);
|
||||
boolean isPlotPossibleFor(@NotNull Generic expression);
|
||||
|
||||
void removeAllUnpinned();
|
||||
void setPlot3d(boolean plot3d);
|
||||
|
||||
void setPlotImag(boolean plotImag);
|
||||
void removeAllUnpinned();
|
||||
|
||||
void savePlotBoundaries(@NotNull PlotBoundaries plotBoundaries);
|
||||
void setPlotBoundaries(@NotNull PlotBoundaries plotBoundaries);
|
||||
void setPlotImag(boolean plotImag);
|
||||
|
||||
void savePlotBoundaries(@NotNull PlotBoundaries plotBoundaries);
|
||||
|
||||
void setPlotBoundaries(@NotNull PlotBoundaries plotBoundaries);
|
||||
}
|
||||
|
@@ -22,258 +22,258 @@ import java.util.List;
|
||||
*/
|
||||
public class CalculatorPlotterImpl implements CalculatorPlotter {
|
||||
|
||||
@NotNull
|
||||
private final List<PlotFunction> functions = new ArrayList<PlotFunction>();
|
||||
@NotNull
|
||||
private final List<PlotFunction> functions = new ArrayList<PlotFunction>();
|
||||
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
@NotNull
|
||||
private final Calculator calculator;
|
||||
|
||||
private final PlotResourceManager resourceManager = new MapPlotResourceManager();
|
||||
private final PlotResourceManager resourceManager = new MapPlotResourceManager();
|
||||
|
||||
private boolean plot3d = false;
|
||||
private boolean plot3d = false;
|
||||
|
||||
private boolean plotImag = false;
|
||||
private boolean plotImag = false;
|
||||
|
||||
private int arity = 0;
|
||||
private int arity = 0;
|
||||
|
||||
@NotNull
|
||||
private PlotBoundaries plotBoundaries = PlotBoundaries.newDefaultInstance();
|
||||
@NotNull
|
||||
private PlotBoundaries plotBoundaries = PlotBoundaries.newDefaultInstance();
|
||||
|
||||
@NotNull
|
||||
private PlotData plotData = new PlotData(Collections.<PlotFunction>emptyList(), plot3d, plotBoundaries);
|
||||
@NotNull
|
||||
private PlotData plotData = new PlotData(Collections.<PlotFunction>emptyList(), plot3d, plotBoundaries);
|
||||
|
||||
public CalculatorPlotterImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
}
|
||||
public CalculatorPlotterImpl(@NotNull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotData getPlotData() {
|
||||
return plotData;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotData getPlotData() {
|
||||
return plotData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addFunction(@NotNull Generic expression) {
|
||||
final List<Constant> variables = new ArrayList<Constant>(CalculatorUtils.getNotSystemConstants(expression));
|
||||
@Override
|
||||
public boolean addFunction(@NotNull Generic expression) {
|
||||
final List<Constant> variables = new ArrayList<Constant>(CalculatorUtils.getNotSystemConstants(expression));
|
||||
|
||||
assert variables.size() <= 2;
|
||||
assert variables.size() <= 2;
|
||||
|
||||
final Constant xVariable;
|
||||
if (variables.size() > 0) {
|
||||
xVariable = variables.get(0);
|
||||
} else {
|
||||
xVariable = null;
|
||||
}
|
||||
final Constant xVariable;
|
||||
if (variables.size() > 0) {
|
||||
xVariable = variables.get(0);
|
||||
} else {
|
||||
xVariable = null;
|
||||
}
|
||||
|
||||
final Constant yVariable;
|
||||
if (variables.size() > 1) {
|
||||
yVariable = variables.get(1);
|
||||
} else {
|
||||
yVariable = null;
|
||||
}
|
||||
final Constant yVariable;
|
||||
if (variables.size() > 1) {
|
||||
yVariable = variables.get(1);
|
||||
} else {
|
||||
yVariable = null;
|
||||
}
|
||||
|
||||
final XyFunction realXyFunction = new XyFunction(expression, xVariable, yVariable, false);
|
||||
final XyFunction imagXyFunction = new XyFunction(expression, xVariable, yVariable, true);
|
||||
final XyFunction realXyFunction = new XyFunction(expression, xVariable, yVariable, false);
|
||||
final XyFunction imagXyFunction = new XyFunction(expression, xVariable, yVariable, true);
|
||||
|
||||
// first create plot functions with default line definitions
|
||||
PlotFunction realPlotFunction = new PlotFunction(realXyFunction);
|
||||
PlotFunction imagPlotFunction = new PlotFunction(imagXyFunction);
|
||||
// first create plot functions with default line definitions
|
||||
PlotFunction realPlotFunction = new PlotFunction(realXyFunction);
|
||||
PlotFunction imagPlotFunction = new PlotFunction(imagXyFunction);
|
||||
|
||||
// then remove all unpinned graphs and free their line definitions
|
||||
removeAllUnpinnedExcept(realPlotFunction, imagPlotFunction);
|
||||
// then remove all unpinned graphs and free their line definitions
|
||||
removeAllUnpinnedExcept(realPlotFunction, imagPlotFunction);
|
||||
|
||||
// create plot functions with freed line definitions
|
||||
realPlotFunction = newPlotFunction(realXyFunction);
|
||||
imagPlotFunction = newPlotFunction(imagXyFunction);
|
||||
// create plot functions with freed line definitions
|
||||
realPlotFunction = newPlotFunction(realXyFunction);
|
||||
imagPlotFunction = newPlotFunction(imagXyFunction);
|
||||
|
||||
final boolean realAdded = addFunction(realPlotFunction);
|
||||
final boolean imagAdded = addFunction(plotImag ? imagPlotFunction : PlotFunction.invisible(imagPlotFunction));
|
||||
final boolean realAdded = addFunction(realPlotFunction);
|
||||
final boolean imagAdded = addFunction(plotImag ? imagPlotFunction : PlotFunction.invisible(imagPlotFunction));
|
||||
|
||||
return imagAdded || realAdded;
|
||||
}
|
||||
return imagAdded || realAdded;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PlotFunction newPlotFunction(@NotNull XyFunction xyFunction) {
|
||||
return new PlotFunction(xyFunction, resourceManager.generateAndRegister());
|
||||
}
|
||||
@NotNull
|
||||
private PlotFunction newPlotFunction(@NotNull XyFunction xyFunction) {
|
||||
return new PlotFunction(xyFunction, resourceManager.generateAndRegister());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addFunction(@NotNull PlotFunction plotFunction) {
|
||||
synchronized (functions) {
|
||||
if (!functions.contains(plotFunction)) {
|
||||
resourceManager.register(plotFunction.getPlotLineDef());
|
||||
functions.add(plotFunction);
|
||||
onFunctionsChanged();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean addFunction(@NotNull PlotFunction plotFunction) {
|
||||
synchronized (functions) {
|
||||
if (!functions.contains(plotFunction)) {
|
||||
resourceManager.register(plotFunction.getPlotLineDef());
|
||||
functions.add(plotFunction);
|
||||
onFunctionsChanged();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean removeAllUnpinnedExcept(@NotNull final PlotFunction... exceptFunctions) {
|
||||
synchronized (functions) {
|
||||
private boolean removeAllUnpinnedExcept(@NotNull final PlotFunction... exceptFunctions) {
|
||||
synchronized (functions) {
|
||||
|
||||
boolean changed = Iterables.removeIf(functions, new Predicate<PlotFunction>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotFunction function) {
|
||||
if ( function != null && !function.isPinned() ) {
|
||||
boolean changed = Iterables.removeIf(functions, new Predicate<PlotFunction>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotFunction function) {
|
||||
if (function != null && !function.isPinned()) {
|
||||
|
||||
for (PlotFunction exceptFunction : exceptFunctions) {
|
||||
if ( exceptFunction.equals(function) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (PlotFunction exceptFunction : exceptFunctions) {
|
||||
if (exceptFunction.equals(function)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
resourceManager.unregister(function.getPlotLineDef());
|
||||
resourceManager.unregister(function.getPlotLineDef());
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (changed) {
|
||||
onFunctionsChanged();
|
||||
}
|
||||
if (changed) {
|
||||
onFunctionsChanged();
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeAllUnpinned() {
|
||||
synchronized (functions) {
|
||||
boolean changed = Iterables.removeIf(functions, new Predicate<PlotFunction>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotFunction function) {
|
||||
boolean removed = function != null && !function.isPinned();
|
||||
@Override
|
||||
public void removeAllUnpinned() {
|
||||
synchronized (functions) {
|
||||
boolean changed = Iterables.removeIf(functions, new Predicate<PlotFunction>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotFunction function) {
|
||||
boolean removed = function != null && !function.isPinned();
|
||||
|
||||
if ( removed ) {
|
||||
resourceManager.unregister(function.getPlotLineDef());
|
||||
}
|
||||
if (removed) {
|
||||
resourceManager.unregister(function.getPlotLineDef());
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
});
|
||||
return removed;
|
||||
}
|
||||
});
|
||||
|
||||
if (changed) {
|
||||
onFunctionsChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
onFunctionsChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeFunction(@NotNull PlotFunction plotFunction) {
|
||||
synchronized (functions) {
|
||||
boolean changed = functions.remove(plotFunction);
|
||||
if (changed) {
|
||||
resourceManager.unregister(plotFunction.getPlotLineDef());
|
||||
onFunctionsChanged();
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean removeFunction(@NotNull PlotFunction plotFunction) {
|
||||
synchronized (functions) {
|
||||
boolean changed = functions.remove(plotFunction);
|
||||
if (changed) {
|
||||
resourceManager.unregister(plotFunction.getPlotLineDef());
|
||||
onFunctionsChanged();
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addFunction(@NotNull XyFunction xyFunction) {
|
||||
return addFunction(newPlotFunction(xyFunction));
|
||||
}
|
||||
@Override
|
||||
public boolean addFunction(@NotNull XyFunction xyFunction) {
|
||||
return addFunction(newPlotFunction(xyFunction));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef) {
|
||||
return addFunction(new PlotFunction(xyFunction, functionLineDef));
|
||||
}
|
||||
@Override
|
||||
public boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef) {
|
||||
return addFunction(new PlotFunction(xyFunction, functionLineDef));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef) {
|
||||
final PlotFunction newFunction = new PlotFunction(xyFunction, functionLineDef);
|
||||
@Override
|
||||
public boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef) {
|
||||
final PlotFunction newFunction = new PlotFunction(xyFunction, functionLineDef);
|
||||
|
||||
return updateFunction(newFunction);
|
||||
}
|
||||
return updateFunction(newFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateFunction(@NotNull PlotFunction newFunction) {
|
||||
boolean changed = updateFunction0(newFunction);
|
||||
if (changed) {
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
@Override
|
||||
public boolean updateFunction(@NotNull PlotFunction newFunction) {
|
||||
boolean changed = updateFunction0(newFunction);
|
||||
if (changed) {
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
public boolean updateFunction0(@NotNull PlotFunction newFunction) {
|
||||
boolean changed = false;
|
||||
public boolean updateFunction0(@NotNull PlotFunction newFunction) {
|
||||
boolean changed = false;
|
||||
|
||||
synchronized (functions) {
|
||||
for (int i = 0; i < functions.size(); i++) {
|
||||
final PlotFunction oldFunction = functions.get(i);
|
||||
if (oldFunction.equals(newFunction)) {
|
||||
synchronized (functions) {
|
||||
for (int i = 0; i < functions.size(); i++) {
|
||||
final PlotFunction oldFunction = functions.get(i);
|
||||
if (oldFunction.equals(newFunction)) {
|
||||
|
||||
resourceManager.unregister(oldFunction.getPlotLineDef());
|
||||
resourceManager.register(newFunction.getPlotLineDef());
|
||||
resourceManager.unregister(oldFunction.getPlotLineDef());
|
||||
resourceManager.register(newFunction.getPlotLineDef());
|
||||
|
||||
// update old function
|
||||
functions.set(i, newFunction);
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// update old function
|
||||
functions.set(i, newFunction);
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeFunction(@NotNull XyFunction xyFunction) {
|
||||
return removeFunction(new PlotFunction(xyFunction));
|
||||
}
|
||||
@Override
|
||||
public boolean removeFunction(@NotNull XyFunction xyFunction) {
|
||||
return removeFunction(new PlotFunction(xyFunction));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction pin(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.pin(plotFunction);
|
||||
updateFunction0(newFunction);
|
||||
return newFunction;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction pin(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.pin(plotFunction);
|
||||
updateFunction0(newFunction);
|
||||
return newFunction;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction unpin(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.unpin(plotFunction);
|
||||
updateFunction0(newFunction);
|
||||
return newFunction;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction unpin(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.unpin(plotFunction);
|
||||
updateFunction0(newFunction);
|
||||
return newFunction;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction show(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.visible(plotFunction);
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction show(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.visible(plotFunction);
|
||||
|
||||
updateFunction(newFunction);
|
||||
updateFunction(newFunction);
|
||||
|
||||
return newFunction;
|
||||
}
|
||||
return newFunction;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction hide(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.invisible(plotFunction);
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotFunction hide(@NotNull PlotFunction plotFunction) {
|
||||
final PlotFunction newFunction = PlotFunction.invisible(plotFunction);
|
||||
|
||||
updateFunction(newFunction);
|
||||
updateFunction(newFunction);
|
||||
|
||||
return newFunction;
|
||||
}
|
||||
return newFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAllFunctions() {
|
||||
synchronized (functions) {
|
||||
resourceManager.unregisterAll();
|
||||
functions.clear();
|
||||
onFunctionsChanged();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void clearAllFunctions() {
|
||||
synchronized (functions) {
|
||||
resourceManager.unregisterAll();
|
||||
functions.clear();
|
||||
onFunctionsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@org.jetbrains.annotations.Nullable
|
||||
@Override
|
||||
@@ -289,137 +289,137 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
||||
}
|
||||
|
||||
// NOTE: this method must be called from synchronized block
|
||||
private void onFunctionsChanged() {
|
||||
assert Thread.holdsLock(functions);
|
||||
private void onFunctionsChanged() {
|
||||
assert Thread.holdsLock(functions);
|
||||
|
||||
int maxArity = 0;
|
||||
for (PlotFunction function : functions) {
|
||||
final XyFunction xyFunction = function.getXyFunction();
|
||||
int maxArity = 0;
|
||||
for (PlotFunction function : functions) {
|
||||
final XyFunction xyFunction = function.getXyFunction();
|
||||
|
||||
maxArity = Math.max(maxArity, xyFunction.getArity());
|
||||
}
|
||||
maxArity = Math.max(maxArity, xyFunction.getArity());
|
||||
}
|
||||
|
||||
if (maxArity > 1) {
|
||||
plot3d = true;
|
||||
} else {
|
||||
plot3d = false;
|
||||
}
|
||||
if (maxArity > 1) {
|
||||
plot3d = true;
|
||||
} else {
|
||||
plot3d = false;
|
||||
}
|
||||
|
||||
if ( functions.isEmpty() ) {
|
||||
// no functions => new plot => default boundaries
|
||||
this.plotBoundaries = PlotBoundaries.newDefaultInstance();
|
||||
}
|
||||
if (functions.isEmpty()) {
|
||||
// no functions => new plot => default boundaries
|
||||
this.plotBoundaries = PlotBoundaries.newDefaultInstance();
|
||||
}
|
||||
|
||||
arity = maxArity;
|
||||
arity = maxArity;
|
||||
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<PlotFunction> getFunctions() {
|
||||
synchronized (functions) {
|
||||
return new ArrayList<PlotFunction>(functions);
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public List<PlotFunction> getFunctions() {
|
||||
synchronized (functions) {
|
||||
return new ArrayList<PlotFunction>(functions);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<PlotFunction> getVisibleFunctions() {
|
||||
synchronized (functions) {
|
||||
return Lists.newArrayList(Iterables.filter(functions, new Predicate<PlotFunction>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotFunction function) {
|
||||
return function != null && function.isVisible();
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public List<PlotFunction> getVisibleFunctions() {
|
||||
synchronized (functions) {
|
||||
return Lists.newArrayList(Iterables.filter(functions, new Predicate<PlotFunction>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotFunction function) {
|
||||
return function != null && function.isVisible();
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plot() {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.plot_graph, null);
|
||||
}
|
||||
@Override
|
||||
public void plot() {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.plot_graph, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plot(@NotNull Generic expression) {
|
||||
addFunction(expression);
|
||||
plot();
|
||||
}
|
||||
@Override
|
||||
public void plot(@NotNull Generic expression) {
|
||||
addFunction(expression);
|
||||
plot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean is2dPlotPossible() {
|
||||
return arity < 2;
|
||||
}
|
||||
@Override
|
||||
public boolean is2dPlotPossible() {
|
||||
return arity < 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlotPossibleFor(@NotNull Generic expression) {
|
||||
boolean result = false;
|
||||
@Override
|
||||
public boolean isPlotPossibleFor(@NotNull Generic expression) {
|
||||
boolean result = false;
|
||||
|
||||
int size = CalculatorUtils.getNotSystemConstants(expression).size();
|
||||
if (size == 0 || size == 1 || size == 2) {
|
||||
result = true;
|
||||
}
|
||||
int size = CalculatorUtils.getNotSystemConstants(expression).size();
|
||||
if (size == 0 || size == 1 || size == 2) {
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlot3d(boolean plot3d) {
|
||||
if (this.plot3d != plot3d) {
|
||||
this.plot3d = plot3d;
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setPlot3d(boolean plot3d) {
|
||||
if (this.plot3d != plot3d) {
|
||||
this.plot3d = plot3d;
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private void firePlotDataChangedEvent() {
|
||||
updatePlotData();
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.plot_data_changed, plotData);
|
||||
}
|
||||
private void firePlotDataChangedEvent() {
|
||||
updatePlotData();
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.plot_data_changed, plotData);
|
||||
}
|
||||
|
||||
private void updatePlotData() {
|
||||
plotData = new PlotData(getVisibleFunctions(), plot3d, plotBoundaries);
|
||||
}
|
||||
private void updatePlotData() {
|
||||
plotData = new PlotData(getVisibleFunctions(), plot3d, plotBoundaries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlotImag(boolean plotImag) {
|
||||
if (this.plotImag != plotImag) {
|
||||
this.plotImag = plotImag;
|
||||
if (toggleImagFunctions(this.plotImag)) {
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setPlotImag(boolean plotImag) {
|
||||
if (this.plotImag != plotImag) {
|
||||
this.plotImag = plotImag;
|
||||
if (toggleImagFunctions(this.plotImag)) {
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlotBoundaries(@NotNull PlotBoundaries plotBoundaries) {
|
||||
if ( !this.plotBoundaries.equals(plotBoundaries) ) {
|
||||
this.plotBoundaries = plotBoundaries;
|
||||
updatePlotData();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void savePlotBoundaries(@NotNull PlotBoundaries plotBoundaries) {
|
||||
if (!this.plotBoundaries.equals(plotBoundaries)) {
|
||||
this.plotBoundaries = plotBoundaries;
|
||||
updatePlotData();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlotBoundaries(@NotNull PlotBoundaries plotBoundaries) {
|
||||
if ( !this.plotBoundaries.equals(plotBoundaries) ) {
|
||||
this.plotBoundaries = plotBoundaries;
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void setPlotBoundaries(@NotNull PlotBoundaries plotBoundaries) {
|
||||
if (!this.plotBoundaries.equals(plotBoundaries)) {
|
||||
this.plotBoundaries = plotBoundaries;
|
||||
firePlotDataChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean toggleImagFunctions(boolean show) {
|
||||
boolean changed = false;
|
||||
private boolean toggleImagFunctions(boolean show) {
|
||||
boolean changed = false;
|
||||
|
||||
synchronized (functions) {
|
||||
for (int i = 0; i < functions.size(); i++) {
|
||||
final PlotFunction plotFunction = functions.get(i);
|
||||
if (plotFunction.getXyFunction().isImag()) {
|
||||
functions.set(i, show ? PlotFunction.visible(plotFunction) : PlotFunction.invisible(plotFunction));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
synchronized (functions) {
|
||||
for (int i = 0; i < functions.size(); i++) {
|
||||
final PlotFunction plotFunction = functions.get(i);
|
||||
if (plotFunction.getXyFunction().isImag()) {
|
||||
functions.set(i, show ? PlotFunction.visible(plotFunction) : PlotFunction.invisible(plotFunction));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
@@ -1,13 +1,16 @@
|
||||
package org.solovyev.android.calculator.plot;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/18/13
|
||||
* Time: 7:44 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 1/18/13
|
||||
* Time: 7:44 PM
|
||||
*/
|
||||
interface FunctionEvaluator {
|
||||
int getArity();
|
||||
double eval();
|
||||
double eval(double x);
|
||||
double eval(double x, double y);
|
||||
int getArity();
|
||||
|
||||
double eval();
|
||||
|
||||
double eval(double x);
|
||||
|
||||
double eval(double x, double y);
|
||||
}
|
||||
|
@@ -14,101 +14,101 @@ import java.util.*;
|
||||
*/
|
||||
public class MapPlotResourceManager implements PlotResourceManager {
|
||||
|
||||
@NotNull
|
||||
private Map<PlotLineDef, List<PlotLineDef>> registeredLineDefsMap = new HashMap<PlotLineDef, List<PlotLineDef>>();
|
||||
@NotNull
|
||||
private Map<PlotLineDef, List<PlotLineDef>> registeredLineDefsMap = new HashMap<PlotLineDef, List<PlotLineDef>>();
|
||||
|
||||
@NotNull
|
||||
private final List<PlotLineDef> preparedLineDefs = new ArrayList<PlotLineDef>(PlotLineStyle.values().length * PlotLineColor.values().length);
|
||||
@NotNull
|
||||
private final List<PlotLineDef> preparedLineDefs = new ArrayList<PlotLineDef>(PlotLineStyle.values().length * PlotLineColor.values().length);
|
||||
|
||||
public MapPlotResourceManager() {
|
||||
for (PlotLineStyle plotLineStyle : PlotLineStyle.values()) {
|
||||
for (PlotLineColor plotLineColor : PlotLineColor.values()) {
|
||||
preparedLineDefs.add(PlotLineDef.newInstance(plotLineColor.getColor(), plotLineStyle));
|
||||
}
|
||||
}
|
||||
}
|
||||
public MapPlotResourceManager() {
|
||||
for (PlotLineStyle plotLineStyle : PlotLineStyle.values()) {
|
||||
for (PlotLineColor plotLineColor : PlotLineColor.values()) {
|
||||
preparedLineDefs.add(PlotLineDef.newInstance(plotLineColor.getColor(), plotLineStyle));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotLineDef generateAndRegister() {
|
||||
synchronized (this) {
|
||||
for (PlotLineDef lineDef : preparedLineDefs) {
|
||||
final List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(lineDef);
|
||||
if ( registeredLineDefs == null || registeredLineDefs.isEmpty() ) {
|
||||
register(lineDef);
|
||||
return lineDef;
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public PlotLineDef generateAndRegister() {
|
||||
synchronized (this) {
|
||||
for (PlotLineDef lineDef : preparedLineDefs) {
|
||||
final List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(lineDef);
|
||||
if (registeredLineDefs == null || registeredLineDefs.isEmpty()) {
|
||||
register(lineDef);
|
||||
return lineDef;
|
||||
}
|
||||
}
|
||||
|
||||
return preparedLineDefs.get(0);
|
||||
}
|
||||
}
|
||||
return preparedLineDefs.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void addLineDef(@NotNull final PlotLineDef toBeAdded) {
|
||||
assert Thread.holdsLock(this);
|
||||
private void addLineDef(@NotNull final PlotLineDef toBeAdded) {
|
||||
assert Thread.holdsLock(this);
|
||||
|
||||
List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(toBeAdded);
|
||||
if ( registeredLineDefs == null ) {
|
||||
registeredLineDefs = new ArrayList<PlotLineDef>();
|
||||
registeredLineDefsMap.put(toBeAdded, registeredLineDefs);
|
||||
}
|
||||
List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(toBeAdded);
|
||||
if (registeredLineDefs == null) {
|
||||
registeredLineDefs = new ArrayList<PlotLineDef>();
|
||||
registeredLineDefsMap.put(toBeAdded, registeredLineDefs);
|
||||
}
|
||||
|
||||
try {
|
||||
Iterables.find(registeredLineDefs, new Predicate<PlotLineDef>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotLineDef lineDef) {
|
||||
return lineDef == toBeAdded;
|
||||
}
|
||||
});
|
||||
try {
|
||||
Iterables.find(registeredLineDefs, new Predicate<PlotLineDef>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotLineDef lineDef) {
|
||||
return lineDef == toBeAdded;
|
||||
}
|
||||
});
|
||||
|
||||
// already added
|
||||
// already added
|
||||
|
||||
} catch (NoSuchElementException e) {
|
||||
registeredLineDefs.add(toBeAdded);
|
||||
}
|
||||
} catch (NoSuchElementException e) {
|
||||
registeredLineDefs.add(toBeAdded);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void removeLineDef(@NotNull final PlotLineDef toBeRemoved) {
|
||||
assert Thread.holdsLock(this);
|
||||
private void removeLineDef(@NotNull final PlotLineDef toBeRemoved) {
|
||||
assert Thread.holdsLock(this);
|
||||
|
||||
List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(toBeRemoved);
|
||||
List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(toBeRemoved);
|
||||
|
||||
if (registeredLineDefs != null) {
|
||||
Iterables.removeIf(registeredLineDefs, new Predicate<PlotLineDef>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotLineDef lineDef) {
|
||||
return lineDef == toBeRemoved;
|
||||
}
|
||||
});
|
||||
if (registeredLineDefs != null) {
|
||||
Iterables.removeIf(registeredLineDefs, new Predicate<PlotLineDef>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PlotLineDef lineDef) {
|
||||
return lineDef == toBeRemoved;
|
||||
}
|
||||
});
|
||||
|
||||
if ( registeredLineDefs.isEmpty() ) {
|
||||
registeredLineDefsMap.remove(toBeRemoved);
|
||||
}
|
||||
if (registeredLineDefs.isEmpty()) {
|
||||
registeredLineDefsMap.remove(toBeRemoved);
|
||||
}
|
||||
|
||||
} else {
|
||||
registeredLineDefsMap.remove(toBeRemoved);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
registeredLineDefsMap.remove(toBeRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(@NotNull PlotLineDef lineDef) {
|
||||
synchronized (this) {
|
||||
addLineDef(lineDef);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void register(@NotNull PlotLineDef lineDef) {
|
||||
synchronized (this) {
|
||||
addLineDef(lineDef);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregister(@NotNull PlotLineDef lineDef) {
|
||||
synchronized (this) {
|
||||
removeLineDef(lineDef);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void unregister(@NotNull PlotLineDef lineDef) {
|
||||
synchronized (this) {
|
||||
removeLineDef(lineDef);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterAll() {
|
||||
synchronized (this) {
|
||||
registeredLineDefsMap.clear();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void unregisterAll() {
|
||||
synchronized (this) {
|
||||
registeredLineDefsMap.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,99 +5,99 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/19/13
|
||||
* Time: 4:51 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 1/19/13
|
||||
* Time: 4:51 PM
|
||||
*/
|
||||
public final class PlotBoundaries implements Serializable {
|
||||
|
||||
public static final float DEFAULT_MIN_NUMBER = -10f;
|
||||
|
||||
public static final float DEFAULT_MAX_NUMBER = 10f;
|
||||
|
||||
public static final float DEFAULT_MIN_NUMBER = -10f;
|
||||
|
||||
private float xMin;
|
||||
private float xMax;
|
||||
private float yMin;
|
||||
private float yMax;
|
||||
public static final float DEFAULT_MAX_NUMBER = 10f;
|
||||
|
||||
public PlotBoundaries() {
|
||||
}
|
||||
|
||||
PlotBoundaries(float xMin, float xMax, float yMin, float yMax) {
|
||||
this.xMin = Math.min(xMin, xMax);
|
||||
this.xMax = Math.max(xMin, xMax);
|
||||
this.yMin = Math.min(yMin, yMax);
|
||||
this.yMax = Math.max(yMin, yMax);
|
||||
}
|
||||
private float xMin;
|
||||
private float xMax;
|
||||
private float yMin;
|
||||
private float yMax;
|
||||
|
||||
@NotNull
|
||||
public static PlotBoundaries newInstance(float xMin, float xMax, float yMin, float yMax) {
|
||||
return new PlotBoundaries(xMin, xMax, yMin, yMax);
|
||||
}
|
||||
public PlotBoundaries() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotBoundaries newInstance(float xMin, float xMax) {
|
||||
return newInstance(xMin, xMax, DEFAULT_MIN_NUMBER, DEFAULT_MAX_NUMBER);
|
||||
}
|
||||
PlotBoundaries(float xMin, float xMax, float yMin, float yMax) {
|
||||
this.xMin = Math.min(xMin, xMax);
|
||||
this.xMax = Math.max(xMin, xMax);
|
||||
this.yMin = Math.min(yMin, yMax);
|
||||
this.yMax = Math.max(yMin, yMax);
|
||||
}
|
||||
|
||||
public float getXMin() {
|
||||
return xMin;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotBoundaries newInstance(float xMin, float xMax, float yMin, float yMax) {
|
||||
return new PlotBoundaries(xMin, xMax, yMin, yMax);
|
||||
}
|
||||
|
||||
public float getXMax() {
|
||||
return xMax;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotBoundaries newInstance(float xMin, float xMax) {
|
||||
return newInstance(xMin, xMax, DEFAULT_MIN_NUMBER, DEFAULT_MAX_NUMBER);
|
||||
}
|
||||
|
||||
public float getYMin() {
|
||||
return yMin;
|
||||
}
|
||||
public float getXMin() {
|
||||
return xMin;
|
||||
}
|
||||
|
||||
public float getYMax() {
|
||||
return yMax;
|
||||
}
|
||||
public float getXMax() {
|
||||
return xMax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PlotBoundaries{" +
|
||||
"yMax=" + yMax +
|
||||
", yMin=" + yMin +
|
||||
", xMax=" + xMax +
|
||||
", xMin=" + xMin +
|
||||
'}';
|
||||
}
|
||||
public float getYMin() {
|
||||
return yMin;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotBoundaries newDefaultInstance() {
|
||||
PlotBoundaries plotBoundaries = new PlotBoundaries();
|
||||
plotBoundaries.xMin = DEFAULT_MIN_NUMBER;
|
||||
plotBoundaries.yMin = DEFAULT_MIN_NUMBER;
|
||||
plotBoundaries.xMax = DEFAULT_MAX_NUMBER;
|
||||
plotBoundaries.yMax = DEFAULT_MAX_NUMBER;
|
||||
return plotBoundaries;
|
||||
}
|
||||
public float getYMax() {
|
||||
return yMax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PlotBoundaries)) return false;
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PlotBoundaries{" +
|
||||
"yMax=" + yMax +
|
||||
", yMin=" + yMin +
|
||||
", xMax=" + xMax +
|
||||
", xMin=" + xMin +
|
||||
'}';
|
||||
}
|
||||
|
||||
PlotBoundaries that = (PlotBoundaries) o;
|
||||
@NotNull
|
||||
public static PlotBoundaries newDefaultInstance() {
|
||||
PlotBoundaries plotBoundaries = new PlotBoundaries();
|
||||
plotBoundaries.xMin = DEFAULT_MIN_NUMBER;
|
||||
plotBoundaries.yMin = DEFAULT_MIN_NUMBER;
|
||||
plotBoundaries.xMax = DEFAULT_MAX_NUMBER;
|
||||
plotBoundaries.yMax = DEFAULT_MAX_NUMBER;
|
||||
return plotBoundaries;
|
||||
}
|
||||
|
||||
if (Float.compare(that.xMax, xMax) != 0) return false;
|
||||
if (Float.compare(that.xMin, xMin) != 0) return false;
|
||||
if (Float.compare(that.yMax, yMax) != 0) return false;
|
||||
if (Float.compare(that.yMin, yMin) != 0) return false;
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PlotBoundaries)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
PlotBoundaries that = (PlotBoundaries) o;
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (xMin != +0.0f ? Float.floatToIntBits(xMin) : 0);
|
||||
result = 31 * result + (xMax != +0.0f ? Float.floatToIntBits(xMax) : 0);
|
||||
result = 31 * result + (yMin != +0.0f ? Float.floatToIntBits(yMin) : 0);
|
||||
result = 31 * result + (yMax != +0.0f ? Float.floatToIntBits(yMax) : 0);
|
||||
return result;
|
||||
}
|
||||
if (Float.compare(that.xMax, xMax) != 0) return false;
|
||||
if (Float.compare(that.xMin, xMin) != 0) return false;
|
||||
if (Float.compare(that.yMax, yMax) != 0) return false;
|
||||
if (Float.compare(that.yMin, yMin) != 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (xMin != +0.0f ? Float.floatToIntBits(xMin) : 0);
|
||||
result = 31 * result + (xMax != +0.0f ? Float.floatToIntBits(xMax) : 0);
|
||||
result = 31 * result + (yMin != +0.0f ? Float.floatToIntBits(yMin) : 0);
|
||||
result = 31 * result + (yMax != +0.0f ? Float.floatToIntBits(yMax) : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -11,33 +11,33 @@ import java.util.List;
|
||||
*/
|
||||
public class PlotData {
|
||||
|
||||
@NotNull
|
||||
private List<PlotFunction> functions;
|
||||
@NotNull
|
||||
private List<PlotFunction> functions;
|
||||
|
||||
private boolean plot3d;
|
||||
private boolean plot3d;
|
||||
|
||||
@NotNull
|
||||
private PlotBoundaries boundaries;
|
||||
@NotNull
|
||||
private PlotBoundaries boundaries;
|
||||
|
||||
public PlotData(@NotNull List<PlotFunction> functions,
|
||||
boolean plot3d,
|
||||
@NotNull PlotBoundaries boundaries) {
|
||||
this.functions = functions;
|
||||
this.plot3d = plot3d;
|
||||
this.boundaries = boundaries;
|
||||
}
|
||||
public PlotData(@NotNull List<PlotFunction> functions,
|
||||
boolean plot3d,
|
||||
@NotNull PlotBoundaries boundaries) {
|
||||
this.functions = functions;
|
||||
this.plot3d = plot3d;
|
||||
this.boundaries = boundaries;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<PlotFunction> getFunctions() {
|
||||
return functions;
|
||||
}
|
||||
@NotNull
|
||||
public List<PlotFunction> getFunctions() {
|
||||
return functions;
|
||||
}
|
||||
|
||||
public boolean isPlot3d() {
|
||||
return plot3d;
|
||||
}
|
||||
public boolean isPlot3d() {
|
||||
return plot3d;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public PlotBoundaries getBoundaries() {
|
||||
return boundaries;
|
||||
}
|
||||
@NotNull
|
||||
public PlotBoundaries getBoundaries() {
|
||||
return boundaries;
|
||||
}
|
||||
}
|
||||
|
@@ -9,36 +9,36 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class PlotFunction {
|
||||
|
||||
@NotNull
|
||||
private XyFunction xyFunction;
|
||||
@NotNull
|
||||
private XyFunction xyFunction;
|
||||
|
||||
@NotNull
|
||||
private PlotLineDef plotLineDef;
|
||||
@NotNull
|
||||
private PlotLineDef plotLineDef;
|
||||
|
||||
private boolean pinned = false;
|
||||
private boolean pinned = false;
|
||||
|
||||
private boolean visible = true;
|
||||
private boolean visible = true;
|
||||
|
||||
public PlotFunction(@NotNull XyFunction xyFunction) {
|
||||
this.xyFunction = xyFunction;
|
||||
this.plotLineDef = PlotLineDef.newDefaultInstance();
|
||||
}
|
||||
public PlotFunction(@NotNull XyFunction xyFunction) {
|
||||
this.xyFunction = xyFunction;
|
||||
this.plotLineDef = PlotLineDef.newDefaultInstance();
|
||||
}
|
||||
|
||||
public PlotFunction(@NotNull XyFunction xyFunction,
|
||||
@NotNull PlotLineDef plotLineDef) {
|
||||
this.xyFunction = xyFunction;
|
||||
this.plotLineDef = plotLineDef;
|
||||
}
|
||||
public PlotFunction(@NotNull XyFunction xyFunction,
|
||||
@NotNull PlotLineDef plotLineDef) {
|
||||
this.xyFunction = xyFunction;
|
||||
this.plotLineDef = plotLineDef;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PlotFunction copy() {
|
||||
final PlotFunction copy = new PlotFunction(this.xyFunction, this.plotLineDef);
|
||||
@NotNull
|
||||
private PlotFunction copy() {
|
||||
final PlotFunction copy = new PlotFunction(this.xyFunction, this.plotLineDef);
|
||||
|
||||
copy.pinned = this.pinned;
|
||||
copy.visible = this.visible;
|
||||
copy.pinned = this.pinned;
|
||||
copy.visible = this.visible;
|
||||
|
||||
return copy;
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction changePlotLineDef(@NotNull PlotFunction that, @NotNull PlotLineDef newPlotLineDef) {
|
||||
@@ -48,71 +48,71 @@ public class PlotFunction {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction pin(@NotNull PlotFunction that) {
|
||||
return togglePinned(that, true);
|
||||
}
|
||||
public static PlotFunction pin(@NotNull PlotFunction that) {
|
||||
return togglePinned(that, true);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction togglePinned(@NotNull PlotFunction that, boolean pinned) {
|
||||
final PlotFunction copy = that.copy();
|
||||
copy.pinned = pinned;
|
||||
return copy;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotFunction togglePinned(@NotNull PlotFunction that, boolean pinned) {
|
||||
final PlotFunction copy = that.copy();
|
||||
copy.pinned = pinned;
|
||||
return copy;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction unpin(@NotNull PlotFunction that) {
|
||||
return togglePinned(that, false);
|
||||
}
|
||||
@NotNull
|
||||
public static PlotFunction unpin(@NotNull PlotFunction that) {
|
||||
return togglePinned(that, false);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction visible(@NotNull PlotFunction that) {
|
||||
return toggleVisible(that, true);
|
||||
}
|
||||
@NotNull
|
||||
public static PlotFunction visible(@NotNull PlotFunction that) {
|
||||
return toggleVisible(that, true);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction toggleVisible(@NotNull PlotFunction that, boolean visible) {
|
||||
final PlotFunction copy = that.copy();
|
||||
copy.visible = visible;
|
||||
return copy;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotFunction toggleVisible(@NotNull PlotFunction that, boolean visible) {
|
||||
final PlotFunction copy = that.copy();
|
||||
copy.visible = visible;
|
||||
return copy;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotFunction invisible(@NotNull PlotFunction that) {
|
||||
return toggleVisible(that, false);
|
||||
}
|
||||
@NotNull
|
||||
public static PlotFunction invisible(@NotNull PlotFunction that) {
|
||||
return toggleVisible(that, false);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public XyFunction getXyFunction() {
|
||||
return xyFunction;
|
||||
}
|
||||
@NotNull
|
||||
public XyFunction getXyFunction() {
|
||||
return xyFunction;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public PlotLineDef getPlotLineDef() {
|
||||
return plotLineDef;
|
||||
}
|
||||
@NotNull
|
||||
public PlotLineDef getPlotLineDef() {
|
||||
return plotLineDef;
|
||||
}
|
||||
|
||||
public boolean isPinned() {
|
||||
return pinned;
|
||||
}
|
||||
public boolean isPinned() {
|
||||
return pinned;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PlotFunction)) return false;
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PlotFunction)) return false;
|
||||
|
||||
PlotFunction that = (PlotFunction) o;
|
||||
PlotFunction that = (PlotFunction) o;
|
||||
|
||||
if (!xyFunction.equals(that.xyFunction)) return false;
|
||||
if (!xyFunction.equals(that.xyFunction)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return xyFunction.hashCode();
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return xyFunction.hashCode();
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return visible;
|
||||
}
|
||||
public boolean isVisible() {
|
||||
return visible;
|
||||
}
|
||||
}
|
||||
|
@@ -9,35 +9,35 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public enum PlotLineColor {
|
||||
|
||||
// Color.WHITE
|
||||
white(0xFFFFFFFF),
|
||||
// Color.WHITE
|
||||
white(0xFFFFFFFF),
|
||||
|
||||
blue(0xFF10648C),
|
||||
blue(0xFF10648C),
|
||||
|
||||
// Color.RED
|
||||
red(0xFFFF0000),
|
||||
// Color.RED
|
||||
red(0xFFFF0000),
|
||||
|
||||
// Color.GREEN
|
||||
green(0xFF00FF00),
|
||||
// Color.GREEN
|
||||
green(0xFF00FF00),
|
||||
|
||||
// Color.GRAY
|
||||
grey(0xFF888888);
|
||||
// Color.GRAY
|
||||
grey(0xFF888888);
|
||||
|
||||
private final int color;
|
||||
private final int color;
|
||||
|
||||
private PlotLineColor(int color) {
|
||||
this.color = color;
|
||||
}
|
||||
private PlotLineColor(int color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
|
||||
public int getColor() {
|
||||
return this.color;
|
||||
}
|
||||
public int getColor() {
|
||||
return this.color;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotLineColor valueOf(int color) {
|
||||
for (PlotLineColor plotLineColor : PlotLineColor.values()) {
|
||||
if ( plotLineColor.color == color ) {
|
||||
if (plotLineColor.color == color) {
|
||||
return plotLineColor;
|
||||
}
|
||||
}
|
||||
|
@@ -8,5 +8,5 @@ package org.solovyev.android.calculator.plot;
|
||||
public enum PlotLineColorType {
|
||||
|
||||
monochrome,
|
||||
color_map;
|
||||
color_map;
|
||||
}
|
||||
|
@@ -9,67 +9,67 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class PlotLineDef {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private static final Float DEFAULT_LINE_WIDTH = 0f;
|
||||
@NotNull
|
||||
private static final Float DEFAULT_LINE_WIDTH = 0f;
|
||||
|
||||
private static final int WHITE = 0xFFFFFFFF;
|
||||
private static final int WHITE = 0xFFFFFFFF;
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private PlotLineColorType lineColorType = PlotLineColorType.monochrome;
|
||||
@NotNull
|
||||
private PlotLineColorType lineColorType = PlotLineColorType.monochrome;
|
||||
|
||||
private int lineColor = WHITE;
|
||||
private int lineColor = WHITE;
|
||||
|
||||
@NotNull
|
||||
private PlotLineStyle lineStyle = PlotLineStyle.solid;
|
||||
@NotNull
|
||||
private PlotLineStyle lineStyle = PlotLineStyle.solid;
|
||||
|
||||
private float lineWidth = DEFAULT_LINE_WIDTH;
|
||||
private float lineWidth = DEFAULT_LINE_WIDTH;
|
||||
|
||||
private PlotLineDef() {
|
||||
}
|
||||
private PlotLineDef() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle) {
|
||||
final PlotLineDef result = new PlotLineDef();
|
||||
result.lineColor = lineColor;
|
||||
result.lineStyle = lineStyle;
|
||||
return result;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle) {
|
||||
final PlotLineDef result = new PlotLineDef();
|
||||
result.lineColor = lineColor;
|
||||
result.lineStyle = lineStyle;
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth) {
|
||||
final PlotLineDef result = new PlotLineDef();
|
||||
result.lineColor = lineColor;
|
||||
result.lineStyle = lineStyle;
|
||||
result.lineWidth = lineWidth;
|
||||
return result;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth) {
|
||||
final PlotLineDef result = new PlotLineDef();
|
||||
result.lineColor = lineColor;
|
||||
result.lineStyle = lineStyle;
|
||||
result.lineWidth = lineWidth;
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth, @NotNull PlotLineColorType lineColorType) {
|
||||
final PlotLineDef result = new PlotLineDef();
|
||||
result.lineColor = lineColor;
|
||||
result.lineColorType = lineColorType;
|
||||
result.lineStyle = lineStyle;
|
||||
result.lineWidth = lineWidth;
|
||||
return result;
|
||||
}
|
||||
@NotNull
|
||||
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth, @NotNull PlotLineColorType lineColorType) {
|
||||
final PlotLineDef result = new PlotLineDef();
|
||||
result.lineColor = lineColor;
|
||||
result.lineColorType = lineColorType;
|
||||
result.lineStyle = lineStyle;
|
||||
result.lineWidth = lineWidth;
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PlotLineDef copy() {
|
||||
@@ -117,52 +117,50 @@ public class PlotLineDef {
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public static PlotLineDef newDefaultInstance() {
|
||||
return new PlotLineDef();
|
||||
}
|
||||
|
||||
public int getLineColor() {
|
||||
return lineColor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PlotLineDef newDefaultInstance() {
|
||||
return new PlotLineDef();
|
||||
}
|
||||
public PlotLineStyle getLineStyle() {
|
||||
return lineStyle;
|
||||
}
|
||||
|
||||
public int getLineColor() {
|
||||
return lineColor;
|
||||
}
|
||||
public float getLineWidth() {
|
||||
return lineWidth;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public PlotLineStyle getLineStyle() {
|
||||
return lineStyle;
|
||||
}
|
||||
@NotNull
|
||||
public PlotLineColorType getLineColorType() {
|
||||
return lineColorType;
|
||||
}
|
||||
|
||||
public float getLineWidth() {
|
||||
return lineWidth;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PlotLineDef)) return false;
|
||||
|
||||
@NotNull
|
||||
public PlotLineColorType getLineColorType() {
|
||||
return lineColorType;
|
||||
}
|
||||
PlotLineDef that = (PlotLineDef) o;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PlotLineDef)) return false;
|
||||
if (lineColor != that.lineColor) return false;
|
||||
if (Float.compare(that.lineWidth, lineWidth) != 0) return false;
|
||||
if (lineColorType != that.lineColorType) return false;
|
||||
if (lineStyle != that.lineStyle) return false;
|
||||
|
||||
PlotLineDef that = (PlotLineDef) o;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (lineColor != that.lineColor) return false;
|
||||
if (Float.compare(that.lineWidth, lineWidth) != 0) return false;
|
||||
if (lineColorType != that.lineColorType) return false;
|
||||
if (lineStyle != that.lineStyle) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = lineColorType.hashCode();
|
||||
result = 31 * result + lineColor;
|
||||
result = 31 * result + lineStyle.hashCode();
|
||||
result = 31 * result + (lineWidth != +0.0f ? Float.floatToIntBits(lineWidth) : 0);
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = lineColorType.hashCode();
|
||||
result = 31 * result + lineColor;
|
||||
result = 31 * result + lineStyle.hashCode();
|
||||
result = 31 * result + (lineWidth != +0.0f ? Float.floatToIntBits(lineWidth) : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -8,9 +8,9 @@ package org.solovyev.android.calculator.plot;
|
||||
*/
|
||||
public enum PlotLineStyle {
|
||||
|
||||
solid,
|
||||
dashed,
|
||||
dotted,
|
||||
dash_dotted;
|
||||
solid,
|
||||
dashed,
|
||||
dotted,
|
||||
dash_dotted;
|
||||
|
||||
}
|
||||
|
@@ -9,12 +9,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
interface PlotResourceManager {
|
||||
|
||||
@NotNull
|
||||
PlotLineDef generateAndRegister();
|
||||
@NotNull
|
||||
PlotLineDef generateAndRegister();
|
||||
|
||||
void register(@NotNull PlotLineDef lineDef);
|
||||
void register(@NotNull PlotLineDef lineDef);
|
||||
|
||||
void unregister(@NotNull PlotLineDef lineDef);
|
||||
void unregister(@NotNull PlotLineDef lineDef);
|
||||
|
||||
void unregisterAll();
|
||||
void unregisterAll();
|
||||
}
|
||||
|
@@ -24,43 +24,43 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public final class PlotUtils {
|
||||
|
||||
// not intended for instantiation
|
||||
// not intended for instantiation
|
||||
private PlotUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Complex calculatorExpression(@NotNull Generic expression) {
|
||||
try {
|
||||
return unwrap(expression.numeric());
|
||||
} catch (RuntimeException e) {
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public static Complex calculatorExpression(@NotNull Generic expression) {
|
||||
try {
|
||||
return unwrap(expression.numeric());
|
||||
} catch (RuntimeException e) {
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public static Complex calculatorExpression(@NotNull Generic expression, @NotNull Constant xVar, double x) {
|
||||
try {
|
||||
return unwrap(expression.substitute(xVar, Expression.valueOf(x)).numeric());
|
||||
} catch (RuntimeException e) {
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
try {
|
||||
return unwrap(expression.substitute(xVar, Expression.valueOf(x)).numeric());
|
||||
} catch (RuntimeException e) {
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Complex calculatorExpression(@NotNull Generic expression, @NotNull Constant xVar, double x, @NotNull Constant yVar, double y) {
|
||||
try {
|
||||
Generic tmp = expression.substitute(xVar, Expression.valueOf(x));
|
||||
tmp = tmp.substitute(yVar, Expression.valueOf(y));
|
||||
return unwrap(tmp.numeric());
|
||||
} catch (RuntimeException e) {
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public static Complex calculatorExpression(@NotNull Generic expression, @NotNull Constant xVar, double x, @NotNull Constant yVar, double y) {
|
||||
try {
|
||||
Generic tmp = expression.substitute(xVar, Expression.valueOf(x));
|
||||
tmp = tmp.substitute(yVar, Expression.valueOf(y));
|
||||
return unwrap(tmp.numeric());
|
||||
} catch (RuntimeException e) {
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
|
||||
private static final Complex NaN = Complex.valueOf(Double.NaN, 0d);
|
||||
private static final Complex NaN = Complex.valueOf(Double.NaN, 0d);
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public static Complex unwrap(@Nullable Generic numeric) {
|
||||
if (numeric instanceof JsclInteger) {
|
||||
return Complex.valueOf(((JsclInteger) numeric).intValue(), 0d);
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user