performance improvement

This commit is contained in:
Sergey Solovyev 2012-11-18 15:00:44 +04:00
parent 8ea91849c4
commit 96d937e74b
2 changed files with 46 additions and 13 deletions

View File

@ -3,6 +3,9 @@ package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 10/20/12
@ -128,6 +131,9 @@ public enum CalculatorSpecialButton {
}
};
@NotNull
private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<String, CalculatorSpecialButton>();
@NotNull
private final String actionCode;
@ -142,14 +148,24 @@ public enum CalculatorSpecialButton {
@Nullable
public static CalculatorSpecialButton getByActionCode(@NotNull String actionCode) {
for (CalculatorSpecialButton button : values()) {
if (button.getActionCode().equals(actionCode)) {
return button;
}
}
return null;
initButtonsByActionCodesMap();
return buttonsByActionCodes.get(actionCode);
}
public abstract void onClick(@NotNull CalculatorKeyboard keyboard);
private static void initButtonsByActionCodesMap() {
if ( buttonsByActionCodes.isEmpty() ) {
// if not initialized
final CalculatorSpecialButton[] specialButtons = values();
final Map<String, CalculatorSpecialButton> localButtonsByActionCodes = new HashMap<String, CalculatorSpecialButton>(specialButtons.length);
for (CalculatorSpecialButton specialButton : specialButtons) {
localButtonsByActionCodes.put(specialButton.getActionCode(), specialButton);
}
buttonsByActionCodes = localButtonsByActionCodes;
}
}
}

View File

@ -7,6 +7,9 @@ import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorSpecialButton;
import org.solovyev.android.calculator.R;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 10/20/12
@ -63,6 +66,9 @@ enum WidgetButton {
@NotNull
private final String text;
@NotNull
private static Map<Integer, WidgetButton> buttonsByIds = new HashMap<Integer, WidgetButton>();
WidgetButton(int buttonId, @NotNull CalculatorSpecialButton button) {
this(buttonId, button.getActionCode());
}
@ -79,13 +85,24 @@ enum WidgetButton {
@Nullable
public static WidgetButton getById(int buttonId) {
for (WidgetButton widgetButton : values()) {
if (widgetButton.buttonId == buttonId) {
return widgetButton;
}
initButtonsByIdsMap();
return buttonsByIds.get(buttonId);
}
return null;
private static void initButtonsByIdsMap() {
if ( buttonsByIds.isEmpty() ) {
// if not initialized
final WidgetButton[] widgetButtons = values();
final Map<Integer, WidgetButton> localButtonsByIds = new HashMap<Integer, WidgetButton>(widgetButtons.length);
for (WidgetButton widgetButton : widgetButtons) {
localButtonsByIds.put(widgetButton.getButtonId(), widgetButton);
}
buttonsByIds = localButtonsByIds;
}
}
public int getButtonId() {