This commit is contained in:
Sergey Solovyev 2012-10-11 01:22:37 +04:00
parent 64f5f0c44a
commit 9f30f3e5c0
7 changed files with 59 additions and 26 deletions

View File

@ -43,14 +43,14 @@ public class ListCalculatorEventContainer implements CalculatorEventContainer {
for (CalculatorEvent e : calculatorEvents) { for (CalculatorEvent e : calculatorEvents) {
for (CalculatorEventListener listener : listeners) { for (CalculatorEventListener listener : listeners) {
long startTime = System.currentTimeMillis(); /*long startTime = System.currentTimeMillis();*/
listener.onCalculatorEvent(e.getCalculatorEventData(), e.getCalculatorEventType(), e.getData()); listener.onCalculatorEvent(e.getCalculatorEventData(), e.getCalculatorEventType(), e.getData());
long endTime = System.currentTimeMillis(); /* long endTime = System.currentTimeMillis();
long totalTime = (endTime - startTime); long totalTime = (endTime - startTime);
if ( totalTime > 300 ) { 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(), "Started event: " + e.getCalculatorEventType() + " with data: " + e.getData() + " for: " + listener.getClass().getSimpleName());
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Total time, ms: " + totalTime); logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Total time, ms: " + totalTime);
} }*/
} }
} }
} }

View File

@ -11,7 +11,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory;
public class AbstractCalculatorTest { public class AbstractCalculatorTest {
protected void setUp() throws Exception { protected void setUp() throws Exception {
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), null); CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger());
CalculatorLocatorImpl.getInstance().getEngine().init(); CalculatorLocatorImpl.getInstance().getEngine().init();
} }

View File

@ -22,7 +22,7 @@ public class CalculatorTestUtils {
public static final int TIMEOUT = 1; public static final int TIMEOUT = 1;
public static void staticSetUp() throws Exception { public static void staticSetUp() throws Exception {
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), null); CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger());
CalculatorLocatorImpl.getInstance().getEngine().init(); CalculatorLocatorImpl.getInstance().getEngine().init();
} }

View File

@ -16,6 +16,9 @@ import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.view.TextHighlighter; import org.solovyev.android.calculator.view.TextHighlighter;
import org.solovyev.android.view.AutoResizeTextView; import org.solovyev.android.view.AutoResizeTextView;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* User: serso * User: serso
* Date: 9/17/11 * Date: 9/17/11
@ -53,6 +56,9 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
@NotNull @NotNull
private final Handler uiHandler = new Handler(); private final Handler uiHandler = new Handler();
@NotNull
private final ExecutorService bgExecutor = Executors.newSingleThreadExecutor();
private volatile boolean initialized = false; private volatile boolean initialized = false;
/* /*
@ -96,12 +102,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
try { try {
viewStateChange = true; viewStateChange = true;
long startTime = System.currentTimeMillis();
final CharSequence text = prepareText(state.getStringResult(), state.isValid()); final CharSequence text = prepareText(state.getStringResult(), state.isValid());
long endTime = System.currentTimeMillis();
long totalTime = (endTime - startTime);
CalculatorLocatorImpl.getInstance().getLogger().debug("CalculatorDisplayView", "Total time, ms: " + totalTime);
AndroidCalculatorDisplayView.this.state = state; AndroidCalculatorDisplayView.this.state = state;
if (state.isValid()) { if (state.isValid()) {

View File

@ -32,35 +32,46 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
public static class Result implements CharSequence { public static class Result implements CharSequence {
@NotNull @NotNull
private final String string; private final CharSequence charSequence;
@Nullable
private String string;
private final int offset; private final int offset;
public Result(@NotNull String string, int offset) { public Result(@NotNull CharSequence charSequence, int offset) {
this.string = string; this.charSequence = charSequence;
this.offset = offset; this.offset = offset;
} }
@Override @Override
public int length() { public int length() {
return string.length(); return charSequence.length();
} }
@Override @Override
public char charAt(int i) { public char charAt(int i) {
return string.charAt(i); return charSequence.charAt(i);
} }
@Override @Override
public CharSequence subSequence(int i, int i1) { public CharSequence subSequence(int i, int i1) {
return string.subSequence(i, i1); return charSequence.subSequence(i, i1);
} }
@Override @Override
public String toString() { public String toString() {
if (string == null) {
string = charSequence.toString();
}
return string; return string;
} }
@NotNull
public CharSequence getCharSequence() {
return charSequence;
}
public int getOffset() { public int getOffset() {
return offset; return offset;
} }
@ -86,7 +97,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
@NotNull @NotNull
@Override @Override
public Result process(@NotNull String text) throws CalculatorParseException { public Result process(@NotNull String text) throws CalculatorParseException {
final String result; final CharSequence result;
int maxNumberOfOpenGroupSymbols = 0; int maxNumberOfOpenGroupSymbols = 0;
int numberOfOpenGroupSymbols = 0; int numberOfOpenGroupSymbols = 0;
@ -158,7 +169,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
final StringBuilder text2 = new StringBuilder(text1.length()); final StringBuilder text2 = new StringBuilder(text1.length());
String s = text1.toString(); final CharSequence s = text1;
int i = processBracketGroup(text2, s, 0, 0, maxNumberOfOpenGroupSymbols); int i = processBracketGroup(text2, s, 0, 0, maxNumberOfOpenGroupSymbols);
for (; i < s.length(); i++) { for (; i < s.length(); i++) {
text2.append(s.charAt(i)); text2.append(s.charAt(i));
@ -177,7 +188,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String match, @NotNull String tag, @Nullable Map<String, String> tagAttributes) { private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String match, @NotNull String tag, @Nullable Map<String, String> tagAttributes) {
result.append("<").append(tag); result.append("<").append(tag);
if (tagAttributes != null) { if (tagAttributes != null && !tagAttributes.entrySet().isEmpty()) {
for (Map.Entry<String, String> entry : tagAttributes.entrySet()) { for (Map.Entry<String, String> entry : tagAttributes.entrySet()) {
// attr1="attr1_value" attr2="attr2_value" // attr1="attr1_value" attr2="attr2_value"
result.append(" ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\""); result.append(" ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
@ -192,14 +203,15 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
} }
} }
private int processBracketGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) { private int processBracketGroup(@NotNull StringBuilder result, @NotNull CharSequence s, int i, int numberOfOpenings, int maxNumberOfGroups) {
result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).append("\">"); result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).append("\">");
for (; i < s.length(); i++) { for (; i < s.length(); i++) {
char ch = s.charAt(i); char ch = s.charAt(i);
String strCh = String.valueOf(ch);
if (MathType.open_group_symbol.getTokens().contains(String.valueOf(ch))) { if (MathType.open_group_symbol.getTokens().contains(strCh)) {
result.append(ch); result.append(ch);
result.append("</font>"); result.append("</font>");
i = processBracketGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups); i = processBracketGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups);
@ -207,7 +219,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
if (i < s.length() && MathType.close_group_symbol.getTokens().contains(String.valueOf(s.charAt(i)))) { if (i < s.length() && MathType.close_group_symbol.getTokens().contains(String.valueOf(s.charAt(i)))) {
result.append(s.charAt(i)); result.append(s.charAt(i));
} }
} else if (MathType.close_group_symbol.getTokens().contains(String.valueOf(ch))) { } else if (MathType.close_group_symbol.getTokens().contains(strCh)) {
break; break;
} else { } else {
result.append(ch); result.append(ch);

View File

@ -13,7 +13,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory;
public class CalculatorTestUtils { public class CalculatorTestUtils {
public static void staticSetUp() throws Exception { public static void staticSetUp() throws Exception {
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), null); CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger());
CalculatorLocatorImpl.getInstance().getEngine().init(); CalculatorLocatorImpl.getInstance().getEngine().init();
} }

View File

@ -6,6 +6,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.graphics.Color;
import jscl.MathEngine; import jscl.MathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;
import junit.framework.Assert; import junit.framework.Assert;
@ -138,4 +139,23 @@ public class TextHighlighterTest {
me.setNumeralBase(NumeralBase.dec); me.setNumeralBase(NumeralBase.dec);
} }
} }
@Test
public void testTime() throws Exception {
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
final int count = 1000;
final String subExpression = "cos(acos(t8ln(t5t85tln(8ln(5t55tln(5))))))+tln(88cos(tln(t)))+t√(ln(t))";
final StringBuilder expression = new StringBuilder(subExpression.length() * count);
for ( int i = 0; i < count; i++ ){
expression.append(subExpression);
expression.append("+");
}
expression.append(subExpression);
long startTime = System.currentTimeMillis();
textHighlighter.process(expression.toString());
long endTime = System.currentTimeMillis();
System.out.println("Total time, ms: " + (endTime - startTime));
}
} }