performance improvement
This commit is contained in:
		| @@ -3,6 +3,9 @@ package org.solovyev.android.calculator; | |||||||
| import org.jetbrains.annotations.NotNull; | import org.jetbrains.annotations.NotNull; | ||||||
| import org.jetbrains.annotations.Nullable; | import org.jetbrains.annotations.Nullable; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * User: serso |  * User: serso | ||||||
|  * Date: 10/20/12 |  * Date: 10/20/12 | ||||||
| @@ -128,6 +131,9 @@ public enum CalculatorSpecialButton { | |||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|  |     @NotNull | ||||||
|  |     private static Map<String, CalculatorSpecialButton> buttonsByActionCodes = new HashMap<String, CalculatorSpecialButton>(); | ||||||
|  |  | ||||||
|     @NotNull |     @NotNull | ||||||
|     private final String actionCode; |     private final String actionCode; | ||||||
|  |  | ||||||
| @@ -142,14 +148,24 @@ public enum CalculatorSpecialButton { | |||||||
|  |  | ||||||
|     @Nullable |     @Nullable | ||||||
|     public static CalculatorSpecialButton getByActionCode(@NotNull String actionCode) { |     public static CalculatorSpecialButton getByActionCode(@NotNull String actionCode) { | ||||||
|         for (CalculatorSpecialButton button : values()) { |         initButtonsByActionCodesMap(); | ||||||
|             if (button.getActionCode().equals(actionCode)) { |         return buttonsByActionCodes.get(actionCode); | ||||||
|                 return button; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return null; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public abstract void onClick(@NotNull CalculatorKeyboard keyboard); |     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; | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,6 +7,9 @@ import org.solovyev.android.calculator.CalculatorLocatorImpl; | |||||||
| import org.solovyev.android.calculator.CalculatorSpecialButton; | import org.solovyev.android.calculator.CalculatorSpecialButton; | ||||||
| import org.solovyev.android.calculator.R; | import org.solovyev.android.calculator.R; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| * User: serso | * User: serso | ||||||
| * Date: 10/20/12 | * Date: 10/20/12 | ||||||
| @@ -63,6 +66,9 @@ enum WidgetButton { | |||||||
|     @NotNull |     @NotNull | ||||||
|     private final String text; |     private final String text; | ||||||
|  |  | ||||||
|  |     @NotNull | ||||||
|  |     private static Map<Integer, WidgetButton> buttonsByIds = new HashMap<Integer, WidgetButton>(); | ||||||
|  |  | ||||||
|     WidgetButton(int buttonId, @NotNull CalculatorSpecialButton button) { |     WidgetButton(int buttonId, @NotNull CalculatorSpecialButton button) { | ||||||
|         this(buttonId, button.getActionCode()); |         this(buttonId, button.getActionCode()); | ||||||
|     } |     } | ||||||
| @@ -79,13 +85,24 @@ enum WidgetButton { | |||||||
|  |  | ||||||
|     @Nullable |     @Nullable | ||||||
|     public static WidgetButton getById(int buttonId) { |     public static WidgetButton getById(int buttonId) { | ||||||
|         for (WidgetButton widgetButton : values()) { |         initButtonsByIdsMap(); | ||||||
|             if (widgetButton.buttonId == buttonId) { |  | ||||||
|                 return widgetButton; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return null; |         return buttonsByIds.get(buttonId); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     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() { |     public int getButtonId() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sergey Solovyev
					Sergey Solovyev