1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v7.internal.view.menu;
18
19import android.support.v4.internal.view.SupportMenuItem;
20import android.view.MenuItem;
21import android.view.SubMenu;
22
23import java.util.HashMap;
24import java.util.Iterator;
25
26abstract class BaseMenuWrapper<T> extends BaseWrapper<T> {
27
28    private HashMap<MenuItem, SupportMenuItem> mMenuItems;
29
30    private HashMap<SubMenu, SubMenu> mSubMenus;
31
32    BaseMenuWrapper(T object) {
33        super(object);
34    }
35
36    final SupportMenuItem getMenuItemWrapper(android.view.MenuItem frameworkItem) {
37        if (frameworkItem != null) {
38            // Instantiate HashMap if null
39            if (mMenuItems == null) {
40                mMenuItems = new HashMap<MenuItem, SupportMenuItem>();
41            }
42
43            SupportMenuItem compatItem = mMenuItems.get(frameworkItem);
44
45            if (null == compatItem) {
46                compatItem = MenuWrapperFactory.createSupportMenuItemWrapper(frameworkItem);
47                mMenuItems.put(frameworkItem, compatItem);
48            }
49
50            return compatItem;
51        }
52        return null;
53    }
54
55    final SubMenu getSubMenuWrapper(android.view.SubMenu frameworkSubMenu) {
56        if (frameworkSubMenu != null) {
57            // Instantiate HashMap if null
58            if (mSubMenus == null) {
59                mSubMenus = new HashMap<android.view.SubMenu, SubMenu>();
60            }
61
62            SubMenu compatSubMenu = mSubMenus.get(frameworkSubMenu);
63
64            if (null == compatSubMenu) {
65                compatSubMenu = MenuWrapperFactory.createSupportSubMenuWrapper(frameworkSubMenu);
66                mSubMenus.put(frameworkSubMenu, compatSubMenu);
67            }
68            return compatSubMenu;
69        }
70        return null;
71    }
72
73
74    final void internalClear() {
75        if (mMenuItems != null) {
76            mMenuItems.clear();
77        }
78        if (mSubMenus != null) {
79            mSubMenus.clear();
80        }
81    }
82
83    final void internalRemoveGroup(final int groupId) {
84        if (mMenuItems == null) {
85            return;
86        }
87
88        Iterator<android.view.MenuItem> iterator = mMenuItems.keySet().iterator();
89        android.view.MenuItem menuItem;
90
91        while (iterator.hasNext()) {
92            menuItem = iterator.next();
93            if (groupId == menuItem.getGroupId()) {
94                iterator.remove();
95            }
96        }
97    }
98
99    final void internalRemoveItem(final int id) {
100        if (mMenuItems == null) {
101            return;
102        }
103
104        Iterator<android.view.MenuItem> iterator = mMenuItems.keySet().iterator();
105        android.view.MenuItem menuItem;
106
107        while (iterator.hasNext()) {
108            menuItem = iterator.next();
109            if (id == menuItem.getItemId()) {
110                iterator.remove();
111                break;
112            }
113        }
114    }
115}
116