diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java index 41bde78d..ecb910f7 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java @@ -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 buttonsByActionCodes = new HashMap(); + @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 localButtonsByActionCodes = new HashMap(specialButtons.length); + for (CalculatorSpecialButton specialButton : specialButtons) { + localButtonsByActionCodes.put(specialButton.getActionCode(), specialButton); + } + + buttonsByActionCodes = localButtonsByActionCodes; + } + } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/WidgetButton.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/WidgetButton.java index 4643fb29..94fab478 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/WidgetButton.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/widget/WidgetButton.java @@ -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 buttonsByIds = new HashMap(); + 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 null; + return buttonsByIds.get(buttonId); + } + + private static void initButtonsByIdsMap() { + if ( buttonsByIds.isEmpty() ) { + // if not initialized + + final WidgetButton[] widgetButtons = values(); + + final Map localButtonsByIds = new HashMap(widgetButtons.length); + for (WidgetButton widgetButton : widgetButtons) { + localButtonsByIds.put(widgetButton.getButtonId(), widgetButton); + } + + buttonsByIds = localButtonsByIds; + } } public int getButtonId() {