From 4656e46c9980716fafa0f711bae52e892a20b464 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 5 Oct 2011 01:41:40 +0400 Subject: [PATCH] highlighting in editor + refactor --- res/layout-land/main.xml | 2 +- res/layout-port/main.xml | 2 +- res/layout/var_edit.xml | 1 + res/values/strings.xml | 4 +- .../calculator/CalculatorActivity.java | 2 +- .../calculator/CalculatorEditText.java | 131 ------------ .../android/calculator/CalculatorEditor.java | 157 ++++++++++++++ .../android/calculator/CalculatorModel.java | 2 +- .../calculator/CalculatorVarsActivity.java | 30 +-- .../android/calculator/CalculatorView.java | 30 ++- .../calculator/ToJsclPreprocessor.java | 79 ++++--- .../calculator/math/MathEntityType.java | 201 +++++++++--------- .../calculator/math/MathEntityTypeTest.java | 45 ++++ 13 files changed, 377 insertions(+), 309 deletions(-) delete mode 100644 src/main/java/org/solovyev/android/calculator/CalculatorEditText.java create mode 100644 src/main/java/org/solovyev/android/calculator/CalculatorEditor.java create mode 100644 src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java diff --git a/res/layout-land/main.xml b/res/layout-land/main.xml index 338c6962..e1979bd5 100644 --- a/res/layout-land/main.xml +++ b/res/layout-land/main.xml @@ -15,7 +15,7 @@ - - diff --git a/res/values/strings.xml b/res/values/strings.xml index 71633cc2..88ea3c15 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -31,7 +31,7 @@ paste vars - Color expressions + Highlight expressions Precision of result Exit Add @@ -43,7 +43,7 @@ Removal confirmation Do you really want to delete \'%s\' variable? Name - value + Value Description Create variable Edit variable diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 05fb36e3..2b6476f8 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -283,7 +283,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display)); assert colorExpressionsInBracketsDefault != null; - this.calculatorView.getEditor().setHighlightExpressionInBrackets(sharedPreferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault)); + this.calculatorView.getEditor().setHighlightText(sharedPreferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault)); this.calculatorView.evaluate(); } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java b/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java deleted file mode 100644 index 02c8b140..00000000 --- a/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.android.calculator; - -import android.content.Context; -import android.content.res.ColorStateList; -import android.graphics.Color; -import android.text.Html; -import android.util.AttributeSet; -import android.util.Log; -import android.widget.EditText; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.math.MathEntityType; - -/** - * User: serso - * Date: 9/17/11 - * Time: 12:25 AM - */ -public class CalculatorEditText extends EditText { - - private boolean highlightExpressionInBrackets = true; - - public CalculatorEditText(Context context) { - super(context); - } - - public CalculatorEditText(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public CalculatorEditText(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @Override - public void setTextColor(ColorStateList colors) { - super.setTextColor(colors); //To change body of overridden methods use File | Settings | File Templates. - } - - public void redraw() { - String text = getText().toString(); - - int selectionStart = getSelectionStart(); - int selectionEnd = getSelectionEnd(); - - if (highlightExpressionInBrackets) { - - int maxNumberOfOpenGroupSymbols = 0; - int numberOfOpenGroupSymbols = 0; - for (int i = 0; i < text.length(); i++) { - char ch = text.charAt(i); - if (MathEntityType.openGroupSymbols.contains(ch)) { - numberOfOpenGroupSymbols++; - maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols); - } else if (MathEntityType.closeGroupSymbols.contains(ch)) { - numberOfOpenGroupSymbols--; - } - } - - if (maxNumberOfOpenGroupSymbols > 0) { - - final StringBuilder sb = new StringBuilder(); - - processGroup(sb, text, 0, 0, maxNumberOfOpenGroupSymbols); - - Log.d(CalculatorEditText.class.getName(), sb.toString()); - - super.setText(Html.fromHtml(sb.toString()), BufferType.EDITABLE); - } else { - super.setText(text, BufferType.EDITABLE); - } - } else { - super.setText(text, BufferType.EDITABLE); - } - - setSelection(selectionStart, selectionEnd); - } - - private int processGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) { - - result.append(""); - - for (; i < s.length(); i++) { - char ch = s.charAt(i); - - if (MathEntityType.openGroupSymbols.contains(ch)) { - result.append(ch); - result.append(""); - i = processGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups); - result.append(""); - if (i < s.length() && MathEntityType.closeGroupSymbols.contains(s.charAt(i))) { - result.append(s.charAt(i)); - } - } else if (MathEntityType.closeGroupSymbols.contains(ch)) { - break; - } else { - result.append(ch); - } - } - - result.append(""); - - - return i; - } - - private String getColor(int numberOfOpenGroupSymbols, int numberOfOpenings) { - final int baseColor = Color.WHITE; - - double c = 1; - - int i = ((int) (255 * c)) * numberOfOpenings / (numberOfOpenGroupSymbols + 1); - - int result = Color.rgb(Color.red(baseColor) - i, Color.green(baseColor) - i, Color.blue(baseColor) - i); - - return "#" + Integer.toHexString(result).substring(2); - } - - public boolean isHighlightExpressionInBrackets() { - return highlightExpressionInBrackets; - } - - public void setHighlightExpressionInBrackets(boolean highlightExpressionInBrackets) { - this.highlightExpressionInBrackets = highlightExpressionInBrackets; - redraw(); - } -} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java b/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java new file mode 100644 index 00000000..ae802c61 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + */ + +package org.solovyev.android.calculator; + +import android.content.Context; +import android.graphics.Color; +import android.text.Html; +import android.util.AttributeSet; +import android.util.Log; +import android.widget.EditText; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.math.MathEntityType; + +/** + * User: serso + * Date: 9/17/11 + * Time: 12:25 AM + */ +public class CalculatorEditor extends EditText { + + private boolean highlightText = true; + + public CalculatorEditor(Context context) { + super(context); + } + + public CalculatorEditor(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public CalculatorEditor(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public void redraw() { + String text = getText().toString(); + + int selectionStart = getSelectionStart(); + int selectionEnd = getSelectionEnd(); + + if (highlightText) { + + text = highlightText(text); + + super.setText(Html.fromHtml(text), BufferType.EDITABLE); + } else { + super.setText(text, BufferType.EDITABLE); + } + + setSelection(selectionStart, selectionEnd); + } + + private String highlightText(@NotNull final String text) { + final String result; + + int maxNumberOfOpenGroupSymbols = 0; + int numberOfOpenGroupSymbols = 0; + + final StringBuilder text1 = new StringBuilder(); + + for (int i = 0; i < text.length(); i++) { + final MathEntityType.Result mathType = MathEntityType.getType(text, i); + + switch (mathType.getMathEntityType()) { + case open_group_symbol: + numberOfOpenGroupSymbols++; + maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols); + text1.append(text.charAt(i)); + break; + case close_group_symbol: + numberOfOpenGroupSymbols--; + text1.append(text.charAt(i)); + break; + case function: + i = processHighlightedText(text1, i, mathType.getS(), "i"); + break; + case constant: + i = processHighlightedText(text1, i, mathType.getS(), "b"); + break; + default: + text1.append(text.charAt(i)); + } + } + + if (maxNumberOfOpenGroupSymbols > 0) { + + final StringBuilder text2 = new StringBuilder(); + + processBracketGroup(text2, text1.toString(), 0, 0, maxNumberOfOpenGroupSymbols); + + Log.d(CalculatorEditor.class.getName(), text2.toString()); + + result = text2.toString(); + } else { + result = text1.toString(); + } + + return result; + } + + private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String functionName, @NotNull String tag) { + result.append("<").append(tag).append(">").append(functionName).append(""); + return i + functionName.length() - 1; + } + + private int processBracketGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) { + + result.append(""); + + for (; i < s.length(); i++) { + char ch = s.charAt(i); + + if (MathEntityType.openGroupSymbols.contains(ch)) { + result.append(ch); + result.append(""); + i = processBracketGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups); + result.append(""); + if (i < s.length() && MathEntityType.closeGroupSymbols.contains(s.charAt(i))) { + result.append(s.charAt(i)); + } + } else if (MathEntityType.closeGroupSymbols.contains(ch)) { + break; + } else { + result.append(ch); + } + } + + result.append(""); + + + return i; + } + + private String getColor(int numberOfOpenGroupSymbols, int numberOfOpenings) { + final int baseColor = Color.WHITE; + + double c = 1; + + int i = ((int) (255 * c)) * numberOfOpenings / (numberOfOpenGroupSymbols + 1); + + int result = Color.rgb(Color.red(baseColor) - i, Color.green(baseColor) - i, Color.blue(baseColor) - i); + + return "#" + Integer.toHexString(result).substring(2); + } + + public boolean isHighlightText() { + return highlightText; + } + + public void setHighlightText(boolean highlightText) { + this.highlightText = highlightText; + redraw(); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java index d6798c56..892c5306 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java @@ -41,7 +41,7 @@ public class CalculatorModel { private static CalculatorModel instance; - private CalculatorModel(@Nullable Context context) { + public CalculatorModel(@Nullable Context context) { load(context); reset(); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java index 2d0a36b5..4b90a37c 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -16,6 +16,7 @@ import android.view.*; import android.widget.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.math.MathEntityType; import org.solovyev.common.utils.StringUtils; import java.util.ArrayList; @@ -150,21 +151,26 @@ public class CalculatorVarsActivity extends ListActivity { if (!StringUtils.isEmpty(name)) { final Var varFromRegister = varsRegister.getVar(name); if (varFromRegister == null || varFromRegister == editedInstance) { + final MathEntityType.Result mathType = MathEntityType.getType(name, 0); - boolean correctDouble = true; - try { - Double.valueOf(value); - } catch (NumberFormatException e) { - correctDouble = false; - } + if (mathType.getMathEntityType() == MathEntityType.text || mathType.getMathEntityType() == MathEntityType.constant) { + boolean correctDouble = true; + try { + Double.valueOf(value); + } catch (NumberFormatException e) { + correctDouble = false; + } - if (correctDouble) { - varBuilder.setName(name); - varBuilder.setValue(value); - varBuilder.setDescription(description); - error = null; + if (correctDouble) { + varBuilder.setName(name); + varBuilder.setValue(value); + varBuilder.setDescription(description); + error = null; + } else { + error = "Value is not a number!"; + } } else { - error = "Value is not a number!"; + error = "Variable name clashes with function name!"; } } else { error = "Variable with same name already exist!"; diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorView.java b/src/main/java/org/solovyev/android/calculator/CalculatorView.java index bc6c63b6..875edb72 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorView.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorView.java @@ -39,7 +39,7 @@ public class CalculatorView implements CursorControl, HistoryControl= 0; result--) { - final MathEntityType mathEntityType = MathEntityType.getType(s, result); + final MathEntityType mathEntityType = MathEntityType.getMathEntityType(s, result); - if (mathEntityType != null) { - if (CollectionsUtils.contains(mathEntityType, MathEntityType.digit, MathEntityType.dot)) { - // continue - } else if (MathEntityType.closeGroupSymbols.contains(s.charAt(result))) { - numberOfOpenGroups++; - } else if (MathEntityType.openGroupSymbols.contains(s.charAt(result))) { - numberOfOpenGroups--; - } else { - if (stop(s, numberOfOpenGroups, result)) break; - } + if (CollectionsUtils.contains(mathEntityType, MathEntityType.digit, MathEntityType.dot)) { + // continue + } else if (mathEntityType == MathEntityType.close_group_symbol) { + numberOfOpenGroups++; + } else if (mathEntityType == MathEntityType.open_group_symbol) { + numberOfOpenGroups--; } else { if (stop(s, numberOfOpenGroups, result)) break; } @@ -136,8 +125,8 @@ public class ToJsclPreprocessor implements Preprocessor { final EndsWithFinder endsWithFinder = new EndsWithFinder(s); endsWithFinder.setI(i + 1); if (!CollectionsUtils.contains(MathEntityType.prefixFunctions, FilterType.included, endsWithFinder)) { - MathEntityType type = MathEntityType.getType(s, i); - if (type != null && type != MathEntityType.constant) { + MathEntityType type = MathEntityType.getMathEntityType(s, i); + if (type != MathEntityType.constant) { return true; } } @@ -185,25 +174,29 @@ public class ToJsclPreprocessor implements Preprocessor { } } - private static void checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb, @NotNull String s, int i, boolean constantBefore) { + @NotNull + private static MathEntityType.Result checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb, + @NotNull String s, + int i, + @Nullable MathEntityType.Result mathTypeBeforeResult) { + MathEntityType.Result result = MathEntityType.getType(s, i); + if (i > 0) { - // get character before function - char chBefore = s.charAt(i - 1); - char ch = s.charAt(i); - final MathEntityType mathTypeBefore = MathEntityType.getType(String.valueOf(chBefore)); - final MathEntityType mathType = MathEntityType.getType(s, i); + final MathEntityType mathType = result.getMathEntityType(); + assert mathTypeBeforeResult != null; + final MathEntityType mathTypeBefore = mathTypeBeforeResult.getMathEntityType(); - if (constantBefore || (mathTypeBefore != MathEntityType.binary_operation && + if (mathTypeBefore == MathEntityType.constant || (mathTypeBefore != MathEntityType.binary_operation && mathTypeBefore != MathEntityType.unary_operation && mathTypeBefore != MathEntityType.function && - !MathEntityType.openGroupSymbols.contains(chBefore))) { + mathTypeBefore != MathEntityType.open_group_symbol)) { if (mathType == MathEntityType.constant) { sb.append("*"); - } else if (MathEntityType.openGroupSymbols.contains(ch) && mathTypeBefore != null) { + } else if (mathType == MathEntityType.open_group_symbol && mathTypeBefore != null) { sb.append("*"); - } else if (mathType == MathEntityType.digit && ((mathTypeBefore != MathEntityType.digit && mathTypeBefore != MathEntityType.dot) || constantBefore) ) { + } else if (mathType == MathEntityType.digit && ((mathTypeBefore != MathEntityType.digit && mathTypeBefore != MathEntityType.dot) || mathTypeBefore == MathEntityType.constant)) { sb.append("*"); } else { for (String function : MathEntityType.prefixFunctions) { @@ -215,6 +208,8 @@ public class ToJsclPreprocessor implements Preprocessor { } } } + + return result; } public static String wrap(@NotNull JsclOperation operation, @NotNull String s) { diff --git a/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java b/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java index a18d216b..95b1b398 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java @@ -6,19 +6,16 @@ package org.solovyev.android.calculator.math; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.CharacterAtPositionFinder; import org.solovyev.android.calculator.StartsWithFinder; -import org.solovyev.android.calculator.Var; -import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.Finder; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; +import static org.solovyev.common.utils.CollectionsUtils.get; + public enum MathEntityType { digit, @@ -29,11 +26,14 @@ public enum MathEntityType { unary_operation, binary_operation, group_symbols, - group_symbol; + open_group_symbol, + close_group_symbol, + text; public static final List constants = Arrays.asList("e", "π", "i"); public static final List digits = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); + public static final List dots = Arrays.asList('.'); public static final List unaryOperations = Arrays.asList('-', '=', '!'); @@ -50,113 +50,114 @@ public enum MathEntityType { public static final List closeGroupSymbols = Arrays.asList(']', ')', '}'); - public static final List singleGroupSymbols; - - static { - final List list = new ArrayList(); - list.addAll(openGroupSymbols); - list.addAll(closeGroupSymbols); - singleGroupSymbols = Collections.unmodifiableList(list); + /** + * 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 + * @return math entity type of substring starting from ith index of specified text + */ + @NotNull + public static MathEntityType getMathEntityType(@NotNull String text, int i) { + return getType(text, i).getMathEntityType(); } - @Nullable - public static MathEntityType getType(@NotNull String s) { - MathEntityType result = null; - - if (s.length() == 1) { - result = getType(s.charAt(0)); + @NotNull + public static Result getType(@NotNull String text, int i) { + if (i < 0) { + throw new IllegalArgumentException("I must be more or equals to 0."); + } else if (i >= text.length() && i != 0) { + throw new IllegalArgumentException("I must be less than size of text."); + } else if (i == 0 && text.length() == 0) { + return new Result(MathEntityType.text, text); } - if (result == null) { - if (prefixFunctions.contains(s)) { - result = MathEntityType.function; - } else if (isConstant(s)) { - result = MathEntityType.constant; - } else if (groupSymbols.contains(s)) { - result = MathEntityType.group_symbols; - } + final StartsWithFinder stringStartWithFinder = new StartsWithFinder(text, i); + final CharacterAtPositionFinder characterStartWithFinder = new CharacterAtPositionFinder(text, i); + + String foundString = get(digits, stringStartWithFinder); + if (foundString != null) { + return new Result(MathEntityType.digit, foundString); } - - return result; - } - - @Nullable - public static MathEntityType getType(final char ch) { - MathEntityType result = null; - - if (Character.isDigit(ch)) { - result = MathEntityType.digit; - } else if (postfixFunctions.contains(ch)) { - result = MathEntityType.postfix_function; - } else if (unaryOperations.contains(ch)) { - result = MathEntityType.unary_operation; - } else if (binaryOperations.contains(ch)) { - result = MathEntityType.binary_operation; - } else if (singleGroupSymbols.contains(ch)) { - result = MathEntityType.group_symbol; - } else if (isConstant(ch)) { - result = MathEntityType.constant; - } else if (dots.contains(ch)) { - result = MathEntityType.dot; - } - return result; - } - - private static boolean isConstant(final char ch) { - final String name = String.valueOf(ch); - - return isConstant(name); - } - - private static boolean isConstant(final String name) { - return CollectionsUtils.get(CalculatorModel.getInstance().getVarsRegister().getVars(), new Finder() { - @Override - public boolean isFound(@Nullable Var var) { - return var != null && var.getName().equals(name); - } - }) != null; - } - - public static MathEntityType getType(String s, int i) { - final StartsWithFinder startsWithFinder = new StartsWithFinder(s, i); - final CharacterAtPositionFinder characterStartWithFinder = new CharacterAtPositionFinder(s, i); - - return getType(startsWithFinder, characterStartWithFinder); - } - - @Nullable - private static MathEntityType getType(@NotNull Finder finder, @NotNull CharacterAtPositionFinder characterStartWithFinder) { - MathEntityType result = null; - - if (contains(digits, finder)) { - result = MathEntityType.digit; - } else if (contains(postfixFunctions, characterStartWithFinder)) { - result = MathEntityType.postfix_function; - } else if (contains(unaryOperations, characterStartWithFinder)) { - result = MathEntityType.unary_operation; - } else if (contains(binaryOperations, characterStartWithFinder)) { - result = MathEntityType.binary_operation; - } else if (contains(groupSymbols, finder)) { - result = MathEntityType.group_symbols; - } else if (contains(singleGroupSymbols, characterStartWithFinder)) { - result = MathEntityType.group_symbol; - } else if (contains(prefixFunctions, finder)) { - result = MathEntityType.function; - } else if (contains(CalculatorModel.getInstance().getVarsRegister().getVarNames(), finder)) { - result = MathEntityType.constant; - } else if (contains(dots, characterStartWithFinder)) { - result = MathEntityType.dot; + Character foundCharacter = get(dots, characterStartWithFinder); + if (foundCharacter != null) { + return new Result(dot, String.valueOf(foundCharacter)); } - return result; + foundCharacter = get(postfixFunctions, characterStartWithFinder); + if (foundCharacter != null) { + return new Result(postfix_function, String.valueOf(foundCharacter)); + } + + foundCharacter = get(unaryOperations, characterStartWithFinder); + if (foundCharacter != null) { + return new Result(unary_operation, String.valueOf(foundCharacter)); + } + + foundCharacter = get(binaryOperations, characterStartWithFinder); + if (foundCharacter != null) { + return new Result(binary_operation, String.valueOf(foundCharacter)); + } + + foundString = get(groupSymbols, stringStartWithFinder); + if (foundString != null) { + return new Result(MathEntityType.group_symbols, foundString); + } + + foundCharacter = get(openGroupSymbols, characterStartWithFinder); + if (foundCharacter != null) { + return new Result(open_group_symbol, String.valueOf(foundCharacter)); + } + + foundCharacter = get(closeGroupSymbols, characterStartWithFinder); + if (foundCharacter != null) { + return new Result(close_group_symbol, String.valueOf(foundCharacter)); + } + + foundString = get(prefixFunctions, stringStartWithFinder); + if (foundString != null) { + return new Result(MathEntityType.function, foundString); + } + + foundString = get(CalculatorModel.getInstance().getVarsRegister().getVarNames(), stringStartWithFinder); + if (foundString != null) { + return new Result(MathEntityType.constant, foundString); + } + + return new Result(MathEntityType.text, text.substring(i)); + } + + public static class Result { + + @NotNull + private final MathEntityType mathEntityType; + + @NotNull + private final String s; + + private Result(@NotNull MathEntityType mathEntityType, @NotNull String s){ + this.mathEntityType = mathEntityType; + + this.s = s; + } + + @NotNull + public String getS() { + return s; + } + + @NotNull + public MathEntityType getMathEntityType() { + return mathEntityType; + } } private static boolean contains(@NotNull List list, @NotNull final Finder startsWithFinder) { - return CollectionsUtils.get(list, startsWithFinder) != null; + return get(list, startsWithFinder) != null; } private static boolean contains(@NotNull List list, @NotNull final CharacterAtPositionFinder atPositionFinder) { - return CollectionsUtils.get(list, atPositionFinder) != null; + return get(list, atPositionFinder) != null; } } diff --git a/src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java b/src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java new file mode 100644 index 00000000..de91f9bf --- /dev/null +++ b/src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math; + +import junit.framework.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.solovyev.android.calculator.CalculatorModel; + +/** + * User: serso + * Date: 10/5/11 + * Time: 1:25 AM + */ +public class MathEntityTypeTest { + + @BeforeClass + public static void setUp() throws Exception { + CalculatorModel.init(null); + } + + @Test + public void testGetType() throws Exception { + Assert.assertEquals(MathEntityType.function, MathEntityType.getType("sin", 0).getMathEntityType()); + Assert.assertEquals(MathEntityType.text, MathEntityType.getType("sn", 0).getMathEntityType()); + Assert.assertEquals(MathEntityType.text, MathEntityType.getType("s", 0).getMathEntityType()); + Assert.assertEquals(MathEntityType.text, MathEntityType.getType("", 0).getMathEntityType()); + + try { + Assert.assertEquals(MathEntityType.text, MathEntityType.getType("22", -1).getMathEntityType()); + Assert.fail(); + } catch (IllegalArgumentException e) { + } + + try { + Assert.assertEquals(MathEntityType.text, MathEntityType.getType("22", 2).getMathEntityType()); + Assert.fail(); + } catch (IllegalArgumentException e) { + } + } +}