New architecture

This commit is contained in:
Sergey Solovyev
2012-09-22 15:02:38 +04:00
parent d0fe0ca012
commit 3661774d9b
28 changed files with 741 additions and 462 deletions

View File

@@ -0,0 +1,19 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 9/22/12
* Time: 1:34 PM
*/
public interface CalculatorClipboard {
@Nullable
String getText();
void setText(@NotNull String text);
void setText(@NotNull CharSequence text);
}

View File

@@ -30,6 +30,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
public CalculatorDisplayImpl(@NotNull Calculator calculator) {
this.calculator = calculator;
this.lastCalculatorEventData = CalculatorEventDataImpl.newInstance(calculator.createFirstEventDataId());
this.calculator.addCalculatorEventListener(this);
}
@Override

View File

@@ -59,6 +59,9 @@ public interface CalculatorEditor extends CalculatorEventListener/*, CursorContr
@NotNull
CalculatorEditorViewState erase();
@NotNull
CalculatorEditorViewState clear();
@NotNull
CalculatorEditorViewState setText(@NotNull String text);
@@ -68,6 +71,9 @@ public interface CalculatorEditor extends CalculatorEventListener/*, CursorContr
@NotNull
CalculatorEditorViewState insert(@NotNull String text);
@NotNull
CalculatorEditorViewState insert(@NotNull String text, int selectionOffset);
@NotNull
CalculatorEditorViewState moveSelection(int offset);

View File

@@ -24,6 +24,7 @@ public class CalculatorEditorImpl implements CalculatorEditor {
public CalculatorEditorImpl(@NotNull Calculator calculator) {
this.calculator = calculator;
this.calculator.addCalculatorEventListener(this);
}
@Override
@@ -130,6 +131,14 @@ public class CalculatorEditorImpl implements CalculatorEditor {
}
}
@NotNull
@Override
public CalculatorEditorViewState clear() {
synchronized (viewLock) {
return setText("");
}
}
@NotNull
@Override
public CalculatorEditorViewState setText(@NotNull String text) {
@@ -155,17 +164,27 @@ public class CalculatorEditorImpl implements CalculatorEditor {
@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();
final StringBuilder newText = new StringBuilder(text.length() + oldText.length());
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));
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), text.length() + selection);
int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength);
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection);
setViewState(result);
return result;
}
@@ -194,8 +213,12 @@ public class CalculatorEditorImpl implements CalculatorEditor {
}
private int correctSelection(int selection, @NotNull String text) {
return correctSelection(selection, text.length());
}
private int correctSelection(int selection, int textLength) {
int result = Math.max(selection, 0);
result = Math.min(result, text.length());
result = Math.min(result, textLength);
return result;
}
}

View File

@@ -3,6 +3,7 @@ package org.solovyev.android.calculator;
import jscl.AbstractJsclArithmeticException;
import jscl.NumeralBase;
import jscl.NumeralBaseException;
import jscl.math.Expression;
import jscl.math.Generic;
import jscl.text.ParseInterruptedException;
import org.jetbrains.annotations.NotNull;
@@ -36,9 +37,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull
private final AtomicLong counter = new AtomicLong(FIRST_ID);
@NotNull
private final Object lock = new Object();
@NotNull
private final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance();
@@ -208,13 +206,19 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull JsclOperation operation,
@NotNull String expression,
@Nullable MessageRegistry mr) {
synchronized (lock) {
PreparedExpression preparedExpression = null;
PreparedExpression preparedExpression = null;
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_started, new CalculatorInputImpl(expression, operation));
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_started, new CalculatorInputImpl(expression, operation));
try {
try {
expression = expression.trim();
if ( StringUtils.isEmpty(expression) ) {
final CalculatorOutputImpl data = new CalculatorOutputImpl("", operation, Expression.valueOf(""));
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
} else {
preparedExpression = preprocessor.process(expression);
final String jsclExpression = preparedExpression.toString();
@@ -232,23 +236,23 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
} catch (AbstractJsclArithmeticException e) {
handleException(sequenceId, operation, expression, mr, new CalculatorEvalException(e, e, jsclExpression));
}
} catch (ArithmeticException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
} catch (StackOverflowError e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
} catch (jscl.text.ParseException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
} catch (ParseInterruptedException e) {
// do nothing - we ourselves interrupt the calculations
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
} catch (CalculatorParseException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, e);
} finally {
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_finished, null);
}
} catch (ArithmeticException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage())));
} catch (StackOverflowError e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error)));
} catch (jscl.text.ParseException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e));
} catch (ParseInterruptedException e) {
// do nothing - we ourselves interrupt the calculations
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
} catch (CalculatorParseException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, e);
} finally {
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_finished, null);
}
}
@@ -259,7 +263,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
return new CalculatorEvaluationEventDataImpl(CalculatorEventDataImpl.newInstance(nextEventDataId(calculationId)), operation, expression);
}
private void handleException(@NotNull Long calculationId,
private void handleException(@NotNull Long sequenceId,
@NotNull JsclOperation operation,
@NotNull String expression,
@Nullable MessageRegistry mr,
@@ -270,11 +274,11 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
&& preparedExpression != null
&& preparedExpression.isExistsUndefinedVar()) {
evaluate(calculationId, JsclOperation.simplify, expression, mr);
evaluate(sequenceId, JsclOperation.simplify, expression, mr);
} else {
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(parseException));
}
fireCalculatorEvent(newCalculationEventData(operation, expression, calculationId), CalculatorEventType.calculation_failed, new CalculatorFailureImpl(parseException));
}
private void handleException(@NotNull Long calculationId,
@@ -323,7 +327,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
if (calculatorEventType == CalculatorEventType.editor_state_changed) {
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
final String newText = changeEventData.getNewState().getText();
final String oldText = changeEventData.getOldState().getText();
if ( !newText.equals(oldText) ) {
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
}
}
}

View File

@@ -0,0 +1,21 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 9/22/12
* Time: 1:08 PM
*/
public interface CalculatorKeyboard {
void digitButtonPressed(@Nullable String text);
void roundBracketsButtonPressed();
void pasteButtonPressed();
void clearButtonPressed();
void copyButtonPressed();
}

View File

@@ -0,0 +1,97 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.text.StringUtils;
/**
* User: serso
* Date: 9/22/12
* Time: 1:08 PM
*/
public class CalculatorKeyboardImpl implements CalculatorKeyboard {
@NotNull
private final Calculator calculator;
public CalculatorKeyboardImpl(@NotNull Calculator calculator) {
this.calculator = calculator;
}
@Override
public void digitButtonPressed(@Nullable final String text) {
if (!StringUtils.isEmpty(text)) {
assert text != null;
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;
}
if (cursorPositionOffset == 0) {
if (MathType.openGroupSymbols.contains(text)) {
cursorPositionOffset = -1;
}
}
final CalculatorEditor editor = CalculatorLocatorImpl.getInstance().getCalculatorEditor();
editor.insert(textToBeInserted.toString(), cursorPositionOffset);
}
}
@Override
public void roundBracketsButtonPressed() {
final CalculatorEditor editor = CalculatorLocatorImpl.getInstance().getCalculatorEditor();
CalculatorEditorViewState viewState = editor.getViewState();
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);
}
@Override
public void pasteButtonPressed() {
final String text = CalculatorLocatorImpl.getInstance().getCalculatorClipboard().getText();
if (text != null) {
CalculatorLocatorImpl.getInstance().getCalculatorEditor().insert(text);
}
}
@Override
public void clearButtonPressed() {
CalculatorLocatorImpl.getInstance().getCalculatorEditor().clear();
}
@Override
public void copyButtonPressed() {
final CalculatorDisplayViewState displayViewState = CalculatorLocatorImpl.getInstance().getCalculatorDisplay().getViewState();
if (displayViewState.isValid()) {
final CharSequence text = displayViewState.getText();
if (!StringUtils.isEmpty(text)) {
CalculatorLocatorImpl.getInstance().getCalculatorClipboard().setText(text);
CalculatorLocatorImpl.getInstance().getCalculatorNotifier().showMessage(CalculatorMessage.newInfoMessage(CalculatorMessages.result_copied));
}
}
}
}

View File

@@ -1,25 +1,38 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: Solovyev_S
* Date: 20.09.12
* Time: 12:45
*/
public interface CalculatorLocator {
@NotNull
JCalculatorEngine getCalculatorEngine();
@NotNull
Calculator getCalculator();
@NotNull
CalculatorDisplay getCalculatorDisplay();
@NotNull
CalculatorEditor getCalculatorEditor();
void setCalculatorEngine(@NotNull JCalculatorEngine calculatorEngine);
}
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: Solovyev_S
* Date: 20.09.12
* Time: 12:45
*/
public interface CalculatorLocator {
@NotNull
JCalculatorEngine getCalculatorEngine();
@NotNull
Calculator getCalculator();
@NotNull
CalculatorDisplay getCalculatorDisplay();
@NotNull
CalculatorEditor getCalculatorEditor();
void setCalculatorEngine(@NotNull JCalculatorEngine calculatorEngine);
@NotNull
CalculatorKeyboard getCalculatorKeyboard();
@NotNull
CalculatorClipboard getCalculatorClipboard();
void setCalculatorClipboard(@NotNull CalculatorClipboard calculatorClipboard);
@NotNull
CalculatorNotifier getCalculatorNotifier();
void setCalculatorNotifier(@NotNull CalculatorNotifier calculatorNotifier);
}

View File

@@ -21,6 +21,15 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
@NotNull
private final CalculatorDisplay calculatorDisplay = new CalculatorDisplayImpl(calculator);
@NotNull
private final CalculatorKeyboard calculatorKeyboard = new CalculatorKeyboardImpl(calculator);
@NotNull
private CalculatorNotifier calculatorNotifier = new DummyCalculatorNotifier();
@NotNull
private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard();
@NotNull
private static final CalculatorLocator instance = new CalculatorLocatorImpl();
@@ -60,4 +69,32 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
public CalculatorEditor getCalculatorEditor() {
return calculatorEditor;
}
@Override
@NotNull
public CalculatorKeyboard getCalculatorKeyboard() {
return calculatorKeyboard;
}
@Override
@NotNull
public CalculatorClipboard getCalculatorClipboard() {
return calculatorClipboard;
}
@Override
public void setCalculatorClipboard(@NotNull CalculatorClipboard calculatorClipboard) {
this.calculatorClipboard = calculatorClipboard;
}
@Override
@NotNull
public CalculatorNotifier getCalculatorNotifier() {
return calculatorNotifier;
}
@Override
public void setCalculatorNotifier(@NotNull CalculatorNotifier calculatorNotifier) {
this.calculatorNotifier = calculatorNotifier;
}
}

View File

@@ -1,7 +1,9 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.msg.AbstractMessage;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
@@ -15,14 +17,29 @@ import java.util.ResourceBundle;
*/
public class CalculatorMessage extends AbstractMessage {
protected CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @org.jetbrains.annotations.Nullable Object... parameters) {
public CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
super(messageCode, messageType, parameters);
}
protected CalculatorMessage(@NotNull String messageCode, @NotNull MessageType messageType, @NotNull List<?> 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 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);
}
@Override
protected String getMessagePattern(@NotNull Locale locale) {
final ResourceBundle rb = CalculatorMessages.getBundle(locale);

View File

@@ -17,6 +17,7 @@ public final class CalculatorMessages {
@NotNull
private static final Map<Locale, ResourceBundle> bundlesByLocale = new HashMap<Locale, ResourceBundle>();
private CalculatorMessages() {
throw new AssertionError();
}
@@ -40,30 +41,26 @@ public final class CalculatorMessages {
}
/* Arithmetic error occurred: {0} */
@NotNull
public static final String msg_001 = "msg_1";
/* Too complex expression*/
@NotNull
public static final String msg_002 = "msg_2";
/* Too long execution time - check the expression*/
@NotNull
public static final String msg_003 = "msg_3";
/* Evaluation was cancelled*/
@NotNull
public static final String msg_004 = "msg_4";
/* No parameters are specified for function: {0}*/
@NotNull
public static final String msg_005 = "msg_5";
/* Infinite loop is detected in expression*/
@NotNull
public static final String msg_006 = "msg_6";
/* Error */
@NotNull
public static final String syntax_error = "syntax_error";
/* Result copied to clipboard! */
public static final String result_copied = "result_copied";
}

View File

@@ -0,0 +1,14 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.msg.Message;
/**
* User: serso
* Date: 9/22/12
* Time: 1:52 PM
*/
public interface CalculatorNotifier {
void showMessage(@NotNull Message message);
}

View File

@@ -0,0 +1,24 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/22/12
* Time: 2:00 PM
*/
public class DummyCalculatorClipboard implements CalculatorClipboard {
@Override
public String getText() {
return null;
}
@Override
public void setText(@NotNull String text) {
}
@Override
public void setText(@NotNull CharSequence text) {
}
}

View File

@@ -0,0 +1,16 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.msg.Message;
/**
* User: serso
* Date: 9/22/12
* Time: 2:00 PM
*/
public class DummyCalculatorNotifier implements CalculatorNotifier {
@Override
public void showMessage(@NotNull Message message) {
}
}

View File

@@ -5,4 +5,5 @@ msg_4=Evaluation was cancelled
msg_5=No parameters are specified for function: {0}
msg_6=Infinite loop is detected in expression
syntax_error=Error
syntax_error=Error
result_copied=Result copied to clipboard!